Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/staltz/callbag-to-rxjs
- Owner: staltz
- License: mit
- Created: 2018-02-16T19:10:00.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-02-16T19:10:20.000Z (over 6 years ago)
- Last Synced: 2024-10-09T09:47:32.262Z (about 1 month ago)
- Language: JavaScript
- Size: 4.88 KB
- Stars: 4
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.js
- License: LICENSE
Awesome Lists containing this project
- awesome-callbags - to-rxjs
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;