Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/polyfill/promise.js

Issue 29333514: Issue 3515 - Added polyfill for ES6 promises (Closed)
Patch Set: Created Jan. 14, 2016, 5:19 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | metadata.common » ('j') | metadata.common » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/polyfill/promise.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/lib/polyfill/promise.js
@@ -0,0 +1,146 @@
+/*
+ * This file is part of Adblock Plus <https://adblockplus.org/>,
+ * Copyright (C) 2006-2015 Eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+function(global)
+{
+ if ("Promise" in global)
+ return;
+
+ var PENDING = 0;
kzar 2016/01/15 19:00:41 Nit: const?
Sebastian Noack 2016/01/19 11:34:17 const isn't supported in strict mode before Chrome
kzar 2016/01/19 14:49:27 Acknowledged.
+ var FULFILLED = 1;
+ var REJECTED = 2;
+
+ global.Promise = function(executor)
+ {
+ this._state = PENDING;
+ this._value = undefined;
+ this._subscriptions = [];
+
+ try
+ {
+ executor(this._emit.bind(this, FULFILLED),
+ this._emit.bind(this, REJECTED));
+ }
+ catch (reason)
+ {
+ this._emit(REJECTED, reason);
+ }
+ };
+
+ Promise.prototype = {
kzar 2016/01/15 19:00:41 Should be `global.Promise.prototype = {`?
Sebastian Noack 2016/01/19 11:34:17 As globals literally refers to the global scope, P
kzar 2016/01/19 14:49:27 Acknowledged.
+ _dispatch: function(onFulfilled, onRejected, resolve, reject)
+ {
+ var callback = this._state == FULFILLED ? onFulfilled : onRejected;
+
+ if (typeof callback == "function")
+ {
+ var result;
+
+ try
+ {
+ result = callback(this._value);
+ }
+ catch (reason)
+ {
+ reject(reason);
+ return;
+ }
+
+ Promise.resolve(result).then(resolve, reject);
+ }
+ else if (this._state == FULFILLED)
+ {
+ resolve(this._value);
+ }
+ else if (this._state == REJECTED)
+ {
+ reject(this._value);
+ }
+ },
+ _dispatchSubscriptions: function()
+ {
+ if (this._state == REJECTED && this._subscriptions.length == 0)
kzar 2016/01/15 19:00:41 What if there are subscriptions but only for resol
Sebastian Noack 2016/01/19 11:34:17 No, in that case the error will be delegated to th
kzar 2016/01/19 14:49:27 Acknowledged.
+ console.error('Uncaught (in promise)', this._value);
+
+ for (var i = 0; i < this._subscriptions.length; i++)
+ this._dispatch.apply(this, this._subscriptions[i]);
+
+ this._subscriptions = null;
+ },
+ _emit: function(state, value)
+ {
+ if (this._state != PENDING)
+ return;
+
+ this._state = state;
+ this._value = value;
+
+ setTimeout(this._dispatchSubscriptions.bind(this), 0);
+ },
+ then: function(onFulfilled, onRejected)
+ {
+ return new Promise(function(resolve, reject)
+ {
+ if (this._subscriptions)
+ this._subscriptions.push([onFulfilled, onRejected, resolve, reject]);
+ else
+ setTimeout(
kzar 2016/01/15 19:00:41 How come you're using setTimeout here and above?
Sebastian Noack 2016/01/19 11:34:18 The callbacks are supposed to be called asynchrono
kzar 2016/01/19 14:49:27 Acknowledged.
+ this._dispatch.bind(this), 0,
+ onFulfilled, onRejected, resolve, reject
+ );
+ }.bind(this));
+ },
+ catch: function(onRejected)
+ {
+ return this.then(undefined, onRejected);
+ }
+ };
+
kzar 2016/01/15 19:00:41 What about Promise.race?
Sebastian Noack 2016/01/19 11:34:17 I rather don't introduce dead code and unnecessary
kzar 2016/01/19 14:49:27 Acknowledged.
+ Promise.resolve = function(value)
+ {
+ if (value instanceof Promise)
kzar 2016/01/15 19:00:41 The docs say that we should use the value straight
Sebastian Noack 2016/01/19 11:34:22 True, however this behavior isn't implemented in V
kzar 2016/01/19 14:49:27 Acknowledged.
+ return value;
+ return new Promise(function(resolve, reject) { resolve(value); });
+ };
+
+ Promise.reject = function(reason)
+ {
+ return new Promise(function(resolve, reject) { reject(reason); });
+ };
+
+ Promise.all = function(promises)
+ {
+ return new Promise(function(resolve, reject)
+ {
+ var count = promises.length;
kzar 2016/01/15 19:00:41 Nit: Call this `remaining` instead of `count`?
Sebastian Noack 2016/01/19 11:34:18 Since this variable is used as well to set the siz
+ var result = new Array(count);
+
+ promises.forEach(function(promise, i)
+ {
+ promise.then(
+ function(value)
+ {
+ result[i] = value;
+ if (--count == 0)
+ resolve(result);
+ },
+ reject
+ );
+ });
+ });
+ };
+}(this);
« no previous file with comments | « no previous file | metadata.common » ('j') | metadata.common » ('J')

Powered by Google App Engine
This is Rietveld