Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/staltz/callbag-to-rxjs

👜 Convert a listenable callbag source to an RxJS Observable
https://github.com/staltz/callbag-to-rxjs

Last synced: 26 days ago
JSON representation

👜 Convert a listenable callbag source to an RxJS Observable

Awesome Lists containing this project

README

        

/**
* callbag-to-rxjs
* ---------------
*
* Convert a listenable callbag source to an RxJS Observable.
*
* `npm install callbag-to-rxjs`
*
* Example:
*
* const {pipe, interval, take, filter, map} = require('callbag-basics');
* const toRx = require('callbag-to-rxjs');
* require('rxjs/add/operator/startWith');
*
* const observable = pipe(
* interval(1000), // 0,1,2,3,4,5,6,7,...
* take(5), // 0,1,2,3,4
* filter(x => x !== 0), // 1,2,3,4
* map(x => x * 10), // 10,20,30,40
* toRx
* );
*
* observable.startWith(0).subscribe({
* next: x => console.log(x)
* });
*/

const {Observable} = require('rxjs/Observable');

function toRx(source) {
return Observable.create(function subscribe(observer) {
let talkback;
try {
source(0, (t, d) => {
if (t === 0) talkback = d;
if (t === 1) observer.next(d);
if (t === 2 && d) observer.error(d);
else if (t === 2) talkback = void 0, observer.complete(d);
});
} catch (err) {
observer.error(err);
}
return function unsubscribe() {
if (talkback) talkback(2);
};
});
}

module.exports = toRx;