[Pkg-mozext-commits] [tabmixplus] 22/61: New module AsyncUtils.jsm to help use callbacks as promises
David Prévot
taffit at moszumanska.debian.org
Fri Aug 28 19:09:18 UTC 2015
This is an automated email from the git hooks/post-receive script.
taffit pushed a commit to branch master
in repository tabmixplus.
commit bbaab28ada0b7e9707ae6f0026fc991af4360163
Author: onemen <tabmix.onemen at gmail.com>
Date: Tue Jul 21 21:13:47 2015 +0300
New module AsyncUtils.jsm to help use callbacks as promises
---
modules/AsyncUtils.jsm | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 59 insertions(+)
diff --git a/modules/AsyncUtils.jsm b/modules/AsyncUtils.jsm
new file mode 100644
index 0000000..30b1a52
--- /dev/null
+++ b/modules/AsyncUtils.jsm
@@ -0,0 +1,59 @@
+/* jshint esnext: true */
+"use strict";
+
+var EXPORTED_SYMBOLS = ["AsyncUtils"];
+
+const Cu = Components.utils;
+
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+
+XPCOMUtils.defineLazyModuleGetter(this, "Promise",
+ "resource://gre/modules/Promise.jsm");
+
+XPCOMUtils.defineLazyModuleGetter(this, "TabmixSvc",
+ "resource://tabmixplus/Services.jsm");
+this.AsyncUtils = {
+ /* PromiseUtils.defer exsit since Firefox 39 */
+ defer : function() {
+ return new Deferred();
+ },
+
+ spawnFn: function(thisArg, fn, index) {
+ return this.promisify(thisArg, fn, index).call(undefined);
+ },
+
+ asyncFn: function(thisArg, fn, index) {
+ return this.promisify(thisArg, fn, index);
+ },
+
+ promisify: function(thisArg, fn, index) {
+ return function() {
+ let deferred = new Deferred();
+ let args = Array.slice(arguments);
+ if (typeof index == "undefined")
+ index = args.length;
+ args.splice(index, 0, deferred.callback);
+
+ try {
+ fn.apply(thisArg, args);
+ } catch(ex) {
+ deferred.reject(ex);
+ }
+ return deferred.promise;
+ };
+ }
+};
+
+function Deferred() {
+ this.resolve = null;
+ this.reject = null;
+ this.callback = (result, error) => {
+ if (error)
+ return this.reject(error);
+ return this.resolve(result);
+ };
+ this.promise = new Promise((resolve, reject) => {
+ this.resolve = resolve;
+ this.reject = reject;
+ });
+}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/tabmixplus.git
More information about the Pkg-mozext-commits
mailing list