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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/app-manager/connection-store.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,54 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +const ObservableObject = require("devtools/shared/observable-object");
     1.9 +const {Connection} = require("devtools/client/connection-manager");
    1.10 +
    1.11 +const _knownConnectionStores = new WeakMap();
    1.12 +
    1.13 +let ConnectionStore;
    1.14 +
    1.15 +module.exports = ConnectionStore = function(connection) {
    1.16 +  // If we already know about this connection,
    1.17 +  // let's re-use the existing store.
    1.18 +  if (_knownConnectionStores.has(connection)) {
    1.19 +    return _knownConnectionStores.get(connection);
    1.20 +  }
    1.21 +  _knownConnectionStores.set(connection, this);
    1.22 +
    1.23 +  ObservableObject.call(this, {status:null,host:null,port:null});
    1.24 +
    1.25 +  this.destroy = this.destroy.bind(this);
    1.26 +  this._feedStore = this._feedStore.bind(this);
    1.27 +
    1.28 +  this._connection = connection;
    1.29 +  this._connection.once(Connection.Events.DESTROYED, this.destroy);
    1.30 +  this._connection.on(Connection.Events.STATUS_CHANGED, this._feedStore);
    1.31 +  this._connection.on(Connection.Events.PORT_CHANGED, this._feedStore);
    1.32 +  this._connection.on(Connection.Events.HOST_CHANGED, this._feedStore);
    1.33 +  this._feedStore();
    1.34 +  return this;
    1.35 +}
    1.36 +
    1.37 +ConnectionStore.prototype = {
    1.38 +  destroy: function() {
    1.39 +    if (this._connection) {
    1.40 +      // While this.destroy is bound using .once() above, that event may not
    1.41 +      // have occurred when the ConnectionStore client calls destroy, so we
    1.42 +      // manually remove it here.
    1.43 +      this._connection.off(Connection.Events.DESTROYED, this.destroy);
    1.44 +      this._connection.off(Connection.Events.STATUS_CHANGED, this._feedStore);
    1.45 +      this._connection.off(Connection.Events.PORT_CHANGED, this._feedStore);
    1.46 +      this._connection.off(Connection.Events.HOST_CHANGED, this._feedStore);
    1.47 +      _knownConnectionStores.delete(this._connection);
    1.48 +      this._connection = null;
    1.49 +    }
    1.50 +  },
    1.51 +
    1.52 +  _feedStore: function() {
    1.53 +    this.object.status = this._connection.status;
    1.54 +    this.object.host = this._connection.host;
    1.55 +    this.object.port = this._connection.port;
    1.56 +  }
    1.57 +}

mercurial