Last active
October 2, 2016 18:36
-
-
Save 5alamander/e0205937c829ce5023fdb5946303ce62 to your computer and use it in GitHub Desktop.
js csp, from https://github.com/ubolonton/js-csp
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
csp = require 'js-csp' | |
player = (name, table) -> | |
loop | |
ball = yield csp.take table | |
if ball is csp.CLOSED | |
console.log name + ": table's gone" | |
return | |
ball.hits += 1 | |
console.log name + " " + ball.hits | |
yield csp.timeout 100 | |
yield csp.put table, ball | |
csp.go -> | |
table = csp.chan() | |
csp.go player, ['ping', table] | |
csp.go player, ['pong', table] | |
yield csp.put(table, {hits: 0}) | |
yield csp.timeout 1000 | |
table.close() | |
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
csp = require 'js-csp' | |
ch = csp.chan(1) | |
csp.go -> | |
yield csp.timeout 1000 | |
yield csp.put ch, 42 | |
csp.go -> | |
timeCancle = csp.timeout 1500 | |
ret = yield csp.alts [ch, timeCancle] | |
if ret.channel is timeCancle | |
console.log 'time-out' | |
else | |
v = ret.value | |
console.log 'not-time-out' | |
console.log ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment