blob: ac878c28cd7983111928aba00d0131544c82d5c4 (
plain)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
/* 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/. */
var EXPORTED_SYMBOLS = [
"AsyncRunner",
];
const { interfaces: Ci, classes: Cc } = Components;
function AsyncRunner(callbacks) {
this._callbacks = callbacks;
this._iteratorQueue = [];
// This catches errors reported to the console, e.g., via Cu.reportError.
Cc["@mozilla.org/consoleservice;1"].
getService(Ci.nsIConsoleService).
registerListener(this);
}
AsyncRunner.prototype = {
appendIterator: function AR_appendIterator(iter) {
this._iteratorQueue.push(iter);
},
next: function AR_next(arg) {
if (!this._iteratorQueue.length) {
this.destroy();
this._callbacks.done();
return;
}
try {
var { done, value } = this._iteratorQueue[0].next(arg);
if (done) {
this._iteratorQueue.shift();
this.next();
return;
}
}
catch (err) {
this._callbacks.error(err);
}
// val is truthy => call next
// val is an iterator => prepend it to the queue and start on it
if (value) {
if (typeof(value) != "boolean")
this._iteratorQueue.unshift(value);
this.next();
}
},
destroy: function AR_destroy() {
Cc["@mozilla.org/consoleservice;1"].
getService(Ci.nsIConsoleService).
unregisterListener(this);
this.destroy = function AR_alreadyDestroyed() {};
},
observe: function AR_consoleServiceListener(msg) {
if (msg instanceof Ci.nsIScriptError &&
!(msg.flags & Ci.nsIScriptError.warningFlag))
{
this._callbacks.consoleError(msg);
}
},
};
|