diff options
Diffstat (limited to 'toolkit/components/contentprefs/tests/unit_cps2/AsyncRunner.sys.mjs')
-rw-r--r-- | toolkit/components/contentprefs/tests/unit_cps2/AsyncRunner.sys.mjs | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/toolkit/components/contentprefs/tests/unit_cps2/AsyncRunner.sys.mjs b/toolkit/components/contentprefs/tests/unit_cps2/AsyncRunner.sys.mjs new file mode 100644 index 0000000000..3fab7c981f --- /dev/null +++ b/toolkit/components/contentprefs/tests/unit_cps2/AsyncRunner.sys.mjs @@ -0,0 +1,59 @@ +/* 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/. */ + +export function AsyncRunner(callbacks) { + this._callbacks = callbacks; + this._iteratorQueue = []; + + // This catches errors reported to the console, e.g., via Cu.reportError. + Services.console.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() { + Services.console.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); + } + }, +}; |