diff options
author | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
---|---|---|
committer | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
commit | 5f8de423f190bbb79a62f804151bc24824fa32d8 (patch) | |
tree | 10027f336435511475e392454359edea8e25895d /toolkit/components/contentprefs/tests/unit_cps2/AsyncRunner.jsm | |
parent | 49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff) | |
download | UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip |
Add m-esr52 at 52.6.0
Diffstat (limited to 'toolkit/components/contentprefs/tests/unit_cps2/AsyncRunner.jsm')
-rw-r--r-- | toolkit/components/contentprefs/tests/unit_cps2/AsyncRunner.jsm | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/toolkit/components/contentprefs/tests/unit_cps2/AsyncRunner.jsm b/toolkit/components/contentprefs/tests/unit_cps2/AsyncRunner.jsm new file mode 100644 index 000000000..ac878c28c --- /dev/null +++ b/toolkit/components/contentprefs/tests/unit_cps2/AsyncRunner.jsm @@ -0,0 +1,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); + } + }, +}; |