[Client] Refactoring
This commit is contained in:
parent
431da9e087
commit
b6475958d0
19 changed files with 72 additions and 102 deletions
|
|
@ -8,12 +8,13 @@ class Connection {
|
|||
this.onOpen = this.onOpen.bind(this);
|
||||
this.onClose = this.onClose.bind(this);
|
||||
this.onMessage = this.onMessage.bind(this);
|
||||
this.send = this.send.bind(this);
|
||||
this.close = this.close.bind(this);
|
||||
// ----------------------------------------
|
||||
|
||||
riot.observable(this);
|
||||
|
||||
this.state = 'initializing';
|
||||
this.stateEv = riot.observable();
|
||||
this.event = riot.observable();
|
||||
this.me = me;
|
||||
|
||||
const host = CONFIG.apiUrl.replace('http', 'ws');
|
||||
|
|
@ -22,23 +23,23 @@ class Connection {
|
|||
this.socket.addEventListener('close', this.onClose);
|
||||
this.socket.addEventListener('message', this.onMessage);
|
||||
|
||||
this.event.on('i_updated', me.update);
|
||||
this.on('i_updated', me.update);
|
||||
}
|
||||
|
||||
onOpen() {
|
||||
this.state = 'connected';
|
||||
this.stateEv.trigger('connected');
|
||||
this.trigger('_connected_');
|
||||
}
|
||||
|
||||
onClose() {
|
||||
this.state = 'reconnecting';
|
||||
this.stateEv.trigger('closed');
|
||||
this.trigger('_closed_');
|
||||
}
|
||||
|
||||
onMessage(message) {
|
||||
try {
|
||||
const msg = JSON.parse(message.data);
|
||||
if (msg.type) this.event.trigger(msg.type, msg.body);
|
||||
if (msg.type) this.trigger(msg.type, msg.body);
|
||||
} catch(e) {
|
||||
// noop
|
||||
}
|
||||
|
|
|
|||
|
|
@ -52,8 +52,6 @@
|
|||
this.mixin('api');
|
||||
this.mixin('stream');
|
||||
|
||||
const stream = this.stream.event;
|
||||
|
||||
this.history = [];
|
||||
this.fetching = true;
|
||||
|
||||
|
|
@ -65,11 +63,11 @@
|
|||
});
|
||||
});
|
||||
|
||||
stream.on('signin', this.onSignin);
|
||||
this.stream.on('signin', this.onSignin);
|
||||
});
|
||||
|
||||
this.on('unmount', () => {
|
||||
stream.off('signin', this.onSignin);
|
||||
this.stream.off('signin', this.onSignin);
|
||||
});
|
||||
|
||||
this.onSignin = signin => {
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@
|
|||
}
|
||||
});
|
||||
|
||||
this.stream.stateEv.on('connected', () => {
|
||||
this.stream.on('_connected_', () => {
|
||||
this.update();
|
||||
setTimeout(() => {
|
||||
Velocity(this.root, {
|
||||
|
|
@ -53,7 +53,7 @@
|
|||
}, 1000);
|
||||
});
|
||||
|
||||
this.stream.stateEv.on('closed', () => {
|
||||
this.stream.on('_closed_', () => {
|
||||
this.update();
|
||||
Velocity(this.root, {
|
||||
opacity: 1
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue