Skip to content

Commit

Permalink
Connection: Add client-side subscription (for WS)
Browse files Browse the repository at this point in the history
  • Loading branch information
andersevenrud committed Nov 29, 2016
1 parent 9114fa3 commit 69d2e9b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/client/javascript/core/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
*
* @constructor Connection
* @memberof OSjs.Core
* @mixes OSjs.Helpers.EventHandler
*/
function Connection() {
this.index = 0;
Expand All @@ -78,6 +79,8 @@
*/
this.offline = false;

this._evHandler = new OSjs.Helpers.EventHandler(name, []);

/*eslint consistent-this: "off"*/
_connectionInstance = this;
}
Expand Down Expand Up @@ -112,6 +115,10 @@
Connection.prototype.destroy = function() {
Utils.$unbind(window, 'offline');
Utils.$unbind(window, 'online');

if ( this._evHandler ) {
this._evHandler = this._evHandler.destroy();
}
};

/**
Expand Down Expand Up @@ -397,6 +404,38 @@
return true;
};

/**
* Subscribe to a event
*
* NOTE: This is only available on WebSocket connections
*
* @function subscribe
* @memberof OSjs.Core.Connection#
* @see OSjs.Helpers.EventHandler#on
*
* @param {String} k Event name
* @param {Function} func Callback function
*
* @return {Number}
*/
Connection.prototype.subscribe = function(k, func) {
return this._evHandler.on(k, func, this);
};

/**
* Removes an event subscription
*
* @function unsubscribe
* @memberof OSjs.Core.Connection#
* @see OSjs.Helpers.EventHandler#off
*
* @param {String} k Event name
* @param {Number} [idx] The hook index returned from subscribe()
*/
Connection.prototype.unsubscribe = function(k, idx) {
return this._evHandler.off(k, idx);
};

/////////////////////////////////////////////////////////////////////////////
// EXPORTS
/////////////////////////////////////////////////////////////////////////////
Expand Down
5 changes: 5 additions & 0 deletions src/client/javascript/core/connections/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@
if ( data.action === 'vfs:watch' ) {
VFS.Helpers.triggerWatch(data.action.split(':')[1], VFS.file(data.args));
}

// Emit a subscription event
if ( this._evHandler ) {
this._evHandler.emit(data.action, data.args);
}
};

WSConnection.prototype.request = function(method, args, onsuccess, onerror, options) {
Expand Down

0 comments on commit 69d2e9b

Please sign in to comment.