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