Skip to content

Commit e9fa6c0

Browse files
committed
dynamic reload of components working!
1 parent f048aad commit e9fa6c0

9 files changed

Lines changed: 156 additions & 15 deletions

File tree

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ Development
1717

1818
This project is being developed within the [github.com/firebug/dev-system](https://github.com/firebug/dev-system). Once installed you can run:
1919

20-
pio publish --local firephp-for-firebug.next
21-
pio run firephp-for-firebug.next --open
20+
pio bundle --local firephp-for-firebug.next
21+
pio run --local --dev firephp-for-firebug.next --open
22+
pio run --local --dev firephp-for-firebug.next -- components
23+
pio run --local --dev firephp-for-firebug.next -- server
2224
profile run fbn-firephp
2325
profile run fbn-firephp-test
2426

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
3+
// Connect to server, on error reconnect and once reconnected
4+
// signal reconnect. Client can this determine when server is rebooted and comes back.
5+
// TODO: Include an instance ID in websocket condition so we can distinguish
6+
// between network issues and server restarts.
7+
8+
9+
function connectAndWaitForError (first, _onError) {
10+
11+
var onError = function (err) {
12+
if (!_onError) return;
13+
_onError(err);
14+
_onError = null;
15+
}
16+
17+
var ws = new WebSocket("ws://localhost:8080");
18+
ws.onopen = function (event) {
19+
if (first) {
20+
self.port.emit("opened");
21+
} else {
22+
self.port.emit("reopened");
23+
}
24+
// ws.send("Send test message from client to server!");
25+
};
26+
27+
ws.onerror = function (err) {
28+
onError(err);
29+
};
30+
31+
/*
32+
ws.onmessage = function (message) {
33+
self.port.emit("message", "got message: " + message);
34+
}
35+
*/
36+
37+
ws.onclose = function () {
38+
onError(new Error("closed"));
39+
}
40+
}
41+
42+
43+
function reconnect (first) {
44+
return setTimeout(function () {
45+
return connectAndWaitForError(first, function (err) {
46+
if (first) {
47+
self.port.emit("closed");
48+
}
49+
return reconnect(false);
50+
});
51+
}, 500);
52+
}
53+
54+
55+
reconnect(true);
56+

client/lib/main.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ function main(options, callbacks) {
2323
GDEVTOOLS.on("webconsole-init", onConsoleInit);
2424

2525

26-
SANDBOX(DATA.url("bundles/components/main.js"), function(sandbox) {
26+
var uri = DATA.url("bundles/components/main.js");
27+
// TODO: Get URI from mappings.
28+
uri = "http://localhost:8080/bundles/main.js";
29+
30+
SANDBOX(uri, function(sandbox) {
2731

2832
sandbox.main({
2933
CC: CC,
@@ -35,7 +39,13 @@ function main(options, callbacks) {
3539
CLASS: require("sdk/core/heritage").Class,
3640
DEV_PANEL: require("dev/panel.js").Panel,
3741
DEV_TOOL: require("dev/toolbox").Tool,
38-
PANEL: require("sdk/panel")
42+
PANEL: require("sdk/panel"),
43+
PAGE_WORKER: require("sdk/page-worker"),
44+
ADDON_INSTALLER: require("sdk/addon/installer"),
45+
TABS: require("sdk/tabs"),
46+
getBootOptions: function () {
47+
return options;
48+
}
3949
});
4050
});
4151

components/adapters/addon.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
// @see https://github.com/Gozala/jpm-addon/blob/master/core.js
3+
4+
exports.for = function (API) {
5+
6+
return {
7+
reload: function () {
8+
9+
var addonId = API.SELF.id;
10+
11+
return API.ADDON_INSTALLER.disable(addonId).then(function () {
12+
13+
API.CC["@mozilla.org/observer-service;1"].
14+
getService(API.CI.nsIObserverService).
15+
notifyObservers({}, "startupcache-invalidate", null);
16+
17+
return API.ADDON_INSTALLER.enable(addonId);
18+
});
19+
}
20+
};
21+
}

components/main.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,28 @@ exports.main = function (API) {
2121

2222
try {
2323

24+
const ADDON_API = makeAPI(API, {
25+
name: "adapters/addon"
26+
});
27+
const ADDON_EXPORTS = require("./adapters/addon").for(ADDON_API);
28+
29+
30+
31+
const MONITORS_BUNDLE_SERVER_API = makeAPI(API, {
32+
name: "monitors/bundle-server"
33+
});
34+
const MONITORS_BUNDLE_SERVER_EXPORTS = require("./monitors/bundle-server").for(MONITORS_BUNDLE_SERVER_API);
35+
36+
MONITORS_BUNDLE_SERVER_API.once("restarted", function () {
37+
38+
ADDON_EXPORTS.reload();
39+
40+
});
41+
42+
43+
2444
const HTTP_RESPONSE_OBSERVER_API = makeAPI(API, {
25-
name: "http-response-observer"
45+
name: "adapters/http-response-observer"
2646
});
2747
const HTTP_RESPONSE_OBSERVER_EXPORTS = require("./adapters/http-response-observer").for(HTTP_RESPONSE_OBSERVER_API);
2848

@@ -33,11 +53,19 @@ exports.main = function (API) {
3353
});
3454

3555

56+
3657
const UI_DEVTOOLS_PANEL_API = makeAPI(API, {
37-
name: "devtools-panel"
58+
name: "ui/devtools-panel"
3859
});
3960
const UI_DEVTOOLS_PANEL_EXPORTS = require("./ui/devtools-panel").for(UI_DEVTOOLS_PANEL_API);
4061

62+
63+
64+
API.console.log("Open tab!");
65+
API.TABS.open("http://localhost:49084/");
66+
67+
68+
4169
} catch (err) {
4270
console.error(err.stack);
4371
throw err;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
exports.for = function (API) {
3+
4+
var pageWorker = API.PAGE_WORKER.Page({
5+
contentScriptFile: "./bundle-server-monitor.js"
6+
});
7+
8+
/*
9+
pageWorker.port.on("message", function (msg) {
10+
API.console.log("monitor worker LOADED: " + msg);
11+
});
12+
*/
13+
14+
pageWorker.port.on("opened", function () {
15+
//API.console.log("SERVER OPENED");
16+
});
17+
18+
pageWorker.port.on("closed", function () {
19+
//API.console.log("SERVER CLOSED: trigger reload!");
20+
});
21+
22+
pageWorker.port.on("reopened", function () {
23+
//API.console.log("SERVER RE-OPENED!");
24+
API.emit("restarted");
25+
});
26+
27+
}

components/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
},
1515
"scripts": {
1616
"install": "../node_modules/.bin/smi install",
17-
"build": "rm -Rf .rt ; ../node_modules/to.pinf.lib/bin/pinf-bundle ; ../node_modules/pinf-to-browser/bin/pinf-publish",
18-
"run": "../node_modules/pinf-to-browser/bin/pinf-run",
19-
"start": "../node_modules/pinf-to-browser/bin/pinf-start"
17+
"bundle": "rm -Rf .rt ; ../node_modules/to.pinf.lib/bin/pinf-bundle ; ../node_modules/pinf-to-browser/bin/pinf-publish",
18+
"run": "../node_modules/pinf-to-browser/bin/pinf-run"
2019
}
2120
}

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
1919
},
2020
"scripts": {
2121
"install": "./node_modules/.bin/smi install ; cd components ; npm install --unsafe-perm ; cd .. ; cd ./server; npm install --unsafe-perm",
22-
"publish": "cd components ; npm run-script build ; cd .. ; cd ./server; npm run-script publish",
23-
"run": "./node_modules/to.pinf.lib/bin/pinf-run",
24-
"start": "./node_modules/to.pinf.lib/bin/pinf-start"
22+
"bundle": "cd components ; npm run-script bundle ; cd .. ; cd ./server; npm run-script bundle",
23+
"run": "./node_modules/to.pinf.lib/bin/pinf-run"
2524
},
2625
"config": {
2726
"smi.cli": {

server/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
},
1616
"scripts": {
1717
"install": "../node_modules/.bin/smi install ; ./node_modules/pinf-to-docker/bin/pinf-publish --ignore-dirty",
18-
"publish": "./node_modules/pinf-to-docker/bin/pinf-publish",
19-
"run": "./node_modules/pinf-to-docker/bin/pinf-run",
20-
"start": "./node_modules/pinf-to-docker/bin/pinf-start"
18+
"bundle": "./node_modules/pinf-to-docker/bin/pinf-publish",
19+
"run": "./node_modules/pinf-to-docker/bin/pinf-run"
2120
},
2221
"config": {
2322
"smi.cli": {

0 commit comments

Comments
 (0)