I was playing with js-csp
library and it's mult
function specifically. I setup a test with a source channel, a couple of channels tapped into mult and a result channel. Yet somehow not all values were distributed to the channels. Here's the code:
import csp from 'js-csp';
describe('Dispatcher', function() {
let source, broadcast, listeners, result;
beforeEach(function() {
source = csp.chan();
broadcast = csp.operations.mult(source);
result = csp.chan();
csp.operations.mult.tap(broadcast, result);
listeners = Array(csp.chan(), csp.chan(), csp.chan()).map((chan) => {
csp.operations.mult.tap(broadcast, chan);
return chan;
});
});
it('should broadcast the payload to all listeners', function(done) {
this.timeout(0);
let counter = 0;
csp.go(function*() {
let payload = yield result;
while (payload !== csp.CLOSED) {
console.log('[result]', payload);
if (payload.name === 'done') {
counter++;
console.log('[result]', counter);
if (counter === listeners.length) {
done();
}
}
payload = yield result;
}
});
listeners.forEach((chan, idx) => {
csp.go(function*() {
let payload = yield chan;
while (payload !== csp.CLOSED) {
if (payload.name !== 'done') {
console.log('[listener]', payload);
yield csp.put(source, {name: 'done'});
}
payload = yield chan;
}
});
});
csp.putAsync(source, {name: 'test'}, () => console.log('putAsync callback'));
});
});
I'm using Webpack with Babel.js and their version of Renegerator to transform this code into ES5. Once I run it (in the browser) the {name: 'test'}
message delivered to every channel, but {name: 'done'}
is delivered to result
channel only once.
I checked the source
channel in debugger and it has a puts
buffer which contains two remaining {name: 'done'}
values.
So I'm wondering what I'm doing wrong.