Hullo JS
Search…
Channel
import { Channel } from '@hullo/core'
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".
1
import { Channel } from '@hullo/core';
2
​
3
const ch = new Channel<string>();
4
​
5
/* here, subject allows for a message
6
* but since there is no subscription
7
* the message is lost */
8
ch.next('echo!');
9
​
10
const sub = ch.subscribe({
11
next: v => { console.log(v); }
12
});
13
​
14
/* here, there is a live subscription
15
* and so the message will be received */
16
ch.next('ECHO!');
Copied!
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.
1
import { Channel } from '@hullo/core';
2
const ch = new Channel<string>();
3
​
4
async function listen() {
5
console.log(await ch.take());
6
}
7
​
8
async function broadcast() {
9
await ch.next('hello');
10
}
11
​
12
listen();
13
broadcast();
14
​
15
// prints "hello" to the console
Copied!
Last modified 3yr ago
Copy link