bmw-js
Version:
BMW javascript REST client for the BMW i3 hackathon.
78 lines (65 loc) • 2.82 kB
text/coffeescript
SignalR = require 'signalr-client'
module.exports = class SignalRNodeWrapper
observer_callbacks: {}
observer_registry: (entity) =>
# need to have the observer id to look this up here.
# could use the entity type
if [entity._id]
callback(entity) for callback in [entity._id]
else if [entity.Type]
callback(entity) for callback in [entity.Type]
constructor: (url, hubNames, jquery = null) ->
= url
= {}
= hubNames
getHub: (which, callback, retries = 10) ->
?= new SignalR.client(, , null)
return callback(null, [which]) if [which]?
if [which] = .hub(which)
[which].on 'error', (data) -> log data
[which].on 'UpdateEntity',
return callback null, [which]
else if retries-- > 0
_this = @
retry = () ->
_this.getHub which, callback, retries
setTimeout retry, 1000
else
callback "Timed out.", null
# TODO:: move callback list maintenance to separate class.
setCallback: (id, futureCallback) ->
if (![id]?)
[id] = []
[id].push(futureCallback)
return
removeCallback: (id, pastCallback) ->
if (pastCallback == null)
[id] = []
else
temp = []
# reform the obxerver_callbacks list without the given pastCallback
for callback in [id]
temp.push(callback) if (callback != pastCallback)
[id] = temp
return
subscribe: (hubName, method, observerId, subject, futureCallback, callback) ->
unsubscribe: (hubName, method, observerId, subject, pastCallback, callback) ->
if ([subject].length == 0)
else
callback(null, true)