Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/staltz/callbag-concat
👜 Callbag factory that concatenates data from multiple callbag sources
https://github.com/staltz/callbag-concat
Last synced: 5 days ago
JSON representation
👜 Callbag factory that concatenates data from multiple callbag sources
- Host: GitHub
- URL: https://github.com/staltz/callbag-concat
- Owner: staltz
- License: mit
- Created: 2018-01-26T17:03:59.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-02-11T11:24:47.000Z (almost 6 years ago)
- Last Synced: 2024-12-24T20:38:16.459Z (20 days ago)
- Language: JavaScript
- Homepage:
- Size: 19.5 KB
- Stars: 8
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: readme.js
- License: LICENSE
Awesome Lists containing this project
- awesome-callbags - concat
README
/**
* callbag-concat
* --------------
*
* Callbag factory that concatenates the data from multiple (2 or more)
* callbag sources. It starts each source at a time: waits for the previous
* source to end before starting the next source. Works with both pullable
* and listenable sources.
*
* `npm install callbag-concat`
*
* Example:
*
* const fromIter = require('callbag-from-iter');
* const iterate = require('callbag-iterate');
* const concat = require('callbag-concat');
*
* const source = concat(fromIter([10,20,30]), fromIter(['a','b']));
*
* iterate(x => console.log(x))(source); // 10
* // 20
* // 30
* // a
* // b
*/const UNIQUE = {};
const concat = (...sources) => (start, sink) => {
if (start !== 0) return;
const n = sources.length;
if (n === 0) {
sink(0, () => {});
sink(2);
return;
}
let i = 0;
let sourceTalkback;
let lastPull = UNIQUE;
const talkback = (t, d) => {
if (t === 1) lastPull = d;
sourceTalkback(t, d);
};
(function next() {
if (i === n) {
sink(2);
return;
}
sources[i](0, (t, d) => {
if (t === 0) {
sourceTalkback = d;
if (i === 0) sink(0, talkback);
else if (lastPull !== UNIQUE) sourceTalkback(1, lastPull);
} else if (t === 2 && d) {
sink(2, d);
} else if (t === 2) {
i++;
next();
} else {
sink(t, d);
}
});
})();
};export default concat;