gecko-dev/browser/devtools/app-manager/connection-store.js

49 lines
1.7 KiB
JavaScript

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const ObservableObject = require("devtools/shared/observable-object");
const {Connection} = require("devtools/client/connection-manager");
const _knownConnectionStores = new WeakMap();
let ConnectionStore;
module.exports = ConnectionStore = function(connection) {
// If we already know about this connection,
// let's re-use the existing store.
if (_knownConnectionStores.has(connection)) {
return _knownConnectionStores.get(connection);
}
_knownConnectionStores.set(connection, this);
ObservableObject.call(this, {status:null,host:null,port:null});
this._destroy = this._destroy.bind(this);
this._feedStore = this._feedStore.bind(this);
this._connection = connection;
this._connection.once(Connection.Events.DESTROYED, this._destroy);
this._connection.on(Connection.Events.STATUS_CHANGED, this._feedStore);
this._connection.on(Connection.Events.PORT_CHANGED, this._feedStore);
this._connection.on(Connection.Events.HOST_CHANGED, this._feedStore);
this._feedStore();
return this;
}
ConnectionStore.prototype = {
_destroy: function() {
this._connection.off(Connection.Events.STATUS_CHANGED, this._feedStore);
this._connection.off(Connection.Events.PORT_CHANGED, this._feedStore);
this._connection.off(Connection.Events.HOST_CHANGED, this._feedStore);
_knownConnectionStores.delete(this._connection);
this._connection = null;
},
_feedStore: function() {
this.object.status = this._connection.status;
this.object.host = this._connection.host;
this.object.port = this._connection.port;
}
}