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.

import { Atom } from '@hullo/core';
const a = new Atom(/* initial state */ 0);
/* This will not get anywhere as a message
* but it'll be resent to any future subscriber
* as a first message */
a.next(1);
/* This subscription will log out to the console
* "A 1" immediately as that is a state of atom a */
const sub1 = a.subscribe({
next: v => { console.log('A', v); }
});
/* As there's no new subscription sending a message
* will trigger the same reaction
* as if `a` would be a channel.
* It will log "A 2" to the console */
a.next(2);
/* Every new subscription to an atom
* Will receive last message as its first message.
* This logs out "B 2".
* First subscription will not have this resent */
const sub2 = a.subscribe({
next: v => { console.log('B', v); }
});