linda-socket.io
Version:
Linda implementation on Socket.IO
84 lines (69 loc) • 2.42 kB
text/coffeescript
## linda client for webbrowser
class LindaClient
connect: () ->
return @
tuplespace: (name) ->
return new TupleSpace @, name
class TupleSpace
constructor: (, ) ->
= {}
= []
.io.on 'disconnect', =>
create_callback_id: ->
return Date.now() - Math.random()
create_watch_callback_id: (tuple) ->
key = JSON.stringify tuple
return [key] ||
[key] =
remove_io_callbacks: ->
for c in
.io.removeListener c.name, c.listener
= []
write: (tuple, options={expire: null}) ->
data = { tuplespace: , tuple: tuple, options: options }
.io.emit '__linda_write', data
take: (tuple, callback) ->
return unless typeof callback == 'function'
id =
name = "__linda_take_#{id}"
listener = (err, tuple) ->
callback err, tuple
.push {name: name, listener: listener}
.io.once name, listener
.io.emit '__linda_take', {tuplespace: , tuple: tuple, id: id}
return id
read: (tuple, callback) ->
return unless typeof callback == 'function'
id =
name = "__linda_read_#{id}"
listener = (err, tuple) ->
callback err, tuple
.push {name: name, listener: listener}
.io.once name, listener
.io.emit '__linda_read', {tuplespace: , tuple: tuple, id: id}
return id
watch: (tuple, callback) ->
return unless typeof callback == 'function'
id = tuple
name = "__linda_watch_#{id}"
listener = (err, tuple) ->
callback err, tuple
.push {name: name, listener: listener}
.io.on name, listener
.io.emit '__linda_watch', {tuplespace: , tuple: tuple, id: id}
return id
cancel: (id) ->
if .io.socket.connected
.io.emit '__linda_cancel', {tuplespace: , id: id}
setTimeout =>
for i in [(.length-1)..0]
c = [i]
if c.name.match(new RegExp "_#{id}$")
.io.removeListener c.name, c.listener
.splice i, 1
, 100
if window?
window.Linda = LindaClient
else if module?.exports?
module.exports = LindaClient