Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/staltz/callbag-from-obs
👜 Convert an observable to a callbag listenable source
https://github.com/staltz/callbag-from-obs
Last synced: 5 days ago
JSON representation
👜 Convert an observable to a callbag listenable source
- Host: GitHub
- URL: https://github.com/staltz/callbag-from-obs
- Owner: staltz
- License: mit
- Created: 2018-01-23T21:17:26.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-04-20T05:39:54.000Z (over 1 year ago)
- Last Synced: 2024-12-24T20:38:16.501Z (20 days ago)
- Language: JavaScript
- Homepage:
- Size: 22.5 KB
- Stars: 3
- Watchers: 3
- Forks: 5
- Open Issues: 3
-
Metadata Files:
- Readme: readme.js
- License: LICENSE
Awesome Lists containing this project
- awesome-callbags - from-obs
README
/**
* callbag-from-obs
* --------------
*
* Convert an observable (or subscribable) to a callbag listenable source.
*
* `npm install callbag-from-obs`
*
* Example:
*
* Convert an RxJS Observable:
*
* const Rx = require('rxjs');
* const fromObs = require('callbag-from-obs');
* const observe = require('callbag-observe');
*
* const source = fromObs(Rx.Observable.interval(1000).take(4));
*
* observe(x => console.log(x))(source); // 0
* // 1
* // 2
* // 3
*
* Convert anything that has the `.subscribe` method:
*
* const fromObs = require('callbag-from-obs');
* const observe = require('callbag-observe');
*
* const subscribable = {
* subscribe: (observer) => {
* let i = 0;
* setInterval(() => observer.next(i++), 1000);
* }
* };
*
* const source = fromObs(subscribable);
*
* observe(x => console.log(x))(source); // 0
* // 1
* // 2
* // 3
* // ...
*/const $$observable = require('symbol-observable').default;
const fromObs = observable => (start, sink) => {
if (start !== 0) return;
let dispose;
sink(0, t => {
if (t === 2 && dispose) {
if (dispose.unsubscribe) dispose.unsubscribe();
else dispose();
}
});
observable = observable[$$observable] ? observable[$$observable]() : observable;
dispose = observable.subscribe({
next: x => sink(1, x),
error: e => sink(2, e),
complete: () => sink(2)
});
};module.exports = fromObs;