本文整理汇总了TypeScript中rxjs/operators.publish函数的典型用法代码示例。如果您正苦于以下问题:TypeScript publish函数的具体用法?TypeScript publish怎么用?TypeScript publish使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了publish函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的TypeScript代码示例。
示例1: it
it('should unsub from the source when all other subscriptions are unsubbed', (done: MochaDone) => {
let unsubscribeCalled = false;
const connectable = new Observable<boolean>(observer => {
observer.next(true);
return () => {
unsubscribeCalled = true;
};
}).pipe(publish());
const refCounted = connectable.pipe(refCount());
const sub1 = refCounted.subscribe(() => {
//noop
});
const sub2 = refCounted.subscribe(() => {
//noop
});
const sub3 = refCounted.subscribe((x: any) => {
expect((connectable as any)._refCount).to.equal(1);
});
sub1.unsubscribe();
sub2.unsubscribe();
sub3.unsubscribe();
expect((connectable as any)._refCount).to.equal(0);
expect(unsubscribeCalled).to.be.true;
done();
});
开发者ID:DallanQ,项目名称:rxjs,代码行数:29,代码来源:refCount-spec.ts
示例2: publish1
publish1() {
// emit value every 1 second
const source = interval(1000);
const example = source.pipe(
// side effects will be executed once
tap(_ => console.log('Do Something!')),
// do nothing until connect() is called
publish()
) as ConnectableObservable<number>;
/*
source will not emit values until connect() is called
output: (after 5s)
"Do Something!"
"Subscriber One: 0"
"Subscriber Two: 0"
"Do Something!"
"Subscriber One: 1"
"Subscriber Two: 1"
*/
const subscribe = example.subscribe(val =>
console.log(`Subscriber One: ${val}`)
);
const subscribeTwo = example.subscribe(val =>
console.log(`Subscriber Two: ${val}`)
);
// call connect after 5 seconds, causing source to begin emitting items
setTimeout(() => {
example.connect();
}, 5000);
}
开发者ID:zwvista,项目名称:SampleMisc,代码行数:32,代码来源:connectable.service.ts
示例3: it
it('should support empty parameter', () => {
// Here, TypeScript versions 3.1 and earlier infer Observable<any>. However,
// the next version infers Observable<number>. It's not possible to specify
// an upper bound for the TypeScript version used by dtslint, so an
// expectation cannot be applied.
// TODO: put the test back after Typescript > 3.2
const a = of(1, 2, 3).pipe(publish());
});
开发者ID:jaychsu,项目名称:RxJS,代码行数:8,代码来源:publish-spec.ts
示例4: cold
'(dis)connecting hot one', () => {
const source = cold('--1-2---3-4--5-|');
const sourceSubs = '^ !';
const expected = '--1-2---3-4--5-|';
const result = source.pipe(
publish(),
refCount()
);
expectObservable(result).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
开发者ID:DallanQ,项目名称:rxjs,代码行数:13,代码来源:refCount-spec.ts
示例5: coldMakeHot
coldMakeHot() {
const observable$ = Observable.create((observer) => {
observer.next(Math.random());
// setTimeout(() => {
// observer.next(Math.random());
// }, 1000);
}).pipe(
// share()
// OR publish & connect
publish()
);
observable$.connect();
observable$.subscribe((data) => {
console.log('First subscription:', data); // First subscription: 0.5346840507763866
});
observable$.subscribe((data) => {
console.log('Second subscription:', data); // Second subscription: 0.5346840507763866
});
}
开发者ID:mr-Deviant,项目名称:sandbox,代码行数:22,代码来源:hot-cold.component.ts
示例6: watch
// Some hosts may not support watching.
watch(
path: Path,
_options?: virtualFs.HostWatchOptions,
): Observable<virtualFs.HostWatchEvent> | null {
return new Observable<virtualFs.HostWatchEvent>(obs => {
const opts = { persistent: false };
loadFSWatcher();
const watcher = new FSWatcher(opts).add(getSystemPath(path));
watcher
.on('change', path => {
obs.next({
path: normalize(path),
time: new Date(),
type: virtualFs.HostWatchEventType.Changed,
});
})
.on('add', path => {
obs.next({
path: normalize(path),
time: new Date(),
type: virtualFs.HostWatchEventType.Created,
});
})
.on('unlink', path => {
obs.next({
path: normalize(path),
time: new Date(),
type: virtualFs.HostWatchEventType.Deleted,
});
});
return () => watcher.close();
}).pipe(
publish(),
refCount(),
);
}
开发者ID:angular,项目名称:angular-cli,代码行数:39,代码来源:host.ts
示例7: fromEvent
...Object.keys(sockets).map(name => {
const socket = sockets[name];
// fromEvent typings are broken. socket will work as an event target.
return fromEvent(
// Pending a refactor around jmp, this allows us to treat the socket
// as a normal event emitter
(socket as unknown) as FromEventTarget<JupyterMessage>,
"message"
).pipe(
map(
(body: JupyterMessage): JupyterMessage => {
// Route the message for the frontend by setting the channel
const msg = { ...body, channel: name };
// Conform to same message format as notebook websockets
// See https://github.com/n-riesco/jmp/issues/10
delete (msg as any).idents;
return msg;
}
),
publish(),
refCount()
);
})
开发者ID:nteract,项目名称:nteract,代码行数:23,代码来源:index.ts
示例8: uuid
export const createMainChannelFromSockets = (
sockets: {
[name: string]: moduleJMP.Socket;
},
header: HeaderFiller = {
session: uuid(),
username: getUsername()
},
jmp = moduleJMP
): Channels => {
// The mega subject that encapsulates all the sockets as one multiplexed
// stream
const outgoingMessages = Subscriber.create<JupyterMessage>(
message => {
// There's always a chance that a bad message is sent, we'll ignore it
// instead of consuming it
if (!message || !message.channel) {
console.warn("message sent without a channel", message);
return;
}
const socket = sockets[message.channel];
if (!socket) {
// If, for some reason, a message is sent on a channel we don't have
// a socket for, warn about it but don't bomb the stream
console.warn("channel not understood for message", message);
return;
}
try {
const jMessage = new jmp.Message({
// Fold in the setup header to ease usage of messages on channels
header: { ...message.header, ...header },
parent_header: message.parent_header,
content: message.content,
metadata: message.metadata,
buffers: message.buffers
});
socket.send(jMessage);
} catch (err) {
console.error("Error sending message", err, message);
}
},
undefined, // not bothering with sending errors on
() =>
// When the subject is completed / disposed, close all the event
// listeners and shutdown the socket
Object.keys(sockets).forEach(name => {
const socket = sockets[name];
socket.removeAllListeners();
if (socket.close) socket.close();
})
);
// Messages from kernel on the sockets
const incomingMessages: Observable<JupyterMessage> = merge(
// Form an Observable with each socket
...Object.keys(sockets).map(name => {
const socket = sockets[name];
// fromEvent typings are broken. socket will work as an event target.
return fromEvent(
// Pending a refactor around jmp, this allows us to treat the socket
// as a normal event emitter
(socket as unknown) as FromEventTarget<JupyterMessage>,
"message"
).pipe(
map(
(body: JupyterMessage): JupyterMessage => {
// Route the message for the frontend by setting the channel
const msg = { ...body, channel: name };
// Conform to same message format as notebook websockets
// See https://github.com/n-riesco/jmp/issues/10
delete (msg as any).idents;
return msg;
}
),
publish(),
refCount()
);
})
).pipe(
publish(),
refCount()
);
const subject: Subject<JupyterMessage> = Subject.create(
outgoingMessages,
incomingMessages
);
return subject;
};
开发者ID:nteract,项目名称:nteract,代码行数:91,代码来源:index.ts
示例9: publish
// are already producing values even before a subscription is active.
//
// When an observer subscribes to a Multicast / Hot observable sequence
// it will get all values in the stream that are emitted after it subscribes.
//
// The Multicast / Hot observable sequence IS SHARED among all subscribers,
// and each subscriber is pushed the same next value in the sequence;
//
// The producer will keep going emitting values whether there's a subscriber or not.
//
// a Subject is the only way we can Multicast in rxjs,
// is there a way to make an Observable multicast ?
const o$ = new Observable<number>(subscriber => subscriber.next(Date.now()))
.pipe(
publish() // creates a ConnectableObservable: it creates and underlying Subject
// and shares it with the new subscribers
) as ConnectableObservable<number>; // there's an issue about type inference not working: https://github.com/ReactiveX/rxjs/issues/2972
// calling connect() will "trigger" the subscription to the original observable source
o$.connect(); // nothing will be displayed if we call connect() and activate the observable here, we subscribe a little bit too late.
// Output:
//
// (nothing)
o$.subscribe(v => console.log("1st subscriber: " + v));
o$.subscribe(v => console.log("2nd subscriber: " + v));
// o$.connect(); // we'll see the same value for all the observables if we activate it here
开发者ID:AGiorgetti,项目名称:rxjs101,代码行数:30,代码来源:01-ConnectableObservable.ts
注:本文中的rxjs/operators.publish函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论