toolkit/components/contentprefs/tests/unit_cps2/AsyncRunner.jsm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 let EXPORTED_SYMBOLS = [
michael@0 6 "AsyncRunner",
michael@0 7 ];
michael@0 8
michael@0 9 const { interfaces: Ci, classes: Cc } = Components;
michael@0 10
michael@0 11 function AsyncRunner(callbacks) {
michael@0 12 this._callbacks = callbacks;
michael@0 13 this._iteratorQueue = [];
michael@0 14
michael@0 15 // This catches errors reported to the console, e.g., via Cu.reportError.
michael@0 16 Cc["@mozilla.org/consoleservice;1"].
michael@0 17 getService(Ci.nsIConsoleService).
michael@0 18 registerListener(this);
michael@0 19 }
michael@0 20
michael@0 21 AsyncRunner.prototype = {
michael@0 22
michael@0 23 appendIterator: function AR_appendIterator(iter) {
michael@0 24 this._iteratorQueue.push(iter);
michael@0 25 },
michael@0 26
michael@0 27 next: function AR_next(/* ... */) {
michael@0 28 if (!this._iteratorQueue.length) {
michael@0 29 this.destroy();
michael@0 30 this._callbacks.done();
michael@0 31 return;
michael@0 32 }
michael@0 33
michael@0 34 // send() discards all arguments after the first, so there's no choice here
michael@0 35 // but to send only one argument to the yielder.
michael@0 36 let args = [arguments.length <= 1 ? arguments[0] : Array.slice(arguments)];
michael@0 37 try {
michael@0 38 var val = this._iteratorQueue[0].send.apply(this._iteratorQueue[0], args);
michael@0 39 }
michael@0 40 catch (err if err instanceof StopIteration) {
michael@0 41 this._iteratorQueue.shift();
michael@0 42 this.next();
michael@0 43 return;
michael@0 44 }
michael@0 45 catch (err) {
michael@0 46 this._callbacks.error(err);
michael@0 47 }
michael@0 48
michael@0 49 // val is truthy => call next
michael@0 50 // val is an iterator => prepend it to the queue and start on it
michael@0 51 if (val) {
michael@0 52 if (typeof(val) != "boolean")
michael@0 53 this._iteratorQueue.unshift(val);
michael@0 54 this.next();
michael@0 55 }
michael@0 56 },
michael@0 57
michael@0 58 destroy: function AR_destroy() {
michael@0 59 Cc["@mozilla.org/consoleservice;1"].
michael@0 60 getService(Ci.nsIConsoleService).
michael@0 61 unregisterListener(this);
michael@0 62 this.destroy = function AR_alreadyDestroyed() {};
michael@0 63 },
michael@0 64
michael@0 65 observe: function AR_consoleServiceListener(msg) {
michael@0 66 if (msg instanceof Ci.nsIScriptError &&
michael@0 67 !(msg.flags & Ci.nsIScriptError.warningFlag))
michael@0 68 {
michael@0 69 this._callbacks.consoleError(msg);
michael@0 70 }
michael@0 71 },
michael@0 72 };

mercurial