Hullo JS
Search…
Atom
import { Atom } from '@hullo/core'
An Atom is quite similar to Channel, but it has one more feature. It retains last sent message through it as its state.
1
import { Atom } from '@hullo/core';
2
​
3
const a = new Atom(/* initial state */ 0);
4
​
5
/* This will not get anywhere as a message
6
* but it'll be resent to any future subscriber
7
* as a first message */
8
a.next(1);
9
​
10
/* This subscription will log out to the console
11
* "A 1" immediately as that is a state of atom a */
12
const sub1 = a.subscribe({
13
next: v => { console.log('A', v); }
14
});
15
​
16
/* As there's no new subscription sending a message
17
* will trigger the same reaction
18
* as if `a` would be a channel.
19
* It will log "A 2" to the console */
20
a.next(2);
21
​
22
/* Every new subscription to an atom
23
* Will receive last message as its first message.
24
* This logs out "B 2".
25
* First subscription will not have this resent */
26
const sub2 = a.subscribe({
27
next: v => { console.log('B', v); }
28
});
29
​
30
​
Copied!
Last modified 2yr ago
Copy link