Created
July 22, 2017 18:47
-
-
Save busypeoples/8792f6cad4c570a44aba6cb88e4d729e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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