callbag-combine
Advanced tools
Comparing version 1.0.2 to 1.1.0
{ | ||
"name": "callbag-combine", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Callbag factory that combines data from multiple callbag sources", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -47,4 +47,4 @@ /** | ||
const talkback = (t, d) => { | ||
if (t !== 2) return; | ||
for (let i = 0; i < n; i++) sourceTalkbacks[i](2); | ||
if (t === 0) return; | ||
for (let i = 0; i < n; i++) sourceTalkbacks[i](t, d); | ||
}; | ||
@@ -51,0 +51,0 @@ sources.forEach((source, i) => { |
75
test.js
@@ -283,1 +283,76 @@ const test = require('tape'); | ||
test('it combines pullable sources', (t) => { | ||
t.plan(19); | ||
const downwardsExpectedType = [ | ||
[0, 'function'], | ||
[1, 'object'], | ||
[1, 'object'], | ||
[1, 'object'], | ||
[1, 'object'], | ||
[1, 'object'], | ||
[2, 'undefined'] | ||
]; | ||
const downwardsExpected = [ | ||
[1, 'a'], | ||
[2, 'a'], | ||
[3, 'a'], | ||
[3, 'b'], | ||
[3, 'c'] | ||
]; | ||
function makePullable(values) { | ||
return (start, sink) => { | ||
let completed = false; | ||
sink(0, (type) => { | ||
if (completed) return; | ||
if (type === 1) { | ||
const value = values.shift(); | ||
if (values.length === 0) { | ||
completed = true; | ||
}; | ||
sink(1, value); | ||
if (completed) { | ||
sink(2); | ||
} | ||
} | ||
}); | ||
}; | ||
} | ||
function makeSink() { | ||
let talkback; | ||
return (type, data) => { | ||
const et = downwardsExpectedType.shift(); | ||
t.equals(type, et[0], 'downwards type is expected: ' + et[0]); | ||
t.equals(typeof data, et[1], 'downwards data type is expected: ' + et[1]); | ||
if (type === 2) return; | ||
if (type === 0) { | ||
talkback = data; | ||
} | ||
if (type === 1) { | ||
const e = downwardsExpected.shift(); | ||
t.deepEquals(data, e, 'downwards data is expected: ' + JSON.stringify(e)); | ||
} | ||
talkback(1); | ||
}; | ||
} | ||
const source = combine( | ||
makePullable([1, 2, 3]), | ||
makePullable(['a', 'b', 'c']) | ||
); | ||
source(0, makeSink()); | ||
t.end(); | ||
}); | ||
11925
385