callbag-take
Version:
Callbag operator that limits the total amount of data sent through
33 lines (31 loc) • 649 B
JavaScript
const take = max => source => (start, sink) => {
if (start !== 0) return;
let taken = 0;
let sourceTalkback;
let end;
function talkback(t, d) {
if (t === 2) {
end = true;
sourceTalkback(t, d);
} else if (taken < max) sourceTalkback(t, d);
}
source(0, (t, d) => {
if (t === 0) {
sourceTalkback = d;
sink(0, talkback);
} else if (t === 1) {
if (taken < max) {
taken++;
sink(t, d);
if (taken === max && !end) {
end = true
sourceTalkback(2);
sink(2);
}
}
} else {
sink(t, d);
}
});
};
module.exports = take;