Hullo JS
Search…
|> state
import { state } from '@hullo/core'
Use this operator to create new observable that'll always emit a message - either default or last one. Like subject it also returns a multicast observable.
1
import { Observable } from '@hullo/core';
2
​
3
const times = new Observable<string>(observer => {
4
if (Math.random() > 0.5) {
5
observer.next('now');
6
} else {
7
setTimeout(() => observer.next('later'), 10);
8
}
9
})
10
.pipe(state('sometime'));
11
12
times.subscribe({
13
next: v => { console.log(v); }
14
});
15
​
16
setTimeout(() => {
17
times.subscribe({
18
next: v => { console.log(v); }
19
});
20
}, 100)
Copied!
Example above will make it so a subscription in last lines will either:
  • receive just "now" cause immediately sent message through source will replace both default and last message
  • or receive "sometime" and then "later"
The second subscription will receive only "now" or "later" depends on what was the last message of the source. Original observable producer will not be run twice since source producer is kept live until there is at least one subscription of derivative stream (here times).
Last modified 2yr ago
Copy link