Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #782 from substance/es6-collab
Es6 collab
- Loading branch information
Showing
14 changed files
with
1,039 additions
and
1,109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,105 +1,102 @@ | ||
"use strict"; | ||
|
||
import EventEmitter from '../util/EventEmitter' | ||
import Err from '../util/SubstanceError' | ||
var __id__ = 0; | ||
|
||
let __id__ = 0 | ||
|
||
/** | ||
ClientConnection abstraction. Uses websockets internally | ||
*/ | ||
function ClientConnection(config) { | ||
ClientConnection.super.apply(this); | ||
class ClientConnection extends EventEmitter { | ||
constructor(config) { | ||
super() | ||
|
||
this.__id__ = __id__++; | ||
this.config = config; | ||
this._onMessage = this._onMessage.bind(this); | ||
this._onConnectionOpen = this._onConnectionOpen.bind(this); | ||
this._onConnectionClose = this._onConnectionClose.bind(this); | ||
this.__id__ = __id__++ | ||
this.config = config | ||
this._onMessage = this._onMessage.bind(this) | ||
this._onConnectionOpen = this._onConnectionOpen.bind(this) | ||
this._onConnectionClose = this._onConnectionClose.bind(this) | ||
|
||
// Establish websocket connection | ||
this._connect(); | ||
} | ||
// Establish websocket connection | ||
this._connect() | ||
} | ||
|
||
ClientConnection.Prototype = function() { | ||
|
||
this._createWebSocket = function() { | ||
throw Err('AbstractMethodError'); | ||
}; | ||
_createWebSocket() { | ||
throw Err('AbstractMethodError') | ||
} | ||
|
||
/* | ||
Initializes a new websocket connection | ||
*/ | ||
this._connect = function() { | ||
this.ws = this._createWebSocket(); | ||
this.ws.addEventListener('open', this._onConnectionOpen); | ||
this.ws.addEventListener('close', this._onConnectionClose); | ||
this.ws.addEventListener('message', this._onMessage); | ||
}; | ||
_connect() { | ||
this.ws = this._createWebSocket() | ||
this.ws.addEventListener('open', this._onConnectionOpen) | ||
this.ws.addEventListener('close', this._onConnectionClose) | ||
this.ws.addEventListener('message', this._onMessage) | ||
} | ||
|
||
/* | ||
Disposes the current websocket connection | ||
*/ | ||
this._disconnect = function() { | ||
this.ws.removeEventListener('message', this._onMessage); | ||
this.ws.removeEventListener('open', this._onConnectionOpen); | ||
this.ws.removeEventListener('close', this._onConnectionClose); | ||
this.ws = null; | ||
}; | ||
_disconnect() { | ||
this.ws.removeEventListener('message', this._onMessage) | ||
this.ws.removeEventListener('open', this._onConnectionOpen) | ||
this.ws.removeEventListener('close', this._onConnectionClose) | ||
this.ws = null | ||
} | ||
|
||
/* | ||
Emits open event when connection has been established | ||
*/ | ||
this._onConnectionOpen = function() { | ||
this.emit('open'); | ||
}; | ||
_onConnectionOpen() { | ||
this.emit('open') | ||
} | ||
|
||
/* | ||
Trigger reconnect on connection close | ||
*/ | ||
this._onConnectionClose = function() { | ||
this._disconnect(); | ||
this.emit('close'); | ||
console.info('websocket connection closed. Attempting to reconnect in 5s.'); | ||
_onConnectionClose() { | ||
this._disconnect() | ||
this.emit('close') | ||
console.info('websocket connection closed. Attempting to reconnect in 5s.') | ||
setTimeout(function() { | ||
this._connect(); | ||
}.bind(this), 5000); | ||
}; | ||
this._connect() | ||
}.bind(this), 5000) | ||
} | ||
|
||
/* | ||
Delegate incoming websocket messages | ||
*/ | ||
this._onMessage = function(msg) { | ||
msg = this.deserializeMessage(msg.data); | ||
this.emit('message', msg); | ||
}; | ||
_onMessage(msg) { | ||
msg = this.deserializeMessage(msg.data) | ||
this.emit('message', msg) | ||
} | ||
|
||
/* | ||
Send message via websocket channel | ||
*/ | ||
this.send = function(msg) { | ||
send(msg) { | ||
if (!this.isOpen()) { | ||
console.warn('Message could not be sent. Connection is not open.', msg); | ||
return; | ||
console.warn('Message could not be sent. Connection is not open.', msg) | ||
return | ||
} | ||
this.ws.send(this.serializeMessage(msg)); | ||
}; | ||
this.ws.send(this.serializeMessage(msg)) | ||
} | ||
|
||
/* | ||
Returns true if websocket connection is open | ||
*/ | ||
this.isOpen = function() { | ||
return this.ws && this.ws.readyState === 1; | ||
}; | ||
isOpen() { | ||
return this.ws && this.ws.readyState === 1 | ||
} | ||
|
||
this.serializeMessage = function(msg) { | ||
return JSON.stringify(msg); | ||
}; | ||
serializeMessage(msg) { | ||
return JSON.stringify(msg) | ||
} | ||
|
||
this.deserializeMessage = function(msg) { | ||
return JSON.parse(msg); | ||
}; | ||
deserializeMessage(msg) { | ||
return JSON.parse(msg) | ||
} | ||
|
||
}; | ||
} | ||
|
||
EventEmitter.extend(ClientConnection); | ||
export default ClientConnection; | ||
export default ClientConnection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,78 @@ | ||
"use strict"; | ||
|
||
import EventEmitter from '../util/EventEmitter' | ||
var __id__ = 0; | ||
let __id__ = 0 | ||
|
||
/** | ||
Client for CollabServer API | ||
Communicates via websocket for real-time operations | ||
*/ | ||
function CollabClient(config) { | ||
CollabClient.super.apply(this); | ||
|
||
this.__id__ = __id__++; | ||
this.config = config; | ||
this.connection = config.connection; | ||
class CollabClient extends EventEmitter { | ||
constructor(config) { | ||
super() | ||
|
||
// Hard-coded for now | ||
this.scope = 'substance/collab'; | ||
this.__id__ = __id__++ | ||
this.config = config | ||
this.connection = config.connection | ||
|
||
// Bind handlers | ||
this._onMessage = this._onMessage.bind(this); | ||
this._onConnectionOpen = this._onConnectionOpen.bind(this); | ||
this._onConnectionClose = this._onConnectionClose.bind(this); | ||
// Hard-coded for now | ||
this.scope = 'substance/collab' | ||
|
||
// Connect handlers | ||
this.connection.on('open', this._onConnectionOpen); | ||
this.connection.on('close', this._onConnectionClose); | ||
this.connection.on('message', this._onMessage); | ||
} | ||
// Bind handlers | ||
this._onMessage = this._onMessage.bind(this) | ||
this._onConnectionOpen = this._onConnectionOpen.bind(this) | ||
this._onConnectionClose = this._onConnectionClose.bind(this) | ||
|
||
CollabClient.Prototype = function() { | ||
// Connect handlers | ||
this.connection.on('open', this._onConnectionOpen) | ||
this.connection.on('close', this._onConnectionClose) | ||
this.connection.on('message', this._onMessage) | ||
} | ||
|
||
this._onConnectionClose = function() { | ||
this.emit('disconnected'); | ||
}; | ||
_onConnectionClose() { | ||
this.emit('disconnected') | ||
} | ||
|
||
this._onConnectionOpen = function() { | ||
this.emit('connected'); | ||
}; | ||
_onConnectionOpen() { | ||
this.emit('connected') | ||
} | ||
|
||
/* | ||
Delegate incoming messages from the connection | ||
*/ | ||
this._onMessage = function(msg) { | ||
_onMessage(msg) { | ||
if (msg.scope === this.scope) { | ||
this.emit('message', msg); | ||
this.emit('message', msg) | ||
} else { | ||
console.info('Message ignored. Not sent in hub scope', msg); | ||
console.info('Message ignored. Not sent in hub scope', msg) | ||
} | ||
}; | ||
} | ||
|
||
/* | ||
Send message via websocket channel | ||
*/ | ||
this.send = function(msg) { | ||
send(msg) { | ||
if (!this.connection.isOpen()) { | ||
console.warn('Message could not be sent. Connection not open.', msg); | ||
return; | ||
console.warn('Message could not be sent. Connection not open.', msg) | ||
return | ||
} | ||
|
||
msg.scope = this.scope; | ||
if (this.config.enhanceMessage) { | ||
msg = this.config.enhanceMessage(msg); | ||
msg = this.config.enhanceMessage(msg) | ||
} | ||
this.connection.send(msg); | ||
}; | ||
this.connection.send(msg) | ||
} | ||
|
||
/* | ||
Returns true if websocket connection is open | ||
*/ | ||
this.isConnected = function() { | ||
return this.connection.isOpen(); | ||
}; | ||
isConnected() { | ||
return this.connection.isOpen() | ||
} | ||
|
||
this.dispose = function() { | ||
this.connection.off(this); | ||
}; | ||
}; | ||
|
||
EventEmitter.extend(CollabClient); | ||
dispose() { | ||
this.connection.off(this) | ||
} | ||
} | ||
|
||
export default CollabClient; | ||
export default CollabClient |
Oops, something went wrong.