es6_promise_polyfill_rails 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8e625b83fd4805120089c9832bef53ae8cb10295
4
- data.tar.gz: eb0a00f7a1c875758f43d13a0ba6c0e91d84b097
3
+ metadata.gz: f76ff4726e4ad1058b3bdf01bcb7ae0c452158a4
4
+ data.tar.gz: 6766e65ba5dc070cf561a692eec6074c6684e5cc
5
5
  SHA512:
6
- metadata.gz: ad4677ea51521d2e6d9b544368981fffc5535bed892c2c5d34fd3b13406887380d20d25210134f73737236fa7de0ecaa71fc986daa58b415ba15b9ccf352e72e
7
- data.tar.gz: 0de03d428c4a58771b4e50a4d4348ca4b85bb523b98aa8b0ceff360917380940068b119376319463c719c2acb10093fc04af67f83a73318ed9b950bd6568a500
6
+ metadata.gz: f6a80aff08788a481417c5d584f492b01ceb297c7788a6d02e12a2a37f071f072aecdce8fde1329226bb19189d6d73af7b5a6932efb1a4e8ff27d80369983d1b
7
+ data.tar.gz: 4940a27d4b54f903c0b6bd239e0979432c098d460d2cae1ba1cd04de31b3fd51a5165da8aca0a305aeececd06b721e1d7ca1f2cc9886f596cf020bece8b76ac8
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Es6PromisePolyfillRails - Es6 Promise Polyfill Rails Javascript packaged for the Rails asset pipeline
2
2
 
