Channel is an observable and observer combined. That means you can write to and also read from it. Where observable is activated once there is a live subscription, Channel has no producer and is always "live".
import { Channel } from '@hullo/core';​const ch = new Channel<string>();​/* here, subject allows for a message* but since there is no subscription* the message is lost */ch.next('echo!');​const sub = ch.subscribe({next: v => { console.log(v); }});​/* here, there is a live subscription* and so the message will be received */ch.next('ECHO!');
Channel has also two additional methods, unique to channel: take
and tryTake
. With those you can get a Promise
of next message. Both of those are for the same purpose, one is graceful, the other fails fast.
import { Channel } from '@hullo/core';const ch = new Channel<string>();​async function listen() {console.log(await ch.take());}​async function broadcast() {await ch.next('hello');}​listen();broadcast();​// prints "hello" to the console