Skip to content

Instantly share code, notes, and snippets.

@5alamander
Last active October 2, 2016 18:36
Show Gist options
  • Save 5alamander/e0205937c829ce5023fdb5946303ce62 to your computer and use it in GitHub Desktop.
Save 5alamander/e0205937c829ce5023fdb5946303ce62 to your computer and use it in GitHub Desktop.

JS CSP

basic usage

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()
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