async.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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/. */
  4. this.EXPORTED_SYMBOLS = ["Async"];
  5. var {classes: Cc, interfaces: Ci, results: Cr, utils: Cu} = Components;
  6. // Constants for makeSyncCallback, waitForSyncCallback.
  7. const CB_READY = {};
  8. const CB_COMPLETE = {};
  9. const CB_FAIL = {};
  10. const REASON_ERROR = Ci.mozIStorageStatementCallback.REASON_ERROR;
  11. Cu.import("resource://gre/modules/Services.jsm");
  12. /*
  13. * Helpers for various async operations.
  14. */
  15. this.Async = {
  16. /**
  17. * Execute an arbitrary number of asynchronous functions one after the
  18. * other, passing the callback arguments on to the next one. All functions
  19. * must take a callback function as their last argument. The 'this' object
  20. * will be whatever chain()'s is.
  21. *
  22. * @usage this._chain = Async.chain;
  23. * this._chain(this.foo, this.bar, this.baz)(args, for, foo)
  24. *
  25. * This is equivalent to:
  26. *
  27. * let self = this;
  28. * self.foo(args, for, foo, function (bars, args) {
  29. * self.bar(bars, args, function (baz, params) {
  30. * self.baz(baz, params);
  31. * });
  32. * });
  33. */
  34. chain: function chain() {
  35. let funcs = Array.slice(arguments);
  36. let thisObj = this;
  37. return function callback() {
  38. if (funcs.length) {
  39. let args = Array.slice(arguments).concat(callback);
  40. let f = funcs.shift();
  41. f.apply(thisObj, args);
  42. }
  43. };
  44. },
  45. /**
  46. * Helpers for making asynchronous calls within a synchronous API possible.
  47. *
  48. * If you value your sanity, do not look closely at the following functions.
  49. */
  50. /**
  51. * Create a sync callback that remembers state, in particular whether it has
  52. * been called.
  53. * The returned callback can be called directly passing an optional arg which
  54. * will be returned by waitForSyncCallback(). The callback also has a
  55. * .throw() method, which takes an error object and will cause
  56. * waitForSyncCallback to fail with the error object thrown as an exception
  57. * (but note that the .throw method *does not* itself throw - it just causes
  58. * the wait function to throw).
  59. */
  60. makeSyncCallback: function makeSyncCallback() {
  61. // The main callback remembers the value it was passed, and that it got data.
  62. let onComplete = function onComplete(data) {
  63. onComplete.state = CB_COMPLETE;
  64. onComplete.value = data;
  65. };
  66. // Initialize private callback data in preparation for being called.
  67. onComplete.state = CB_READY;
  68. onComplete.value = null;
  69. // Allow an alternate callback to trigger an exception to be thrown.
  70. onComplete.throw = function onComplete_throw(data) {
  71. onComplete.state = CB_FAIL;
  72. onComplete.value = data;
  73. };
  74. return onComplete;
  75. },
  76. /**
  77. * Wait for a sync callback to finish.
  78. */
  79. waitForSyncCallback: function waitForSyncCallback(callback) {
  80. // Grab the current thread so we can make it give up priority.
  81. let thread = Cc["@mozilla.org/thread-manager;1"].getService().currentThread;
  82. // Keep waiting until our callback is triggered (unless the app is quitting).
  83. while (Async.checkAppReady() && callback.state == CB_READY) {
  84. thread.processNextEvent(true);
  85. }
  86. // Reset the state of the callback to prepare for another call.
  87. let state = callback.state;
  88. callback.state = CB_READY;
  89. // Throw the value the callback decided to fail with.
  90. if (state == CB_FAIL) {
  91. throw callback.value;
  92. }
  93. // Return the value passed to the callback.
  94. return callback.value;
  95. },
  96. /**
  97. * Check if the app is still ready (not quitting).
  98. */
  99. checkAppReady: function checkAppReady() {
  100. // Watch for app-quit notification to stop any sync calls
  101. Services.obs.addObserver(function onQuitApplication() {
  102. Services.obs.removeObserver(onQuitApplication, "quit-application");
  103. Async.checkAppReady = function() {
  104. let exception = Components.Exception("App. Quitting", Cr.NS_ERROR_ABORT);
  105. exception.appIsShuttingDown = true;
  106. throw exception;
  107. };
  108. }, "quit-application", false);
  109. // In the common case, checkAppReady just returns true
  110. return (Async.checkAppReady = function() { return true; })();
  111. },
  112. /**
  113. * Check if the passed exception is one raised by checkAppReady. Typically
  114. * this will be used in exception handlers to allow such exceptions to
  115. * make their way to the top frame and allow the app to actually terminate.
  116. */
  117. isShutdownException(exception) {
  118. return exception && exception.appIsShuttingDown === true;
  119. },
  120. /**
  121. * Return the two things you need to make an asynchronous call synchronous
  122. * by spinning the event loop.
  123. */
  124. makeSpinningCallback: function makeSpinningCallback() {
  125. let cb = Async.makeSyncCallback();
  126. function callback(error, ret) {
  127. if (error)
  128. cb.throw(error);
  129. else
  130. cb(ret);
  131. }
  132. callback.wait = () => Async.waitForSyncCallback(cb);
  133. return callback;
  134. },
  135. // Prototype for mozIStorageCallback, used in querySpinningly.
  136. // This allows us to define the handle* functions just once rather
  137. // than on every querySpinningly invocation.
  138. _storageCallbackPrototype: {
  139. results: null,
  140. // These are set by queryAsync.
  141. names: null,
  142. syncCb: null,
  143. handleResult: function handleResult(results) {
  144. if (!this.names) {
  145. return;
  146. }
  147. if (!this.results) {
  148. this.results = [];
  149. }
  150. let row;
  151. while ((row = results.getNextRow()) != null) {
  152. let item = {};
  153. for (let name of this.names) {
  154. item[name] = row.getResultByName(name);
  155. }
  156. this.results.push(item);
  157. }
  158. },
  159. handleError: function handleError(error) {
  160. this.syncCb.throw(error);
  161. },
  162. handleCompletion: function handleCompletion(reason) {
  163. // If we got an error, handleError will also have been called, so don't
  164. // call the callback! We never cancel statements, so we don't need to
  165. // address that quandary.
  166. if (reason == REASON_ERROR)
  167. return;
  168. // If we were called with column names but didn't find any results,
  169. // the calling code probably still expects an array as a return value.
  170. if (this.names && !this.results) {
  171. this.results = [];
  172. }
  173. this.syncCb(this.results);
  174. }
  175. },
  176. querySpinningly: function querySpinningly(query, names) {
  177. // 'Synchronously' asyncExecute, fetching all results by name.
  178. let storageCallback = Object.create(Async._storageCallbackPrototype);
  179. storageCallback.names = names;
  180. storageCallback.syncCb = Async.makeSyncCallback();
  181. query.executeAsync(storageCallback);
  182. return Async.waitForSyncCallback(storageCallback.syncCb);
  183. },
  184. promiseSpinningly(promise) {
  185. let cb = Async.makeSpinningCallback();
  186. promise.then(result => {
  187. cb(null, result);
  188. }, err => {
  189. cb(err || new Error("Promise rejected without explicit error"));
  190. });
  191. return cb.wait();
  192. },
  193. };