@@ -5,16 +5,7 @@ import Channel from './channel'
55import defaultGhostStoreProvider from './ghost/default'
66import defaultObjectStoreProvider from './storage/default'
77
8- var WebSocketClient ;
9- if ( typeof window !== 'undefined' && window . WebSocket ) {
10- WebSocketClient = window . WebSocket ;
11- } else {
12- WebSocketClient = require ( 'websocket' ) . w3cwebsocket ;
13- }
14-
15- module . exports = Client ;
16- module . exports . Bucket = Bucket ;
17- module . exports . Channel = Channel ;
8+ export { Bucket , Channel } ;
189
1910/**
2011 * @function
@@ -30,6 +21,12 @@ module.exports.Channel = Channel;
3021 * @returns {GhostStore } - the ghost store instance to be used by the bucket
3122 */
3223
24+ /**
25+ * @function
26+ * @name websocketClientProvider
27+ * @param {String } - The url to open the socket connection with
28+ * @returns {Object } a WebSocket client
29+
3330/**
3431 * A Client is the main interface to Simperium.
3532 *
@@ -40,14 +37,16 @@ module.exports.Channel = Channel;
4037 * - factory function for creating ghost store instances
4138 * @param {bucketStoreProvider } [options.objectStoreProvider=defaultObjectStoreProvider]
4239 * - factory function for creating object store instances
43- * @param {number } [heartbeatInterval=4] - heartbeat interval for maintaining connection status with Simperium.com
40+ * @param {number } [options.heartbeatInterval=4] - heartbeat interval for maintaining connection status with Simperium.com
41+ * @param {websocketClientProvider } [options.websocketClientProvider] - WebSocket transport, if not provided tries to use window.WebSocket
4442 */
45- function Client ( appId , accessToken , options ) {
43+ export function Client ( appId , accessToken , options ) {
4644 options = options || { } ;
4745
4846 options . ghostStoreProvider = options . ghostStoreProvider || defaultGhostStoreProvider ;
4947 options . objectStoreProvider = options . objectStoreProvider || defaultObjectStoreProvider ;
5048 options . hearbeatInterval = options . heartbeatInterval || 4 ;
49+ options . websocketClientProvider = options . websocketClientProvider || defaultWebsocketClientProvider ;
5150
5251 this . accessToken = accessToken ;
5352 this . open = false ;
@@ -116,10 +115,9 @@ Client.prototype.onHeartbeat = function( message ) {
116115Client . prototype . onConnect = function ( ) {
117116 this . open = true ;
118117
119- this . emit ( 'connect' ) ;
120-
121118 this . heartbeat . start ( ) ;
122119 this . reconnectionTimer . reset ( ) ;
120+ this . emit ( 'connect' ) ;
123121} ;
124122
125123Client . prototype . onReconnect = function ( attempt ) {
@@ -172,6 +170,7 @@ Client.prototype.send = function( data ) {
172170 this . socket . send ( data ) ;
173171 } catch ( e ) {
174172 // failed to send, probably not connected
173+ this . emit ( 'error' , e ) ;
175174 }
176175} ;
177176
@@ -181,7 +180,7 @@ Client.prototype.sendChannelMessage = function( id, message ) {
181180
182181Client . prototype . connect = function ( ) {
183182 this . reconnect = true ;
184- this . socket = new WebSocketClient ( this . options . url ) ;
183+ this . socket = this . options . websocketClientProvider ( this . options . url ) ;
185184
186185 this . socket . onopen = this . onConnect . bind ( this ) ;
187186 this . socket . onmessage = this . onMessage . bind ( this ) ;
@@ -199,6 +198,7 @@ Client.prototype.disconnect = function() {
199198Client . prototype . end = function ( ) {
200199 this . reconnect = false ;
201200 this . reconnectionTimer . stop ( ) ;
201+ this . heartbeat . stop ( ) ;
202202 this . disconnect ( ) ;
203203} ;
204204
@@ -215,7 +215,7 @@ Client.prototype.setAccessToken = function( token ) {
215215 this . connect ( ) ;
216216} ;
217217
218- function Heartbeat ( seconds , onBeat ) {
218+ export function Heartbeat ( seconds , onBeat ) {
219219 this . count = 0 ;
220220 this . seconds = seconds ;
221221 EventEmitter . call ( this ) ;
@@ -246,6 +246,7 @@ Heartbeat.prototype.tick = function( count ) {
246246
247247Heartbeat . prototype . start = function ( ) {
248248 this . stop ( ) ;
249+ clearTimeout ( this . timer ) ;
249250 this . timer = setTimeout ( this . onBeat . bind ( this ) , this . seconds * 1000 ) ;
250251} ;
251252
@@ -254,7 +255,7 @@ Heartbeat.prototype.stop = function() {
254255 clearTimeout ( this . timeout ) ;
255256} ;
256257
257- function ReconnectionTimer ( interval , onTripped ) {
258+ export function ReconnectionTimer ( interval , onTripped ) {
258259 EventEmitter . call ( this ) ;
259260
260261 this . started = false ;
@@ -290,3 +291,13 @@ ReconnectionTimer.prototype.reset = ReconnectionTimer.prototype.stop = function(
290291 this . started = false ;
291292 clearTimeout ( this . timer ) ;
292293} ;
294+
295+ function defaultWebsocketClientProvider ( url ) {
296+ let WebSocketClient ;
297+ if ( typeof window !== 'undefined' && window . WebSocket ) {
298+ WebSocketClient = window . WebSocket ;
299+ } else {
300+ WebSocketClient = require ( 'websocket' ) . w3cwebsocket ;
301+ }
302+ return new WebSocketClient ( url ) ;
303+ }
0 commit comments