This commit is contained in:
syuilo 2017-02-18 13:18:59 +09:00
parent 5188ed3f10
commit fd5833303f
4 changed files with 42 additions and 42 deletions

View File

@ -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
};
};

View File

@ -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
}

View File

@ -1,8 +1,8 @@
# Stream # Stream
#================================ #================================
stream = require '../../common/scripts/stream.ls' stream = require '../../common/scripts/stream'
get-post-summary = require '../../common/scripts/get-post-summary.ls' get-post-summary = require '../../common/scripts/get-post-summary'
riot = require \riot riot = require \riot
module.exports = (me) ~> module.exports = (me) ~>

View File

@ -1,7 +1,7 @@
# Stream # Stream
#================================ #================================
stream = require '../../common/scripts/stream.ls' stream = require '../../common/scripts/stream'
riot = require \riot riot = require \riot
module.exports = (me) ~> module.exports = (me) ~>