2016-12-28 22:49:51 +00:00
|
|
|
# Stream
|
|
|
|
#================================
|
|
|
|
|
|
|
|
ReconnectingWebSocket = require \reconnecting-websocket
|
|
|
|
riot = require \riot
|
|
|
|
|
|
|
|
module.exports = (me) ~>
|
|
|
|
state = \initializing
|
|
|
|
state-ev = riot.observable!
|
|
|
|
event = riot.observable!
|
|
|
|
|
2017-01-05 16:45:02 +00:00
|
|
|
host = CONFIG.api.url.replace \http \ws
|
|
|
|
socket = new ReconnectingWebSocket "#{host}?i=#{me.token}"
|
2016-12-28 22:49:51 +00:00
|
|
|
|
|
|
|
socket.onopen = ~>
|
|
|
|
state := \connected
|
|
|
|
state-ev.trigger \connected
|
|
|
|
|
|
|
|
socket.onclose = ~>
|
|
|
|
state := \reconnecting
|
|
|
|
state-ev.trigger \closed
|
|
|
|
|
|
|
|
socket.onmessage = (message) ~>
|
|
|
|
try
|
|
|
|
message = JSON.parse message.data
|
|
|
|
if message.type?
|
|
|
|
event.trigger message.type, message.body
|
|
|
|
catch
|
|
|
|
# ignore
|
|
|
|
|
|
|
|
get-state = ~> state
|
|
|
|
|
2017-01-21 12:08:40 +00:00
|
|
|
event.on \i_updated me.update
|
2016-12-28 22:49:51 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
state-ev
|
|
|
|
get-state
|
|
|
|
event
|
|
|
|
}
|