Hullo JS
Search…
|> subject
import { subject } from '@hullo/core'
Use this operator to turn observable from unicast (one producer run for every subscription alive) to multicast (one producer run for multiple subscriptions).
1
import { Observable, subject } from '@hullo/core';
2
​
3
const topic = new Observable<number>(observer => {
4
let i = 0;
5
function next() {
6
observer.next(i++);
7
setTimeout(next, 10)
8
}
9
});
10
​
11
topic.subscribe({ next: v => { console.log(1, v) } });
12
​
13
setTimeout(() => {
14
topic.subscribe({ next: v => { console.log(2, v) } });
15
}, 100);
Copied!
The first subscription will run the producer and so it starts to emit message every ten milliseconds. After about 10 message next subscriptions starts to be and since first one is still on, it does not trigger new producer to run, but starts to receive the same messages as first subscription.
Last modified 2yr ago
Copy link