1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef MERGED_COMPARTMENT
7 this.EXPORTED_SYMBOLS = ["Async"];
9 const {classes: Cc, interfaces: Ci, results: Cr, utils: Cu} = Components;
13 // Constants for makeSyncCallback, waitForSyncCallback.
15 const CB_COMPLETE = {};
18 const REASON_ERROR = Ci.mozIStorageStatementCallback.REASON_ERROR;
20 Cu.import("resource://gre/modules/Services.jsm");
23 * Helpers for various async operations.
28 * Execute an arbitrary number of asynchronous functions one after the
29 * other, passing the callback arguments on to the next one. All functions
30 * must take a callback function as their last argument. The 'this' object
31 * will be whatever chain()'s is.
33 * @usage this._chain = Async.chain;
34 * this._chain(this.foo, this.bar, this.baz)(args, for, foo)
36 * This is equivalent to:
39 * self.foo(args, for, foo, function (bars, args) {
40 * self.bar(bars, args, function (baz, params) {
41 * self.baz(baz, params);
45 chain: function chain() {
46 let funcs = Array.slice(arguments);
48 return function callback() {
50 let args = Array.slice(arguments).concat(callback);
51 let f = funcs.shift();
52 f.apply(thisObj, args);
58 * Helpers for making asynchronous calls within a synchronous API possible.
60 * If you value your sanity, do not look closely at the following functions.
64 * Create a sync callback that remembers state, in particular whether it has
66 * The returned callback can be called directly passing an optional arg which
67 * will be returned by waitForSyncCallback(). The callback also has a
68 * .throw() method, which takes an error object and will cause
69 * waitForSyncCallback to fail with the error object thrown as an exception
70 * (but note that the .throw method *does not* itself throw - it just causes
71 * the wait function to throw).
73 makeSyncCallback: function makeSyncCallback() {
74 // The main callback remembers the value it was passed, and that it got data.
75 let onComplete = function onComplete(data) {
76 onComplete.state = CB_COMPLETE;
77 onComplete.value = data;
80 // Initialize private callback data in preparation for being called.
81 onComplete.state = CB_READY;
82 onComplete.value = null;
84 // Allow an alternate callback to trigger an exception to be thrown.
85 onComplete.throw = function onComplete_throw(data) {
86 onComplete.state = CB_FAIL;
87 onComplete.value = data;
94 * Wait for a sync callback to finish.
96 waitForSyncCallback: function waitForSyncCallback(callback) {
97 // Grab the current thread so we can make it give up priority.
98 let thread = Cc["@mozilla.org/thread-manager;1"].getService().currentThread;
100 // Keep waiting until our callback is triggered (unless the app is quitting).
101 while (Async.checkAppReady() && callback.state == CB_READY) {
102 thread.processNextEvent(true);
105 // Reset the state of the callback to prepare for another call.
106 let state = callback.state;
107 callback.state = CB_READY;
109 // Throw the value the callback decided to fail with.
110 if (state == CB_FAIL) {
111 throw callback.value;
114 // Return the value passed to the callback.
115 return callback.value;
119 * Check if the app is still ready (not quitting).
121 checkAppReady: function checkAppReady() {
122 // Watch for app-quit notification to stop any sync calls
123 Services.obs.addObserver(function onQuitApplication() {
124 Services.obs.removeObserver(onQuitApplication, "quit-application");
125 Async.checkAppReady = function() {
126 throw Components.Exception("App. Quitting", Cr.NS_ERROR_ABORT);
128 }, "quit-application", false);
129 // In the common case, checkAppReady just returns true
130 return (Async.checkAppReady = function() { return true; })();
134 * Return the two things you need to make an asynchronous call synchronous
135 * by spinning the event loop.
137 makeSpinningCallback: function makeSpinningCallback() {
138 let cb = Async.makeSyncCallback();
139 function callback(error, ret) {
145 callback.wait = function() Async.waitForSyncCallback(cb);
149 // Prototype for mozIStorageCallback, used in querySpinningly.
150 // This allows us to define the handle* functions just once rather
151 // than on every querySpinningly invocation.
152 _storageCallbackPrototype: {
155 // These are set by queryAsync.
159 handleResult: function handleResult(results) {
167 while ((row = results.getNextRow()) != null) {
169 for each (let name in this.names) {
170 item[name] = row.getResultByName(name);
172 this.results.push(item);
175 handleError: function handleError(error) {
176 this.syncCb.throw(error);
178 handleCompletion: function handleCompletion(reason) {
180 // If we got an error, handleError will also have been called, so don't
181 // call the callback! We never cancel statements, so we don't need to
182 // address that quandary.
183 if (reason == REASON_ERROR)
186 // If we were called with column names but didn't find any results,
187 // the calling code probably still expects an array as a return value.
188 if (this.names && !this.results) {
191 this.syncCb(this.results);
195 querySpinningly: function querySpinningly(query, names) {
196 // 'Synchronously' asyncExecute, fetching all results by name.
197 let storageCallback = {names: names,
198 syncCb: Async.makeSyncCallback()};
199 storageCallback.__proto__ = Async._storageCallbackPrototype;
200 query.executeAsync(storageCallback);
201 return Async.waitForSyncCallback(storageCallback.syncCb);