This commit is contained in:
parent
d168ac6106
commit
4770e1fab8
|
@ -0,0 +1,39 @@
|
|||
const ReconnectingWebSocket = require('reconnecting-websocket');
|
||||
const riot = require('riot');
|
||||
|
||||
module.exports = me => {
|
||||
let state = 'initializing';
|
||||
const stateEv = riot.observable();
|
||||
const event = riot.observable();
|
||||
const host = CONFIG.api.url.replace('http', 'ws');
|
||||
const socket = new ReconnectingWebSocket(`${host}?i=${me.token}`);
|
||||
|
||||
socket.onopen = () => {
|
||||
state = 'connected';
|
||||
stateEv.trigger('connected');
|
||||
};
|
||||
|
||||
socket.onclose = () => {
|
||||
state = 'reconnecting';
|
||||
stateEv.trigger('closed');
|
||||
};
|
||||
|
||||
socket.onmessage = message => {
|
||||
try {
|
||||
const message = JSON.parse(message.data);
|
||||
if (message.type) {
|
||||
event.trigger(message.type, message.body);
|
||||
}
|
||||
} catch (e) {
|
||||
// noop
|
||||
}
|
||||
};
|
||||
|
||||
event.on('i_updated', me.update);
|
||||
|
||||
return {
|
||||
stateEv: stateEv,
|
||||
getState: () => state,
|
||||
event: event
|
||||
};
|
||||
};
|
|
@ -1,39 +0,0 @@
|
|||
# Stream
|
||||
#================================
|
||||
|
||||
ReconnectingWebSocket = require \reconnecting-websocket
|
||||
riot = require \riot
|
||||
|
||||
module.exports = (me) ~>
|
||||
state = \initializing
|
||||
state-ev = riot.observable!
|
||||
event = riot.observable!
|
||||
|
||||
host = CONFIG.api.url.replace \http \ws
|
||||
socket = new ReconnectingWebSocket "#{host}?i=#{me.token}"
|
||||
|
||||
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
|
||||
|
||||
event.on \i_updated me.update
|
||||
|
||||
{
|
||||
state-ev
|
||||
get-state
|
||||
event
|
||||
}
|
|
@ -1,8 +1,8 @@
|
|||
# Stream
|
||||
#================================
|
||||
|
||||
stream = require '../../common/scripts/stream.ls'
|
||||
get-post-summary = require '../../common/scripts/get-post-summary.ls'
|
||||
stream = require '../../common/scripts/stream'
|
||||
get-post-summary = require '../../common/scripts/get-post-summary'
|
||||
riot = require \riot
|
||||
|
||||
module.exports = (me) ~>
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
# Stream
|
||||
#================================
|
||||
|
||||
stream = require '../../common/scripts/stream.ls'
|
||||
stream = require '../../common/scripts/stream'
|
||||
riot = require \riot
|
||||
|
||||
module.exports = (me) ~>
|
||||
|
|
Loading…
Reference in New Issue