1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-11 01:12:41 +01:00
phorge-phorge/webroot/rsrc/js/application/aphlict/Aphlict.js
Bob Trahan d8ab5f594c Quicksand - make page object notifications work properly with quicksand
Summary: Fixes T7680. Make it so the listen behavior can be initialized multiple times from the server by having the behavior only update a few static data variables on subsequent initializations.

Test Plan:
visited TX with user A and left a comment with user B and got the "reload" and "TX updated" bubbles.
Reloaded and navigated to /maniphest/ with user A and had user B leave another comment on TX - no "reload" bubble and correct "TX updated" bubble.
Navigated to TX again with user A and had user B leave a comment and got the "reload" and "TX updated" bubbles.
visited TX with user A and left a comment with user B and got the "reload" and "TX updated" bubbles. navigated away with user A and the "reload" bubble was automagically closed.

Reviewers: epriestley

Reviewed By: epriestley

Subscribers: Korvin, epriestley

Maniphest Tasks: T7680

Differential Revision: https://secure.phabricator.com/D12448
2015-04-20 16:43:32 -07:00

168 lines
3.7 KiB
JavaScript

/**
* @provides javelin-aphlict
* @requires javelin-install
* javelin-util
* javelin-websocket
* javelin-leader
* javelin-json
*/
/**
* Client for the notification server. Example usage:
*
* var aphlict = new JX.Aphlict('ws://localhost:22280', subscriptions)
* .setHandler(function(message) {
* // ...
* })
* .start();
*
*/
JX.install('Aphlict', {
construct: function(uri, subscriptions) {
if (__DEV__) {
if (JX.Aphlict._instance) {
JX.$E('Aphlict object is a singleton.');
}
}
this._uri = uri;
this._subscriptions = subscriptions;
this._setStatus('setup');
JX.Aphlict._instance = this;
},
events: ['didChangeStatus'],
members: {
_uri: null,
_socket: null,
_subscriptions: null,
_status: null,
start: function() {
JX.Leader.listen('onBecomeLeader', JX.bind(this, this._lead));
JX.Leader.listen('onReceiveBroadcast', JX.bind(this, this._receive));
JX.Leader.start();
JX.Leader.call(JX.bind(this, this._begin));
},
setSubscriptions: function(subscriptions) {
this._subscriptions = subscriptions;
JX.Leader.broadcast(
null,
{type: 'aphlict.subscribe', data: this._subscriptions});
},
clearSubscriptions: function(subscriptions) {
this._subscriptions = null;
JX.Leader.broadcast(
null,
{type: 'aphlict.unsubscribe', data: subscriptions});
},
getStatus: function() {
return this._status;
},
_begin: function() {
JX.Leader.broadcast(
null,
{type: 'aphlict.getstatus'});
JX.Leader.broadcast(
null,
{type: 'aphlict.subscribe', data: this._subscriptions});
},
_lead: function() {
this._socket = new JX.WebSocket(this._uri);
this._socket.setOpenHandler(JX.bind(this, this._open));
this._socket.setMessageHandler(JX.bind(this, this._message));
this._socket.setCloseHandler(JX.bind(this, this._close));
this._socket.open();
},
_open: function() {
this._broadcastStatus('open');
JX.Leader.broadcast(null, {type: 'aphlict.getsubscribers'});
},
_close: function() {
this._broadcastStatus('closed');
},
_broadcastStatus: function(status) {
JX.Leader.broadcast(null, {type: 'aphlict.status', data: status});
},
_message: function(raw) {
var message = JX.JSON.parse(raw);
JX.Leader.broadcast(null, {type: 'aphlict.server', data: message});
},
_receive: function(message, is_leader) {
switch (message.type) {
case 'aphlict.status':
this._setStatus(message.data);
break;
case 'aphlict.getstatus':
if (is_leader) {
this._broadcastStatus(this.getStatus());
}
break;
case 'aphlict.getsubscribers':
JX.Leader.broadcast(
null,
{type: 'aphlict.subscribe', data: this._subscriptions});
break;
case 'aphlict.subscribe':
if (is_leader) {
this._write({
command: 'subscribe',
data: message.data
});
}
break;
default:
var handler = this.getHandler();
handler && handler(message);
break;
}
},
_setStatus: function(status) {
this._status = status;
this.invoke('didChangeStatus');
},
_write: function(message) {
this._socket.send(JX.JSON.stringify(message));
}
},
properties: {
handler: null
},
statics: {
_instance: null,
getInstance: function() {
var self = JX.Aphlict;
if (!self._instance) {
return null;
}
return self._instance;
}
}
});