3
- An es6 promise polyfill js gem packaged for the rails asset pipeline. The js was created by the awesome developer, [Taylor Hakes](https://github.com/taylorhakes). You can take a look at the original repo hosted here [https://github.com/taylorhakes/promise-polyfill](https://github.com/taylorhakes/promise-polyfill).
3
+ An es6 promise polyfill js gem packaged for the rails asset pipeline. The js was created by the awesome developer, [Stefan Penner](https://github.com/stefanpenner). You can take a look at the original repo hosted here [https://github.com/taylorhakes/promise-polyfill](https://github.com/stefanpenner/es6-promise).
4
4
 
5
5
  ## Installation
6
6
 
@@ -24,7 +24,7 @@ Add it to your Javascript manifest file
24
24
 
25
25
  ## Usage
26
26
 
27
- Check out this repo [https://github.com/taylorhakes/promise-polyfill](https://github.com/taylorhakes/promise-polyfill).
27
+ Check out this repo [https://github.com/stefanpenner/es6-promise](https://github.com/stefanpenner/es6-promise).
28
28
 
29
29
  ## Development
30
30
 
@@ -1,231 +1,903 @@
1
1
  if(typeof Promise != 'function'){
2
- (function (root) {
3
- // Store setTimeout reference so promise-polyfill will be unaffected by
4
- // other code modifying setTimeout (like sinon.useFakeTimers())
5
- var setTimeoutFunc = setTimeout;
6
-
7
- function noop() {
8
- }
9
-
10
- // Use polyfill for setImmediate for performance gains
11
- var asap = (typeof setImmediate === 'function' && setImmediate) ||
12
- function (fn) {
13
- setTimeoutFunc(fn, 1);
14
- };
2
+ (function() {
3
+ "use strict";
4
+ function lib$es6$promise$utils$$objectOrFunction(x) {
5
+ return typeof x === 'function' || (typeof x === 'object' && x !== null);
6
+ }
15
7
 
16
- var onUnhandledRejection = function onUnhandledRejection(err) {
17
- console.warn('Possible Unhandled Promise Rejection:', err); // eslint-disable-line no-console
18
- };
8
+ function lib$es6$promise$utils$$isFunction(x) {
9
+ return typeof x === 'function';
10
+ }
19
11
 
20
- // Polyfill for Function.prototype.bind
21
- function bind(fn, thisArg) {
22
- return function () {
23
- fn.apply(thisArg, arguments);
24
- };
25
- }
26
-
27
- var isArray = Array.isArray || function (value) {
28
- return Object.prototype.toString.call(value) === '[object Array]';
29
- };
30
-
31
- function Promise(fn) {
32
- if (typeof this !== 'object') throw new TypeError('Promises must be constructed via new');
33
- if (typeof fn !== 'function') throw new TypeError('not a function');
34
- this._state = 0;
35
- this._handled = false;
36
- this._value = undefined;
37
- this._deferreds = [];
38
-
39
- doResolve(fn, this);
40
- }
41
-
42
- function handle(self, deferred) {
43
- while (self._state === 3) {
44
- self = self._value;
45
- }
46
- if (self._state === 0) {
47
- self._deferreds.push(deferred);
48
- return;
49
- }
50
- self._handled = true;
51
- asap(function () {
52
- var cb = self._state === 1 ? deferred.onFulfilled : deferred.onRejected;
53
- if (cb === null) {
54
- (self._state === 1 ? resolve : reject)(deferred.promise, self._value);
55
- return;
12
+ function lib$es6$promise$utils$$isMaybeThenable(x) {
13
+ return typeof x === 'object' && x !== null;
14
+ }
15
+
16
+ var lib$es6$promise$utils$$_isArray;
17
+ if (!Array.isArray) {
18
+ lib$es6$promise$utils$$_isArray = function (x) {
19
+ return Object.prototype.toString.call(x) === '[object Array]';
20
+ };
21
+ } else {
22
+ lib$es6$promise$utils$$_isArray = Array.isArray;
23
+ }
24
+
25
+ var lib$es6$promise$utils$$isArray = lib$es6$promise$utils$$_isArray;
26
+ var lib$es6$promise$asap$$len = 0;
27
+ var lib$es6$promise$asap$$toString = {}.toString;
28
+ var lib$es6$promise$asap$$vertxNext;
29
+ var lib$es6$promise$asap$$customSchedulerFn;
30
+
31
+ var lib$es6$promise$asap$$asap = function asap(callback, arg) {
32
+ lib$es6$promise$asap$$queue[lib$es6$promise$asap$$len] = callback;
33
+ lib$es6$promise$asap$$queue[lib$es6$promise$asap$$len + 1] = arg;
34
+ lib$es6$promise$asap$$len += 2;
35
+ if (lib$es6$promise$asap$$len === 2) {
36
+ // If len is 2, that means that we need to schedule an async flush.
37
+ // If additional callbacks are queued before the queue is flushed, they
38
+ // will be processed by this flush that we are scheduling.
39
+ if (lib$es6$promise$asap$$customSchedulerFn) {
40
+ lib$es6$promise$asap$$customSchedulerFn(lib$es6$promise$asap$$flush);
41
+ } else {
42
+ lib$es6$promise$asap$$scheduleFlush();
43
+ }
44
+ }
45
+ }
46
+
47
+ function lib$es6$promise$asap$$setScheduler(scheduleFn) {
48
+ lib$es6$promise$asap$$customSchedulerFn = scheduleFn;
49
+ }
50
+
51
+ function lib$es6$promise$asap$$setAsap(asapFn) {
52
+ lib$es6$promise$asap$$asap = asapFn;
53
+ }
54
+
55
+ var lib$es6$promise$asap$$browserWindow = (typeof window !== 'undefined') ? window : undefined;
56
+ var lib$es6$promise$asap$$browserGlobal = lib$es6$promise$asap$$browserWindow || {};
57
+ var lib$es6$promise$asap$$BrowserMutationObserver = lib$es6$promise$asap$$browserGlobal.MutationObserver || lib$es6$promise$asap$$browserGlobal.WebKitMutationObserver;
58
+ var lib$es6$promise$asap$$isNode = typeof process !== 'undefined' && {}.toString.call(process) === '[object process]';
59
+
60
+ // test for web worker but not in IE10
61
+ var lib$es6$promise$asap$$isWorker = typeof Uint8ClampedArray !== 'undefined' &&
62
+ typeof importScripts !== 'undefined' &&
63
+ typeof MessageChannel !== 'undefined';
64
+
65
+ // node
66
+ function lib$es6$promise$asap$$useNextTick() {
67
+ // node version 0.10.x displays a deprecation warning when nextTick is used recursively
68
+ // see https://github.com/cujojs/when/issues/410 for details
69
+ return function() {
70
+ process.nextTick(lib$es6$promise$asap$$flush);
71
+ };
72
+ }
73
+
74
+ // vertx
75
+ function lib$es6$promise$asap$$useVertxTimer() {
76
+ return function() {
77
+ lib$es6$promise$asap$$vertxNext(lib$es6$promise$asap$$flush);
78
+ };
79
+ }
80
+
81
+ function lib$es6$promise$asap$$useMutationObserver() {
82
+ var iterations = 0;
83
+ var observer = new lib$es6$promise$asap$$BrowserMutationObserver(lib$es6$promise$asap$$flush);
84
+ var node = document.createTextNode('');
85
+ observer.observe(node, { characterData: true });
86
+
87
+ return function() {
88
+ node.data = (iterations = ++iterations % 2);
89
+ };
90
+ }
91
+
92
+ // web worker
93
+ function lib$es6$promise$asap$$useMessageChannel() {
94
+ var channel = new MessageChannel();
95
+ channel.port1.onmessage = lib$es6$promise$asap$$flush;
96
+ return function () {
97
+ channel.port2.postMessage(0);
98
+ };
99
+ }
100
+
101
+ function lib$es6$promise$asap$$useSetTimeout() {
102
+ return function() {
103
+ setTimeout(lib$es6$promise$asap$$flush, 1);
104
+ };
105
+ }
106
+
107
+ var lib$es6$promise$asap$$queue = new Array(1000);
108
+ function lib$es6$promise$asap$$flush() {
109
+ for (var i = 0; i < lib$es6$promise$asap$$len; i+=2) {
110
+ var callback = lib$es6$promise$asap$$queue[i];
111
+ var arg = lib$es6$promise$asap$$queue[i+1];
112
+
113
+ callback(arg);
114
+
115
+ lib$es6$promise$asap$$queue[i] = undefined;
116
+ lib$es6$promise$asap$$queue[i+1] = undefined;
56
117
  }
57
- var ret;
118
+
119
+ lib$es6$promise$asap$$len = 0;
120
+ }
121
+
122
+ function lib$es6$promise$asap$$attemptVertx() {
58
123
  try {
59
- ret = cb(self._value);
60
- } catch (e) {
61
- reject(deferred.promise, e);
62
- return;
124
+ var r = require;
125
+ var vertx = r('vertx');
126
+ lib$es6$promise$asap$$vertxNext = vertx.runOnLoop || vertx.runOnContext;
127
+ return lib$es6$promise$asap$$useVertxTimer();
128
+ } catch(e) {
129
+ return lib$es6$promise$asap$$useSetTimeout();
63
130
  }
64
- resolve(deferred.promise, ret);
65
- });
66
- }
67
-
68
- function resolve(self, newValue) {
69
- try {
70
- // Promise Resolution Procedure: https://github.com/promises-aplus/promises-spec#the-promise-resolution-procedure
71
- if (newValue === self) throw new TypeError('A promise cannot be resolved with itself.');
72
- if (newValue && (typeof newValue === 'object' || typeof newValue === 'function')) {
73
- var then = newValue.then;
74
- if (newValue instanceof Promise) {
75
- self._state = 3;
76
- self._value = newValue;
77
- finale(self);
78
- return;
79
- } else if (typeof then === 'function') {
80
- doResolve(bind(then, newValue), self);
81
- return;
131
+ }
132
+
133
+ var lib$es6$promise$asap$$scheduleFlush;
134
+ // Decide what async method to use to triggering processing of queued callbacks:
135
+ if (lib$es6$promise$asap$$isNode) {
136
+ lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$useNextTick();
137
+ } else if (lib$es6$promise$asap$$BrowserMutationObserver) {
138
+ lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$useMutationObserver();
139
+ } else if (lib$es6$promise$asap$$isWorker) {
140
+ lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$useMessageChannel();
141
+ } else if (lib$es6$promise$asap$$browserWindow === undefined && typeof require === 'function') {
142
+ lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$attemptVertx();
143
+ } else {
144
+ lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$useSetTimeout();
145
+ }
146
+
147
+ function lib$es6$promise$$internal$$noop() {}
148
+
149
+ var lib$es6$promise$$internal$$PENDING = void 0;
150
+ var lib$es6$promise$$internal$$FULFILLED = 1;
151
+ var lib$es6$promise$$internal$$REJECTED = 2;
152
+
153
+ var lib$es6$promise$$internal$$GET_THEN_ERROR = new lib$es6$promise$$internal$$ErrorObject();
154
+
155
+ function lib$es6$promise$$internal$$selfFulfillment() {
156
+ return new TypeError("You cannot resolve a promise with itself");
157
+ }
158
+
159
+ function lib$es6$promise$$internal$$cannotReturnOwn() {
160
+ return new TypeError('A promises callback cannot return that same promise.');
161
+ }
162
+
163
+ function lib$es6$promise$$internal$$getThen(promise) {
164
+ try {
165
+ return promise.then;
166
+ } catch(error) {
167
+ lib$es6$promise$$internal$$GET_THEN_ERROR.error = error;
168
+ return lib$es6$promise$$internal$$GET_THEN_ERROR;
169
+ }
170
+ }
171
+
172
+ function lib$es6$promise$$internal$$tryThen(then, value, fulfillmentHandler, rejectionHandler) {
173
+ try {
174
+ then.call(value, fulfillmentHandler, rejectionHandler);
175
+ } catch(e) {
176
+ return e;
177
+ }
178
+ }
179
+
180
+ function lib$es6$promise$$internal$$handleForeignThenable(promise, thenable, then) {
181
+ lib$es6$promise$asap$$asap(function(promise) {
182
+ var sealed = false;
183
+ var error = lib$es6$promise$$internal$$tryThen(then, thenable, function(value) {
184
+ if (sealed) { return; }
185
+ sealed = true;
186
+ if (thenable !== value) {
187
+ lib$es6$promise$$internal$$resolve(promise, value);
188
+ } else {
189
+ lib$es6$promise$$internal$$fulfill(promise, value);
190
+ }
191
+ }, function(reason) {
192
+ if (sealed) { return; }
193
+ sealed = true;
194
+
195
+ lib$es6$promise$$internal$$reject(promise, reason);
196
+ }, 'Settle: ' + (promise._label || ' unknown promise'));
197
+
198
+ if (!sealed && error) {
199
+ sealed = true;
200
+ lib$es6$promise$$internal$$reject(promise, error);
82
201
  }
202
+ }, promise);
203
+ }
204
+
205
+ function lib$es6$promise$$internal$$handleOwnThenable(promise, thenable) {
206
+ if (thenable._state === lib$es6$promise$$internal$$FULFILLED) {
207
+ lib$es6$promise$$internal$$fulfill(promise, thenable._result);
208
+ } else if (thenable._state === lib$es6$promise$$internal$$REJECTED) {
209
+ lib$es6$promise$$internal$$reject(promise, thenable._result);
210
+ } else {
211
+ lib$es6$promise$$internal$$subscribe(thenable, undefined, function(value) {
212
+ lib$es6$promise$$internal$$resolve(promise, value);
213
+ }, function(reason) {
214
+ lib$es6$promise$$internal$$reject(promise, reason);
215
+ });
83
216
  }
84
- self._state = 1;
85
- self._value = newValue;
86
- finale(self);
87
- } catch (e) {
88
- reject(self, e);
89
- }
90
- }
91
-
92
- function reject(self, newValue) {
93
- self._state = 2;
94
- self._value = newValue;
95
- finale(self);
96
- }
97
-
98
- function finale(self) {
99
- if (self._state === 2 && self._deferreds.length === 0) {
100
- setTimeout(function() {
101
- if (!self._handled) {
102
- onUnhandledRejection(self._value);
217
+ }
218
+
219
+ function lib$es6$promise$$internal$$handleMaybeThenable(promise, maybeThenable) {
220
+ if (maybeThenable.constructor === promise.constructor) {
221
+ lib$es6$promise$$internal$$handleOwnThenable(promise, maybeThenable);
222
+ } else {
223
+ var then = lib$es6$promise$$internal$$getThen(maybeThenable);
224
+
225
+ if (then === lib$es6$promise$$internal$$GET_THEN_ERROR) {
226
+ lib$es6$promise$$internal$$reject(promise, lib$es6$promise$$internal$$GET_THEN_ERROR.error);
227
+ } else if (then === undefined) {
228
+ lib$es6$promise$$internal$$fulfill(promise, maybeThenable);
229
+ } else if (lib$es6$promise$utils$$isFunction(then)) {
230
+ lib$es6$promise$$internal$$handleForeignThenable(promise, maybeThenable, then);
231
+ } else {
232
+ lib$es6$promise$$internal$$fulfill(promise, maybeThenable);
103
233
  }
104
- }, 1);
105
- }
106
-
107
- for (var i = 0, len = self._deferreds.length; i < len; i++) {
108
- handle(self, self._deferreds[i]);
109
- }
110
- self._deferreds = null;
111
- }
112
-
113
- function Handler(onFulfilled, onRejected, promise) {
114
- this.onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : null;
115
- this.onRejected = typeof onRejected === 'function' ? onRejected : null;
116
- this.promise = promise;
117
- }
118
-
119
- /**
120
- * Take a potentially misbehaving resolver function and make sure
121
- * onFulfilled and onRejected are only called once.
122
- *
123
- * Makes no guarantees about asynchrony.
124
- */
125
- function doResolve(fn, self) {
126
- var done = false;
127
- try {
128
- fn(function (value) {
129
- if (done) return;
130
- done = true;
131
- resolve(self, value);
132
- }, function (reason) {
133
- if (done) return;
134
- done = true;
135
- reject(self, reason);
136
- });
137
- } catch (ex) {
138
- if (done) return;
139
- done = true;
140
- reject(self, ex);
141
- }
142
- }
143
-
144
- Promise.prototype['catch'] = function (onRejected) {
145
- return this.then(null, onRejected);
146
- };
147
-
148
- Promise.prototype.then = function (onFulfilled, onRejected) {
149
- var prom = new Promise(noop);
150
- handle(this, new Handler(onFulfilled, onRejected, prom));
151
- return prom;
152
- };
153
-
154
- Promise.all = function () {
155
- var args = Array.prototype.slice.call(arguments.length === 1 && isArray(arguments[0]) ? arguments[0] : arguments);
156
-
157
- return new Promise(function (resolve, reject) {
158
- if (args.length === 0) return resolve([]);
159
- var remaining = args.length;
160
-
161
- function res(i, val) {
162
- try {
163
- if (val && (typeof val === 'object' || typeof val === 'function')) {
164
- var then = val.then;
165
- if (typeof then === 'function') {
166
- then.call(val, function (val) {
167
- res(i, val);
168
- }, reject);
169
- return;
170
- }
171
- }
172
- args[i] = val;
173
- if (--remaining === 0) {
174
- resolve(args);
234
+ }
235
+ }
236
+
237
+ function lib$es6$promise$$internal$$resolve(promise, value) {
238
+ if (promise === value) {
239
+ lib$es6$promise$$internal$$reject(promise, lib$es6$promise$$internal$$selfFulfillment());
240
+ } else if (lib$es6$promise$utils$$objectOrFunction(value)) {
241
+ lib$es6$promise$$internal$$handleMaybeThenable(promise, value);
242
+ } else {
243
+ lib$es6$promise$$internal$$fulfill(promise, value);
244
+ }
245
+ }
246
+
247
+ function lib$es6$promise$$internal$$publishRejection(promise) {
248
+ if (promise._onerror) {
249
+ promise._onerror(promise._result);
250
+ }
251
+
252
+ lib$es6$promise$$internal$$publish(promise);
253
+ }
254
+
255
+ function lib$es6$promise$$internal$$fulfill(promise, value) {
256
+ if (promise._state !== lib$es6$promise$$internal$$PENDING) { return; }
257
+
258
+ promise._result = value;
259
+ promise._state = lib$es6$promise$$internal$$FULFILLED;
260
+
261
+ if (promise._subscribers.length !== 0) {
262
+ lib$es6$promise$asap$$asap(lib$es6$promise$$internal$$publish, promise);
263
+ }
264
+ }
265
+
266
+ function lib$es6$promise$$internal$$reject(promise, reason) {
267
+ if (promise._state !== lib$es6$promise$$internal$$PENDING) { return; }
268
+ promise._state = lib$es6$promise$$internal$$REJECTED;
269
+ promise._result = reason;
270
+
271
+ lib$es6$promise$asap$$asap(lib$es6$promise$$internal$$publishRejection, promise);
272
+ }
273
+
274
+ function lib$es6$promise$$internal$$subscribe(parent, child, onFulfillment, onRejection) {
275
+ var subscribers = parent._subscribers;
276
+ var length = subscribers.length;
277
+
278
+ parent._onerror = null;
279
+
280
+ subscribers[length] = child;
281
+ subscribers[length + lib$es6$promise$$internal$$FULFILLED] = onFulfillment;
282
+ subscribers[length + lib$es6$promise$$internal$$REJECTED] = onRejection;
283
+
284
+ if (length === 0 && parent._state) {
285
+ lib$es6$promise$asap$$asap(lib$es6$promise$$internal$$publish, parent);
286
+ }
287
+ }
288
+
289
+ function lib$es6$promise$$internal$$publish(promise) {
290
+ var subscribers = promise._subscribers;
291
+ var settled = promise._state;
292
+
293
+ if (subscribers.length === 0) { return; }
294
+
295
+ var child, callback, detail = promise._result;
296
+
297
+ for (var i = 0; i < subscribers.length; i += 3) {
298
+ child = subscribers[i];
299
+ callback = subscribers[i + settled];
300
+
301
+ if (child) {
302
+ lib$es6$promise$$internal$$invokeCallback(settled, child, callback, detail);
303
+ } else {
304
+ callback(detail);
305
+ }
306
+ }
307
+
308
+ promise._subscribers.length = 0;
309
+ }
310
+
311
+ function lib$es6$promise$$internal$$ErrorObject() {
312
+ this.error = null;
313
+ }
314
+
315
+ var lib$es6$promise$$internal$$TRY_CATCH_ERROR = new lib$es6$promise$$internal$$ErrorObject();
316
+
317
+ function lib$es6$promise$$internal$$tryCatch(callback, detail) {
318
+ try {
319
+ return callback(detail);
320
+ } catch(e) {
321
+ lib$es6$promise$$internal$$TRY_CATCH_ERROR.error = e;
322
+ return lib$es6$promise$$internal$$TRY_CATCH_ERROR;
323
+ }
324
+ }
325
+
326
+ function lib$es6$promise$$internal$$invokeCallback(settled, promise, callback, detail) {
327
+ var hasCallback = lib$es6$promise$utils$$isFunction(callback),
328
+ value, error, succeeded, failed;
329
+
330
+ if (hasCallback) {
331
+ value = lib$es6$promise$$internal$$tryCatch(callback, detail);
332
+
333
+ if (value === lib$es6$promise$$internal$$TRY_CATCH_ERROR) {
334
+ failed = true;
335
+ error = value.error;
336
+ value = null;
337
+ } else {
338
+ succeeded = true;
339
+ }
340
+
341
+ if (promise === value) {
342
+ lib$es6$promise$$internal$$reject(promise, lib$es6$promise$$internal$$cannotReturnOwn());
343
+ return;
344
+ }
345
+
346
+ } else {
347
+ value = detail;
348
+ succeeded = true;
349
+ }
350
+
351
+ if (promise._state !== lib$es6$promise$$internal$$PENDING) {
352
+ // noop
353
+ } else if (hasCallback && succeeded) {
354
+ lib$es6$promise$$internal$$resolve(promise, value);
355
+ } else if (failed) {
356
+ lib$es6$promise$$internal$$reject(promise, error);
357
+ } else if (settled === lib$es6$promise$$internal$$FULFILLED) {
358
+ lib$es6$promise$$internal$$fulfill(promise, value);
359
+ } else if (settled === lib$es6$promise$$internal$$REJECTED) {
360
+ lib$es6$promise$$internal$$reject(promise, value);
361
+ }
362
+ }
363
+
364
+ function lib$es6$promise$$internal$$initializePromise(promise, resolver) {
365
+ try {
366
+ resolver(function resolvePromise(value){
367
+ lib$es6$promise$$internal$$resolve(promise, value);
368
+ }, function rejectPromise(reason) {
369
+ lib$es6$promise$$internal$$reject(promise, reason);
370
+ });
371
+ } catch(e) {
372
+ lib$es6$promise$$internal$$reject(promise, e);
373
+ }
374
+ }
375
+
376
+ function lib$es6$promise$enumerator$$Enumerator(Constructor, input) {
377
+ var enumerator = this;
378
+
379
+ enumerator._instanceConstructor = Constructor;
380
+ enumerator.promise = new Constructor(lib$es6$promise$$internal$$noop);
381
+
382
+ if (enumerator._validateInput(input)) {
383
+ enumerator._input = input;
384
+ enumerator.length = input.length;
385
+ enumerator._remaining = input.length;
386
+
387
+ enumerator._init();
388
+
389
+ if (enumerator.length === 0) {
390
+ lib$es6$promise$$internal$$fulfill(enumerator.promise, enumerator._result);
391
+ } else {
392
+ enumerator.length = enumerator.length || 0;
393
+ enumerator._enumerate();
394
+ if (enumerator._remaining === 0) {
395
+ lib$es6$promise$$internal$$fulfill(enumerator.promise, enumerator._result);
175
396
  }
176
- } catch (ex) {
177
- reject(ex);
397
+ }
398
+ } else {
399
+ lib$es6$promise$$internal$$reject(enumerator.promise, enumerator._validationError());
400
+ }
401
+ }
402
+
403
+ lib$es6$promise$enumerator$$Enumerator.prototype._validateInput = function(input) {
404
+ return lib$es6$promise$utils$$isArray(input);
405
+ };
406
+
407
+ lib$es6$promise$enumerator$$Enumerator.prototype._validationError = function() {
408
+ return new Error('Array Methods must be provided an Array');
409
+ };
410
+
411
+ lib$es6$promise$enumerator$$Enumerator.prototype._init = function() {
412
+ this._result = new Array(this.length);
413
+ };
414
+
415
+ var lib$es6$promise$enumerator$$default = lib$es6$promise$enumerator$$Enumerator;
416
+
417
+ lib$es6$promise$enumerator$$Enumerator.prototype._enumerate = function() {
418
+ var enumerator = this;
419
+
420
+ var length = enumerator.length;
421
+ var promise = enumerator.promise;
422
+ var input = enumerator._input;
423
+
424
+ for (var i = 0; promise._state === lib$es6$promise$$internal$$PENDING && i < length; i++) {
425
+ enumerator._eachEntry(input[i], i);
426
+ }
427
+ };
428
+
429
+ lib$es6$promise$enumerator$$Enumerator.prototype._eachEntry = function(entry, i) {
430
+ var enumerator = this;
431
+ var c = enumerator._instanceConstructor;
432
+
433
+ if (lib$es6$promise$utils$$isMaybeThenable(entry)) {
434
+ if (entry.constructor === c && entry._state !== lib$es6$promise$$internal$$PENDING) {
435
+ entry._onerror = null;
436
+ enumerator._settledAt(entry._state, i, entry._result);
437
+ } else {
438
+ enumerator._willSettleAt(c.resolve(entry), i);
439
+ }
440
+ } else {
441
+ enumerator._remaining--;
442
+ enumerator._result[i] = entry;
443
+ }
444
+ };
445
+
446
+ lib$es6$promise$enumerator$$Enumerator.prototype._settledAt = function(state, i, value) {
447
+ var enumerator = this;
448
+ var promise = enumerator.promise;
449
+
450
+ if (promise._state === lib$es6$promise$$internal$$PENDING) {
451
+ enumerator._remaining--;
452
+
453
+ if (state === lib$es6$promise$$internal$$REJECTED) {
454
+ lib$es6$promise$$internal$$reject(promise, value);
455
+ } else {
456
+ enumerator._result[i] = value;
178
457
  }
179
458
  }
180
459
 
181
- for (var i = 0; i < args.length; i++) {
182
- res(i, args[i]);
460
+ if (enumerator._remaining === 0) {
461
+ lib$es6$promise$$internal$$fulfill(promise, enumerator._result);
183
462
  }
184
- });
185
- };
463
+ };
186
464
 
187
- Promise.resolve = function (value) {
188
- if (value && typeof value === 'object' && value.constructor === Promise) {
189
- return value;
465
+ lib$es6$promise$enumerator$$Enumerator.prototype._willSettleAt = function(promise, i) {
466
+ var enumerator = this;
467
+
468
+ lib$es6$promise$$internal$$subscribe(promise, undefined, function(value) {
469
+ enumerator._settledAt(lib$es6$promise$$internal$$FULFILLED, i, value);
470
+ }, function(reason) {
471
+ enumerator._settledAt(lib$es6$promise$$internal$$REJECTED, i, reason);
472
+ });
473
+ };
474
+ function lib$es6$promise$promise$all$$all(entries) {
475
+ return new lib$es6$promise$enumerator$$default(this, entries).promise;
190
476
  }
477
+ var lib$es6$promise$promise$all$$default = lib$es6$promise$promise$all$$all;
478
+ function lib$es6$promise$promise$race$$race(entries) {
479
+ /*jshint validthis:true */
480
+ var Constructor = this;
481
+
482
+ var promise = new Constructor(lib$es6$promise$$internal$$noop);
483
+
484
+ if (!lib$es6$promise$utils$$isArray(entries)) {
485
+ lib$es6$promise$$internal$$reject(promise, new TypeError('You must pass an array to race.'));
486
+ return promise;
487
+ }
488
+
489
+ var length = entries.length;
490
+
491
+ function onFulfillment(value) {
492
+ lib$es6$promise$$internal$$resolve(promise, value);
493
+ }
494
+
495
+ function onRejection(reason) {
496
+ lib$es6$promise$$internal$$reject(promise, reason);
497
+ }
191
498
 
192
- return new Promise(function (resolve) {
193
- resolve(value);
194
- });
195
- };
499
+ for (var i = 0; promise._state === lib$es6$promise$$internal$$PENDING && i < length; i++) {
500
+ lib$es6$promise$$internal$$subscribe(Constructor.resolve(entries[i]), undefined, onFulfillment, onRejection);
501
+ }
196
502
 
197
- Promise.reject = function (value) {
198
- return new Promise(function (resolve, reject) {
199
- reject(value);
200
- });
201
- };
503
+ return promise;
504
+ }
505
+ var lib$es6$promise$promise$race$$default = lib$es6$promise$promise$race$$race;
506
+ function lib$es6$promise$promise$resolve$$resolve(object) {
507
+ /*jshint validthis:true */
508
+ var Constructor = this;
202
509
 
203
- Promise.race = function (values) {
204
- return new Promise(function (resolve, reject) {
205
- for (var i = 0, len = values.length; i < len; i++) {
206
- values[i].then(resolve, reject);
510
+ if (object && typeof object === 'object' && object.constructor === Constructor) {
511
+ return object;
207
512
  }
208
- });
209
- };
210
513
 
211
- /**
212
- * Set the immediate function to execute callbacks
213
- * @param fn {function} Function to execute
214
- * @private
215
- */
216
- Promise._setImmediateFn = function _setImmediateFn(fn) {
217
- asap = fn;
218
- };
514
+ var promise = new Constructor(lib$es6$promise$$internal$$noop);
515
+ lib$es6$promise$$internal$$resolve(promise, object);
516
+ return promise;
517
+ }
518
+ var lib$es6$promise$promise$resolve$$default = lib$es6$promise$promise$resolve$$resolve;
519
+ function lib$es6$promise$promise$reject$$reject(reason) {
520
+ /*jshint validthis:true */
521
+ var Constructor = this;
522
+ var promise = new Constructor(lib$es6$promise$$internal$$noop);
523
+ lib$es6$promise$$internal$$reject(promise, reason);
524
+ return promise;
525
+ }
526
+ var lib$es6$promise$promise$reject$$default = lib$es6$promise$promise$reject$$reject;
527
+
528
+ var lib$es6$promise$promise$$counter = 0;
529
+
530
+ function lib$es6$promise$promise$$needsResolver() {
531
+ throw new TypeError('You must pass a resolver function as the first argument to the promise constructor');
532
+ }
219
533
 
220
- Promise._setUnhandledRejectionFn = function _setUnhandledRejectionFn(fn) {
221
- onUnhandledRejection = fn;
222
- };
534
+ function lib$es6$promise$promise$$needsNew() {
535
+ throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.");
536
+ }
537
+
538
+ var lib$es6$promise$promise$$default = lib$es6$promise$promise$$Promise;
539
+ /**
540
+ Promise objects represent the eventual result of an asynchronous operation. The
541
+ primary way of interacting with a promise is through its `then` method, which
542
+ registers callbacks to receive either a promise's eventual value or the reason
543
+ why the promise cannot be fulfilled.
544
+ Terminology
545
+ -----------
546
+ - `promise` is an object or function with a `then` method whose behavior conforms to this specification.
547
+ - `thenable` is an object or function that defines a `then` method.
548
+ - `value` is any legal JavaScript value (including undefined, a thenable, or a promise).
549
+ - `exception` is a value that is thrown using the throw statement.
550
+ - `reason` is a value that indicates why a promise was rejected.
551
+ - `settled` the final resting state of a promise, fulfilled or rejected.
552
+ A promise can be in one of three states: pending, fulfilled, or rejected.
553
+ Promises that are fulfilled have a fulfillment value and are in the fulfilled
554
+ state. Promises that are rejected have a rejection reason and are in the
555
+ rejected state. A fulfillment value is never a thenable.
556
+ Promises can also be said to *resolve* a value. If this value is also a
557
+ promise, then the original promise's settled state will match the value's
558
+ settled state. So a promise that *resolves* a promise that rejects will
559
+ itself reject, and a promise that *resolves* a promise that fulfills will
560
+ itself fulfill.
561
+ Basic Usage:
562
+ ------------
563
+ ```js
564
+ var promise = new Promise(function(resolve, reject) {
565
+ // on success
566
+ resolve(value);
567
+ // on failure
568
+ reject(reason);
569
+ });
570
+ promise.then(function(value) {
571
+ // on fulfillment
572
+ }, function(reason) {
573
+ // on rejection
574
+ });
575
+ ```
576
+ Advanced Usage:
577
+ ---------------
578
+ Promises shine when abstracting away asynchronous interactions such as
579
+ `XMLHttpRequest`s.
580
+ ```js
581
+ function getJSON(url) {
582
+ return new Promise(function(resolve, reject){
583
+ var xhr = new XMLHttpRequest();
584
+ xhr.open('GET', url);
585
+ xhr.onreadystatechange = handler;
586
+ xhr.responseType = 'json';
587
+ xhr.setRequestHeader('Accept', 'application/json');
588
+ xhr.send();
589
+ function handler() {
590
+ if (this.readyState === this.DONE) {
591
+ if (this.status === 200) {
592
+ resolve(this.response);
593
+ } else {
594
+ reject(new Error('getJSON: `' + url + '` failed with status: [' + this.status + ']'));
595
+ }
596
+ }
597
+ };
598
+ });
599
+ }
600
+ getJSON('/posts.json').then(function(json) {
601
+ // on fulfillment
602
+ }, function(reason) {
603
+ // on rejection
604
+ });
605
+ ```
606
+ Unlike callbacks, promises are great composable primitives.
607
+ ```js
608
+ Promise.all([
609
+ getJSON('/posts'),
610
+ getJSON('/comments')
611
+ ]).then(function(values){
612
+ values[0] // => postsJSON
613
+ values[1] // => commentsJSON
614
+ return values;
615
+ });
616
+ ```
617
+ @class Promise
618
+ @param {function} resolver
619
+ Useful for tooling.
620
+ @constructor
621
+ */
622
+ function lib$es6$promise$promise$$Promise(resolver) {
623
+ this._id = lib$es6$promise$promise$$counter++;
624
+ this._state = undefined;
625
+ this._result = undefined;
626
+ this._subscribers = [];
223
627
 
224
- if (typeof module !== 'undefined' && module.exports) {
225
- module.exports = Promise;
226
- } else if (!root.Promise) {
227
- root.Promise = Promise;
228
- }
628
+ if (lib$es6$promise$$internal$$noop !== resolver) {
629
+ if (!lib$es6$promise$utils$$isFunction(resolver)) {
630
+ lib$es6$promise$promise$$needsResolver();
631
+ }
632
+
633
+ if (!(this instanceof lib$es6$promise$promise$$Promise)) {
634
+ lib$es6$promise$promise$$needsNew();
635
+ }
636
+
637
+ lib$es6$promise$$internal$$initializePromise(this, resolver);
638
+ }
639
+ }
640
+
641
+ lib$es6$promise$promise$$Promise.all = lib$es6$promise$promise$all$$default;
642
+ lib$es6$promise$promise$$Promise.race = lib$es6$promise$promise$race$$default;
643
+ lib$es6$promise$promise$$Promise.resolve = lib$es6$promise$promise$resolve$$default;
644
+ lib$es6$promise$promise$$Promise.reject = lib$es6$promise$promise$reject$$default;
645
+ lib$es6$promise$promise$$Promise._setScheduler = lib$es6$promise$asap$$setScheduler;
646
+ lib$es6$promise$promise$$Promise._setAsap = lib$es6$promise$asap$$setAsap;
647
+ lib$es6$promise$promise$$Promise._asap = lib$es6$promise$asap$$asap;
648
+
649
+ lib$es6$promise$promise$$Promise.prototype = {
650
+ constructor: lib$es6$promise$promise$$Promise,
651
+
652
+ /**
653
+ The primary way of interacting with a promise is through its `then` method,
654
+ which registers callbacks to receive either a promise's eventual value or the
655
+ reason why the promise cannot be fulfilled.
656
+ ```js
657
+ findUser().then(function(user){
658
+ // user is available
659
+ }, function(reason){
660
+ // user is unavailable, and you are given the reason why
661
+ });
662
+ ```
663
+ Chaining
664
+ --------
665
+ The return value of `then` is itself a promise. This second, 'downstream'
666
+ promise is resolved with the return value of the first promise's fulfillment
667
+ or rejection handler, or rejected if the handler throws an exception.
668
+ ```js
669
+ findUser().then(function (user) {
670
+ return user.name;
671
+ }, function (reason) {
672
+ return 'default name';
673
+ }).then(function (userName) {
674
+ // If `findUser` fulfilled, `userName` will be the user's name, otherwise it
675
+ // will be `'default name'`
676
+ });
677
+ findUser().then(function (user) {
678
+ throw new Error('Found user, but still unhappy');
679
+ }, function (reason) {
680
+ throw new Error('`findUser` rejected and we're unhappy');
681
+ }).then(function (value) {
682
+ // never reached
683
+ }, function (reason) {
684
+ // if `findUser` fulfilled, `reason` will be 'Found user, but still unhappy'.
685
+ // If `findUser` rejected, `reason` will be '`findUser` rejected and we're unhappy'.
686
+ });
687
+ ```
688
+ If the downstream promise does not specify a rejection handler, rejection reasons will be propagated further downstream.
689
+ ```js
690
+ findUser().then(function (user) {
691
+ throw new PedagogicalException('Upstream error');
692
+ }).then(function (value) {
693
+ // never reached
694
+ }).then(function (value) {
695
+ // never reached
696
+ }, function (reason) {
697
+ // The `PedgagocialException` is propagated all the way down to here
698
+ });
699
+ ```
700
+ Assimilation
701
+ ------------
702
+ Sometimes the value you want to propagate to a downstream promise can only be
703
+ retrieved asynchronously. This can be achieved by returning a promise in the
704
+ fulfillment or rejection handler. The downstream promise will then be pending
705
+ until the returned promise is settled. This is called *assimilation*.
706
+ ```js
707
+ findUser().then(function (user) {
708
+ return findCommentsByAuthor(user);
709
+ }).then(function (comments) {
710
+ // The user's comments are now available
711
+ });
712
+ ```
713
+ If the assimliated promise rejects, then the downstream promise will also reject.
714
+ ```js
715
+ findUser().then(function (user) {
716
+ return findCommentsByAuthor(user);
717
+ }).then(function (comments) {
718
+ // If `findCommentsByAuthor` fulfills, we'll have the value here
719
+ }, function (reason) {
720
+ // If `findCommentsByAuthor` rejects, we'll have the reason here
721
+ });
722
+ ```
723
+ Simple Example
724
+ --------------
725
+ Synchronous Example
726
+ ```javascript
727
+ var result;
728
+ try {
729
+ result = findResult();
730
+ // success
731
+ } catch(reason) {
732
+ // failure
733
+ }
734
+ ```
735
+ Errback Example
736
+ ```js
737
+ findResult(function(result, err){
738
+ if (err) {
739
+ // failure
740
+ } else {
741
+ // success
742
+ }
743
+ });
744
+ ```
745
+ Promise Example;
746
+ ```javascript
747
+ findResult().then(function(result){
748
+ // success
749
+ }, function(reason){
750
+ // failure
751
+ });
752
+ ```
753
+ Advanced Example
754
+ --------------
755
+ Synchronous Example
756
+ ```javascript
757
+ var author, books;
758
+ try {
759
+ author = findAuthor();
760
+ books = findBooksByAuthor(author);
761
+ // success
762
+ } catch(reason) {
763
+ // failure
764
+ }
765
+ ```
766
+ Errback Example
767
+ ```js
768
+ function foundBooks(books) {
769
+ }
770
+ function failure(reason) {
771
+ }
772
+ findAuthor(function(author, err){
773
+ if (err) {
774
+ failure(err);
775
+ // failure
776
+ } else {
777
+ try {
778
+ findBoooksByAuthor(author, function(books, err) {
779
+ if (err) {
780
+ failure(err);
781
+ } else {
782
+ try {
783
+ foundBooks(books);
784
+ } catch(reason) {
785
+ failure(reason);
786
+ }
787
+ }
788
+ });
789
+ } catch(error) {
790
+ failure(err);
791
+ }
792
+ // success
793
+ }
794
+ });
795
+ ```
796
+ Promise Example;
797
+ ```javascript
798
+ findAuthor().
799
+ then(findBooksByAuthor).
800
+ then(function(books){
801
+ // found books
802
+ }).catch(function(reason){
803
+ // something went wrong
804
+ });
805
+ ```
806
+ @method then
807
+ @param {Function} onFulfilled
808
+ @param {Function} onRejected
809
+ Useful for tooling.
810
+ @return {Promise}
811
+ */
812
+ then: function(onFulfillment, onRejection) {
813
+ var parent = this;
814
+ var state = parent._state;
815
+
816
+ if (state === lib$es6$promise$$internal$$FULFILLED && !onFulfillment || state === lib$es6$promise$$internal$$REJECTED && !onRejection) {
817
+ return this;
818
+ }
819
+
820
+ var child = new this.constructor(lib$es6$promise$$internal$$noop);
821
+ var result = parent._result;
822
+
823
+ if (state) {
824
+ var callback = arguments[state - 1];
825
+ lib$es6$promise$asap$$asap(function(){
826
+ lib$es6$promise$$internal$$invokeCallback(state, child, callback, result);
827
+ });
828
+ } else {
829
+ lib$es6$promise$$internal$$subscribe(parent, child, onFulfillment, onRejection);
830
+ }
831
+
832
+ return child;
833
+ },
834
+
835
+ /**
836
+ `catch` is simply sugar for `then(undefined, onRejection)` which makes it the same
837
+ as the catch block of a try/catch statement.
838
+ ```js
839
+ function findAuthor(){
840
+ throw new Error('couldn't find that author');
841
+ }
842
+ // synchronous
843
+ try {
844
+ findAuthor();
845
+ } catch(reason) {
846
+ // something went wrong
847
+ }
848
+ // async with promises
849
+ findAuthor().catch(function(reason){
850
+ // something went wrong
851
+ });
852
+ ```
853
+ @method catch
854
+ @param {Function} onRejection
855
+ Useful for tooling.
856
+ @return {Promise}
857
+ */
858
+ 'catch': function(onRejection) {
859
+ return this.then(null, onRejection);
860
+ }
861
+ };
862
+ function lib$es6$promise$polyfill$$polyfill() {
863
+ var local;
864
+
865
+ if (typeof global !== 'undefined') {
866
+ local = global;
867
+ } else if (typeof self !== 'undefined') {
868
+ local = self;
869
+ } else {
870
+ try {
871
+ local = Function('return this')();
872
+ } catch (e) {
873
+ throw new Error('polyfill failed because global object is unavailable in this environment');
874
+ }
875
+ }
876
+
877
+ var P = local.Promise;
878
+
879
+ if (P && Object.prototype.toString.call(P.resolve()) === '[object Promise]' && !P.cast) {
880
+ return;
881
+ }
882
+
883
+ local.Promise = lib$es6$promise$promise$$default;
884
+ }
885
+ var lib$es6$promise$polyfill$$default = lib$es6$promise$polyfill$$polyfill;
886
+
887
+ var lib$es6$promise$umd$$ES6Promise = {
888
+ 'Promise': lib$es6$promise$promise$$default,
889
+ 'polyfill': lib$es6$promise$polyfill$$default
890
+ };
891
+
892
+ /* global define:true module:true window: true */
893
+ if (typeof define === 'function' && define['amd']) {
894
+ define(function() { return lib$es6$promise$umd$$ES6Promise; });
895
+ } else if (typeof module !== 'undefined' && module['exports']) {
896
+ module['exports'] = lib$es6$promise$umd$$ES6Promise;
897
+ } else if (typeof this !== 'undefined') {
898
+ this['ES6Promise'] = lib$es6$promise$umd$$ES6Promise;
899
+ }
229
900
 
230
- })(this);
901
+ lib$es6$promise$polyfill$$default();
902
+ }).call(this);
231
903
  }
@@ -1,3 +1,3 @@
1
1
  module Es6PromisePolyfillRails
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: es6_promise_polyfill_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Greene