-
Notifications
You must be signed in to change notification settings - Fork 33
/
CmdRunner.jsm
41 lines (37 loc) · 1.12 KB
/
CmdRunner.jsm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
var EXPORTED_SYMBOLS = ["runCommand"];
var Cc = Components.classes;
var Ci = Components.interfaces;
var sWorker = null;
function createWorker() {
worker = new ChromeWorker("CmdRunWorker.js");
var hh = Cc["@mozilla.org/network/protocol;1?name=http"].getService(Ci.nsIHttpProtocolHandler);
worker.postMessage({type: "platform", cmd: hh["platform"]});
return worker;
}
function getWorker() {
if (!sWorker) {
sWorker = createWorker();
}
return sWorker;
}
function exec(cmd, uniqueWorker) {
var worker = getWorker();
if (uniqueWorker) {
worker = createWorker();
}
worker.postMessage({type: "exec", cmd: cmd});
}
function runCommand(cmd, callback, isProgressive, progress_callback) {
var worker = getWorker();
worker.addEventListener("message", function workerSentMessage(msg) {
if (msg.data.cmd == cmd) {
if (msg.data.progress) {
progress_callback(msg.data.progress);
} else { // finish
worker.removeEventListener("message", workerSentMessage);
callback(msg.data.result);
}
}
});
worker.postMessage({type: "runCommand", cmd: cmd, isProgressive: isProgressive});
}