Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/staltz/callbag-to-pull-stream
👜 Convert a pullable callbag source to a pull-stream source
https://github.com/staltz/callbag-to-pull-stream
Last synced: 5 days ago
JSON representation
👜 Convert a pullable callbag source to a pull-stream source
- Host: GitHub
- URL: https://github.com/staltz/callbag-to-pull-stream
- Owner: staltz
- License: mit
- Created: 2018-02-16T17:56:29.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-02-16T17:56:37.000Z (almost 7 years ago)
- Last Synced: 2024-12-24T20:38:17.067Z (20 days ago)
- Language: JavaScript
- Size: 4.88 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.js
- License: LICENSE
Awesome Lists containing this project
- awesome-callbags - to-pull-stream
README
/**
* callbag-to-pull-stream
* ----------------------
*
* Convert a pullable callbag source to a pull-stream source.
*
* `npm install callbag-to-pull-stream`
*
* Example:
*
* const pull = require('pull-stream');
* const {fromIter, pipe, filter, map} = require('callbag-basics');
* const toPullStream = require('callbag-to-pull-stream');
*
* const source = pipe(
* fromIter([1,3,5,7,9]),
* filter(x => x !== 5), // 1,3,7,9
* map(x => x * 10) // 10,30,70,90
* )
*
* pull(
* toPullStream(source),
* pull.filter(x => x !== 30), // 10,70,90
* pull.drain(x => console.log(x))
* )
*/function toPullStream(source) {
let talkback;
let cb;
source(0, (t, d) => {
let _cb = cb;
cb = void 0;
if (t === 0) talkback = d;
if (t === 1 && _cb) _cb(null, d);
if (t === 2 && _cb) _cb(d || true), talkback = void 0;
});
return function (end, _cb) {
cb = _cb;
if (end) {
if (talkback) talkback(2, end === true ? void 0 : end);
if (cb) cb(end);
return;
}
if (talkback) talkback(1);
else cb(true);
};
}module.exports = toPullStream;