Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/staltz/callbag-share
👜 Callbag operator that broadcasts a single source to multiple sinks
https://github.com/staltz/callbag-share
Last synced: 26 days ago
JSON representation
👜 Callbag operator that broadcasts a single source to multiple sinks
- Host: GitHub
- URL: https://github.com/staltz/callbag-share
- Owner: staltz
- License: mit
- Created: 2018-01-29T10:55:07.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2021-01-26T18:34:53.000Z (almost 4 years ago)
- Last Synced: 2024-10-10T00:50:02.432Z (about 1 month ago)
- Language: JavaScript
- Homepage:
- Size: 13.7 KB
- Stars: 22
- Watchers: 3
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
- awesome-callbags - share
README
# callbag-share
Callbag operator that broadcasts a single source to multiple sinks. Does reference counting on sinks and starts the source when the first sink gets connected, similar to RxJS [`.share()`](https://www.learnrxjs.io/operators/multicasting/share.html). Works on either pullable or listenable sources.
`npm install callbag-share`
## example
Share a listenable source to two listeners:
```js
const interval = require('callbag-interval');
const observe = require('callbag-observe');
const share = require('callbag-share');const source = share(interval(1000));
observe(x => console.log(x))(source); // 0
// 1
// 2
// 3
// ...setTimeout(() => {
observe(x => console.log(x))(source); // 3
// 4
// 5
// ...
}, 3500);
```Share a pullable source to two pullers:
```js
const fromIter = require('callbag-from-iter');
const share = require('callbag-share');const source = share(fromIter([10,20,30,40,50]));
let talkback;
source(0, (type, data) => {
if (type === 0) talkback = data;
else console.log('a' + data);
});source(0, (type, data) => {
if (type === 1) console.log('b' + data);
});talkback(1); // a10
// b10
talkback(1); // a20
// b20
```