Bug 901519 - [app manager] data store for connections. r=alex.poirot

This commit is contained in:
Paul Rouget 2013-08-21 08:56:40 +02:00
parent aa4f0af233
commit 55f76e3281
3 changed files with 156 additions and 1 deletions

View File

@ -0,0 +1,48 @@
/* 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;
}
}

View File

@ -12,6 +12,7 @@ include $(DEPTH)/config/autoconf.mk
MOCHITEST_CHROME_FILES = \ MOCHITEST_CHROME_FILES = \
test_template.html \ test_template.html \
test_connection_store.html \
$(NULL) $(NULL)
include $(topsrcdir)/config/rules.mk include $(topsrcdir)/config/rules.mk

View File

@ -0,0 +1,106 @@
<!DOCTYPE html>
<!--
Bug 901519 - [app manager] data store for connections
-->
<html>
<head>
<meta charset="utf8">
<title></title>
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
</head>
<body>
<div id="root">
<span id="status" template='{"type":"textContent","path":"status"}'></span>
<span id="host" template='{"type":"textContent","path":"host"}'></span>
<span id="port" template='{"type":"textContent","path":"port"}'></span>
</div>
<script type="application/javascript;version=1.8" src="chrome://browser/content/devtools/app-manager/template.js"></script>
<script type="application/javascript;version=1.8">
const Cu = Components.utils;
Cu.import("resource://gre/modules/devtools/dbg-server.jsm");
DebuggerServer.init(function () { return true; });
DebuggerServer.addBrowserActors();
window.onload = function() {
SimpleTest.waitForExplicitFinish();
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource:///modules/devtools/gDevTools.jsm");
const {devtools} = Cu.import("resource://gre/modules/devtools/Loader.jsm", {});
const {require} = devtools;
const {ConnectionManager} = require("devtools/client/connection-manager");
const ConnectionStore = require("devtools/app-manager/connection-store");
let connection = ConnectionManager.createConnection();
let store = new ConnectionStore(connection);
let root = document.querySelector("#root");
let status = root.querySelector("#status");
let host = root.querySelector("#host");
let port = root.querySelector("#port");
let template = new Template(root, store, () => {});
template.start();
connection.host = "foobar";
connection.port = 42;
is(host.textContent, "foobar", "host updated");
is(port.textContent, 42, "port updated");
let been_through_connecting = false;
let been_through_connected = false;
let been_through_disconnected = false;
is(status.textContent, "disconnected", "status updated (diconnected)");
connection.once("connecting", (e) => {
SimpleTest.executeSoon(() => {
been_through_connecting = true;
is(status.textContent, "connecting", "status updated (connecting)");
})
});
connection.once("connected", (e) => {
SimpleTest.executeSoon(() => {
been_through_connected = true;
is(status.textContent, "connected", "status updated (connected)");
connection.disconnect();
})
});
connection.once("disconnected", (e) => {
SimpleTest.executeSoon(() => {
been_through_disconnected = true;
is(status.textContent, "disconnected", "status updated (disconnected)");
connection.destroy();
finishup();
})
});
function finishup() {
ok(been_through_connecting &&
been_through_connected &&
been_through_disconnected, "All updates happened");
DebuggerServer.destroy();
SimpleTest.finish();
}
connection.host = null; // force pipe
connection.port = null;
connection.connect();
}
</script>
</body>
</html>