|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 const ObservableObject = require("devtools/shared/observable-object"); |
|
6 const {Connection} = require("devtools/client/connection-manager"); |
|
7 |
|
8 const _knownConnectionStores = new WeakMap(); |
|
9 |
|
10 let ConnectionStore; |
|
11 |
|
12 module.exports = ConnectionStore = function(connection) { |
|
13 // If we already know about this connection, |
|
14 // let's re-use the existing store. |
|
15 if (_knownConnectionStores.has(connection)) { |
|
16 return _knownConnectionStores.get(connection); |
|
17 } |
|
18 _knownConnectionStores.set(connection, this); |
|
19 |
|
20 ObservableObject.call(this, {status:null,host:null,port:null}); |
|
21 |
|
22 this.destroy = this.destroy.bind(this); |
|
23 this._feedStore = this._feedStore.bind(this); |
|
24 |
|
25 this._connection = connection; |
|
26 this._connection.once(Connection.Events.DESTROYED, this.destroy); |
|
27 this._connection.on(Connection.Events.STATUS_CHANGED, this._feedStore); |
|
28 this._connection.on(Connection.Events.PORT_CHANGED, this._feedStore); |
|
29 this._connection.on(Connection.Events.HOST_CHANGED, this._feedStore); |
|
30 this._feedStore(); |
|
31 return this; |
|
32 } |
|
33 |
|
34 ConnectionStore.prototype = { |
|
35 destroy: function() { |
|
36 if (this._connection) { |
|
37 // While this.destroy is bound using .once() above, that event may not |
|
38 // have occurred when the ConnectionStore client calls destroy, so we |
|
39 // manually remove it here. |
|
40 this._connection.off(Connection.Events.DESTROYED, this.destroy); |
|
41 this._connection.off(Connection.Events.STATUS_CHANGED, this._feedStore); |
|
42 this._connection.off(Connection.Events.PORT_CHANGED, this._feedStore); |
|
43 this._connection.off(Connection.Events.HOST_CHANGED, this._feedStore); |
|
44 _knownConnectionStores.delete(this._connection); |
|
45 this._connection = null; |
|
46 } |
|
47 }, |
|
48 |
|
49 _feedStore: function() { |
|
50 this.object.status = this._connection.status; |
|
51 this.object.host = this._connection.host; |
|
52 this.object.port = this._connection.port; |
|
53 } |
|
54 } |