Skip to content

Instantly share code, notes, and snippets.

@busypeoples
Created July 22, 2017 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save busypeoples/8792f6cad4c570a44aba6cb88e4d729e to your computer and use it in GitHub Desktop.
Save busypeoples/8792f6cad4c570a44aba6cb88e4d729e to your computer and use it in GitHub Desktop.
import {
alts,
chan,
go,
operations,
putAsync,
take,
takeAsync,
timeout,
CLOSED,
} from 'js-csp';
const TestChannelsBox = () => ({
log: (name, channel, active = true) => {
const mult = operations.mult(channel);
let listenerChannel = chan();
operations.mult.tap(mult, listenerChannel);
go(function*() {
let value = yield listenerChannel;
while (value !== CLOSED) {
console.log(`Channel "${name}" received value ${value}`);
value = yield listenerChannel;
}
console.log('Closed Logging Channel.');
});
return () => {
operations.mult.untap(mult, listenerChannel);
listenerChannel.close();
};
},
channels: {
one: chan(),
two: chan(),
three: chan(),
},
put: putAsync,
take: takeAsync,
alts: (ch1, ch2) => {
go(function*() {
const result = yield alts([ch1, ch2]);
console.log(
'which channel ran first?',
result.channel === ch1 ? 'First' : 'Second',
);
});
},
go,
timeout,
});
window.box = TestChannelsBox();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment