-
Notifications
You must be signed in to change notification settings - Fork 22
/
asyncgenerator.js
1678 lines (1451 loc) · 52.7 KB
/
asyncgenerator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var n;"undefined"!=typeof window?n=window:"undefined"!=typeof global?n=global:"undefined"!=typeof self&&(n=self),n.asyncgenerator=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
(function (process){
// Use the fastest possible means to execute a task in a future turn
// of the event loop.
// linked list of tasks (single, with head node)
var head = {task: void 0, next: null};
var tail = head;
var flushing = false;
var requestFlush = void 0;
var isNodeJS = false;
function flush() {
/* jshint loopfunc: true */
while (head.next) {
head = head.next;
var task = head.task;
head.task = void 0;
var domain = head.domain;
if (domain) {
head.domain = void 0;
domain.enter();
}
try {
task();
} catch (e) {
if (isNodeJS) {
// In node, uncaught exceptions are considered fatal errors.
// Re-throw them synchronously to interrupt flushing!
// Ensure continuation if the uncaught exception is suppressed
// listening "uncaughtException" events (as domains does).
// Continue in next event to avoid tick recursion.
if (domain) {
domain.exit();
}
setTimeout(flush, 0);
if (domain) {
domain.enter();
}
throw e;
} else {
// In browsers, uncaught exceptions are not fatal.
// Re-throw them asynchronously to avoid slow-downs.
setTimeout(function() {
throw e;
}, 0);
}
}
if (domain) {
domain.exit();
}
}
flushing = false;
}
if (typeof process !== "undefined" && process.nextTick) {
// Node.js before 0.9. Note that some fake-Node environments, like the
// Mocha test runner, introduce a `process` global without a `nextTick`.
isNodeJS = true;
requestFlush = function () {
process.nextTick(flush);
};
} else if (typeof setImmediate === "function") {
// In IE10, Node.js 0.9+, or https://github.com/NobleJS/setImmediate
if (typeof window !== "undefined") {
requestFlush = setImmediate.bind(window, flush);
} else {
requestFlush = function () {
setImmediate(flush);
};
}
} else if (typeof MessageChannel !== "undefined") {
// modern browsers
// http://www.nonblocking.io/2011/06/windownexttick.html
var channel = new MessageChannel();
channel.port1.onmessage = flush;
requestFlush = function () {
channel.port2.postMessage(0);
};
} else {
// old browsers
requestFlush = function () {
setTimeout(flush, 0);
};
}
function asap(task) {
tail = tail.next = {
task: task,
domain: isNodeJS && process.domain,
next: null
};
if (!flushing) {
flushing = true;
requestFlush();
}
};
module.exports = asap;
}).call(this,_dereq_("FWaASH"))
},{"FWaASH":2}],2:[function(_dereq_,module,exports){
// shim for using process in browser
var process = module.exports = {};
process.nextTick = (function () {
var canSetImmediate = typeof window !== 'undefined'
&& window.setImmediate;
var canPost = typeof window !== 'undefined'
&& window.postMessage && window.addEventListener
;
if (canSetImmediate) {
return function (f) { return window.setImmediate(f) };
}
if (canPost) {
var queue = [];
window.addEventListener('message', function (ev) {
var source = ev.source;
if ((source === window || source === null) && ev.data === 'process-tick') {
ev.stopPropagation();
if (queue.length > 0) {
var fn = queue.shift();
fn();
}
}
}, true);
return function nextTick(fn) {
queue.push(fn);
window.postMessage('process-tick', '*');
};
}
return function nextTick(fn) {
setTimeout(fn, 0);
};
})();
process.title = 'browser';
process.browser = true;
process.env = {};
process.argv = [];
function noop() {}
process.on = noop;
process.addListener = noop;
process.once = noop;
process.off = noop;
process.removeListener = noop;
process.removeAllListeners = noop;
process.emit = noop;
process.binding = function (name) {
throw new Error('process.binding is not supported');
}
// TODO(shtylman)
process.cwd = function () { return '/' };
process.chdir = function (dir) {
throw new Error('process.chdir is not supported');
};
},{}],3:[function(_dereq_,module,exports){
'use strict';
module.exports = _dereq_('./is-implemented')() ? Symbol : _dereq_('./polyfill');
},{"./is-implemented":4,"./polyfill":18}],4:[function(_dereq_,module,exports){
'use strict';
module.exports = function () {
var symbol;
if (typeof Symbol !== 'function') return false;
symbol = Symbol('test symbol');
try {
if (String(symbol) !== 'Symbol (test symbol)') return false;
} catch (e) { return false; }
if (typeof Symbol.iterator === 'symbol') return true;
// Return 'true' for polyfills
if (typeof Symbol.isConcatSpreadable !== 'object') return false;
if (typeof Symbol.isRegExp !== 'object') return false;
if (typeof Symbol.iterator !== 'object') return false;
if (typeof Symbol.toPrimitive !== 'object') return false;
if (typeof Symbol.toStringTag !== 'object') return false;
if (typeof Symbol.unscopables !== 'object') return false;
return true;
};
},{}],5:[function(_dereq_,module,exports){
'use strict';
var assign = _dereq_('es5-ext/object/assign')
, normalizeOpts = _dereq_('es5-ext/object/normalize-options')
, isCallable = _dereq_('es5-ext/object/is-callable')
, contains = _dereq_('es5-ext/string/#/contains')
, d;
d = module.exports = function (dscr, value/*, options*/) {
var c, e, w, options, desc;
if ((arguments.length < 2) || (typeof dscr !== 'string')) {
options = value;
value = dscr;
dscr = null;
} else {
options = arguments[2];
}
if (dscr == null) {
c = w = true;
e = false;
} else {
c = contains.call(dscr, 'c');
e = contains.call(dscr, 'e');
w = contains.call(dscr, 'w');
}
desc = { value: value, configurable: c, enumerable: e, writable: w };
return !options ? desc : assign(normalizeOpts(options), desc);
};
d.gs = function (dscr, get, set/*, options*/) {
var c, e, options, desc;
if (typeof dscr !== 'string') {
options = set;
set = get;
get = dscr;
dscr = null;
} else {
options = arguments[3];
}
if (get == null) {
get = undefined;
} else if (!isCallable(get)) {
options = get;
get = set = undefined;
} else if (set == null) {
set = undefined;
} else if (!isCallable(set)) {
options = set;
set = undefined;
}
if (dscr == null) {
c = true;
e = false;
} else {
c = contains.call(dscr, 'c');
e = contains.call(dscr, 'e');
}
desc = { get: get, set: set, configurable: c, enumerable: e };
return !options ? desc : assign(normalizeOpts(options), desc);
};
},{"es5-ext/object/assign":6,"es5-ext/object/is-callable":9,"es5-ext/object/normalize-options":13,"es5-ext/string/#/contains":15}],6:[function(_dereq_,module,exports){
'use strict';
module.exports = _dereq_('./is-implemented')()
? Object.assign
: _dereq_('./shim');
},{"./is-implemented":7,"./shim":8}],7:[function(_dereq_,module,exports){
'use strict';
module.exports = function () {
var assign = Object.assign, obj;
if (typeof assign !== 'function') return false;
obj = { foo: 'raz' };
assign(obj, { bar: 'dwa' }, { trzy: 'trzy' });
return (obj.foo + obj.bar + obj.trzy) === 'razdwatrzy';
};
},{}],8:[function(_dereq_,module,exports){
'use strict';
var keys = _dereq_('../keys')
, value = _dereq_('../valid-value')
, max = Math.max;
module.exports = function (dest, src/*, …srcn*/) {
var error, i, l = max(arguments.length, 2), assign;
dest = Object(value(dest));
assign = function (key) {
try { dest[key] = src[key]; } catch (e) {
if (!error) error = e;
}
};
for (i = 1; i < l; ++i) {
src = arguments[i];
keys(src).forEach(assign);
}
if (error !== undefined) throw error;
return dest;
};
},{"../keys":10,"../valid-value":14}],9:[function(_dereq_,module,exports){
// Deprecated
'use strict';
module.exports = function (obj) { return typeof obj === 'function'; };
},{}],10:[function(_dereq_,module,exports){
'use strict';
module.exports = _dereq_('./is-implemented')()
? Object.keys
: _dereq_('./shim');
},{"./is-implemented":11,"./shim":12}],11:[function(_dereq_,module,exports){
'use strict';
module.exports = function () {
try {
Object.keys('primitive');
return true;
} catch (e) { return false; }
};
},{}],12:[function(_dereq_,module,exports){
'use strict';
var keys = Object.keys;
module.exports = function (object) {
return keys(object == null ? object : Object(object));
};
},{}],13:[function(_dereq_,module,exports){
'use strict';
var assign = _dereq_('./assign')
, forEach = Array.prototype.forEach
, create = Object.create, getPrototypeOf = Object.getPrototypeOf
, process;
process = function (src, obj) {
var proto = getPrototypeOf(src);
return assign(proto ? process(proto, obj) : obj, src);
};
module.exports = function (options/*, …options*/) {
var result = create(null);
forEach.call(arguments, function (options) {
if (options == null) return;
process(Object(options), result);
});
return result;
};
},{"./assign":6}],14:[function(_dereq_,module,exports){
'use strict';
module.exports = function (value) {
if (value == null) throw new TypeError("Cannot use null or undefined");
return value;
};
},{}],15:[function(_dereq_,module,exports){
'use strict';
module.exports = _dereq_('./is-implemented')()
? String.prototype.contains
: _dereq_('./shim');
},{"./is-implemented":16,"./shim":17}],16:[function(_dereq_,module,exports){
'use strict';
var str = 'razdwatrzy';
module.exports = function () {
if (typeof str.contains !== 'function') return false;
return ((str.contains('dwa') === true) && (str.contains('foo') === false));
};
},{}],17:[function(_dereq_,module,exports){
'use strict';
var indexOf = String.prototype.indexOf;
module.exports = function (searchString/*, position*/) {
return indexOf.call(this, searchString, arguments[1]) > -1;
};
},{}],18:[function(_dereq_,module,exports){
'use strict';
var d = _dereq_('d')
, create = Object.create, defineProperties = Object.defineProperties
, generateName, Symbol;
generateName = (function () {
var created = create(null);
return function (desc) {
var postfix = 0;
while (created[desc + (postfix || '')]) ++postfix;
desc += (postfix || '');
created[desc] = true;
return '@@' + desc;
};
}());
module.exports = Symbol = function (description) {
var symbol;
if (this instanceof Symbol) {
throw new TypeError('TypeError: Symbol is not a constructor');
}
symbol = create(Symbol.prototype);
description = (description === undefined ? '' : String(description));
return defineProperties(symbol, {
__description__: d('', description),
__name__: d('', generateName(description))
});
};
Object.defineProperties(Symbol, {
create: d('', Symbol('create')),
hasInstance: d('', Symbol('hasInstance')),
isConcatSpreadable: d('', Symbol('isConcatSpreadable')),
isRegExp: d('', Symbol('isRegExp')),
iterator: d('', Symbol('iterator')),
toPrimitive: d('', Symbol('toPrimitive')),
toStringTag: d('', Symbol('toStringTag')),
unscopables: d('', Symbol('unscopables'))
});
defineProperties(Symbol.prototype, {
properToString: d(function () {
return 'Symbol (' + this.__description__ + ')';
}),
toString: d('', function () { return this.__name__; })
});
Object.defineProperty(Symbol.prototype, Symbol.toPrimitive, d('',
function (hint) {
throw new TypeError("Conversion of symbol objects is not allowed");
}));
Object.defineProperty(Symbol.prototype, Symbol.toStringTag, d('c', 'Symbol'));
},{"d":5}],19:[function(_dereq_,module,exports){
'use strict';
module.exports = _dereq_('./lib/core.js')
_dereq_('./lib/done.js')
_dereq_('./lib/es6-extensions.js')
_dereq_('./lib/node-extensions.js')
},{"./lib/core.js":20,"./lib/done.js":21,"./lib/es6-extensions.js":22,"./lib/node-extensions.js":23}],20:[function(_dereq_,module,exports){
'use strict';
var asap = _dereq_('asap')
module.exports = Promise;
function Promise(fn) {
if (typeof this !== 'object') throw new TypeError('Promises must be constructed via new')
if (typeof fn !== 'function') throw new TypeError('not a function')
var state = null
var value = null
var deferreds = []
var self = this
this.then = function(onFulfilled, onRejected) {
return new self.constructor(function(resolve, reject) {
handle(new Handler(onFulfilled, onRejected, resolve, reject))
})
}
function handle(deferred) {
if (state === null) {
deferreds.push(deferred)
return
}
asap(function() {
var cb = state ? deferred.onFulfilled : deferred.onRejected
if (cb === null) {
(state ? deferred.resolve : deferred.reject)(value)
return
}
var ret
try {
ret = cb(value)
}
catch (e) {
deferred.reject(e)
return
}
deferred.resolve(ret)
})
}
function resolve(newValue) {
try { //Promise Resolution Procedure: https://github.com/promises-aplus/promises-spec#the-promise-resolution-procedure
if (newValue === self) throw new TypeError('A promise cannot be resolved with itself.')
if (newValue && (typeof newValue === 'object' || typeof newValue === 'function')) {
var then = newValue.then
if (typeof then === 'function') {
doResolve(then.bind(newValue), resolve, reject)
return
}
}
state = true
value = newValue
finale()
} catch (e) { reject(e) }
}
function reject(newValue) {
state = false
value = newValue
finale()
}
function finale() {
for (var i = 0, len = deferreds.length; i < len; i++)
handle(deferreds[i])
deferreds = null
}
doResolve(fn, resolve, reject)
}
function Handler(onFulfilled, onRejected, resolve, reject){
this.onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : null
this.onRejected = typeof onRejected === 'function' ? onRejected : null
this.resolve = resolve
this.reject = reject
}
/**
* Take a potentially misbehaving resolver function and make sure
* onFulfilled and onRejected are only called once.
*
* Makes no guarantees about asynchrony.
*/
function doResolve(fn, onFulfilled, onRejected) {
var done = false;
try {
fn(function (value) {
if (done) return
done = true
onFulfilled(value)
}, function (reason) {
if (done) return
done = true
onRejected(reason)
})
} catch (ex) {
if (done) return
done = true
onRejected(ex)
}
}
},{"asap":24}],21:[function(_dereq_,module,exports){
'use strict';
var Promise = _dereq_('./core.js')
var asap = _dereq_('asap')
module.exports = Promise
Promise.prototype.done = function (onFulfilled, onRejected) {
var self = arguments.length ? this.then.apply(this, arguments) : this
self.then(null, function (err) {
asap(function () {
throw err
})
})
}
},{"./core.js":20,"asap":24}],22:[function(_dereq_,module,exports){
'use strict';
//This file contains the ES6 extensions to the core Promises/A+ API
var Promise = _dereq_('./core.js')
var asap = _dereq_('asap')
module.exports = Promise
/* Static Functions */
function ValuePromise(value) {
this.then = function (onFulfilled) {
if (typeof onFulfilled !== 'function') return this
return new Promise(function (resolve, reject) {
asap(function () {
try {
resolve(onFulfilled(value))
} catch (ex) {
reject(ex);
}
})
})
}
}
ValuePromise.prototype = Promise.prototype
var TRUE = new ValuePromise(true)
var FALSE = new ValuePromise(false)
var NULL = new ValuePromise(null)
var UNDEFINED = new ValuePromise(undefined)
var ZERO = new ValuePromise(0)
var EMPTYSTRING = new ValuePromise('')
Promise.resolve = function (value) {
if (value instanceof Promise) return value
if (value === null) return NULL
if (value === undefined) return UNDEFINED
if (value === true) return TRUE
if (value === false) return FALSE
if (value === 0) return ZERO
if (value === '') return EMPTYSTRING
if (typeof value === 'object' || typeof value === 'function') {
try {
var then = value.then
if (typeof then === 'function') {
return new Promise(then.bind(value))
}
} catch (ex) {
return new Promise(function (resolve, reject) {
reject(ex)
})
}
}
return new ValuePromise(value)
}
Promise.all = function (arr) {
var args = Array.prototype.slice.call(arr)
return new Promise(function (resolve, reject) {
if (args.length === 0) return resolve([])
var remaining = args.length
function res(i, val) {
try {
if (val && (typeof val === 'object' || typeof val === 'function')) {
var then = val.then
if (typeof then === 'function') {
then.call(val, function (val) { res(i, val) }, reject)
return
}
}
args[i] = val
if (--remaining === 0) {
resolve(args);
}
} catch (ex) {
reject(ex)
}
}
for (var i = 0; i < args.length; i++) {
res(i, args[i])
}
})
}
Promise.reject = function (value) {
return new Promise(function (resolve, reject) {
reject(value);
});
}
Promise.race = function (values) {
return new Promise(function (resolve, reject) {
values.forEach(function(value){
Promise.resolve(value).then(resolve, reject);
})
});
}
/* Prototype Methods */
Promise.prototype['catch'] = function (onRejected) {
return this.then(null, onRejected);
}
},{"./core.js":20,"asap":24}],23:[function(_dereq_,module,exports){
'use strict';
//This file contains then/promise specific extensions that are only useful for node.js interop
var Promise = _dereq_('./core.js')
var asap = _dereq_('asap')
module.exports = Promise
/* Static Functions */
Promise.denodeify = function (fn, argumentCount) {
argumentCount = argumentCount || Infinity
return function () {
var self = this
var args = Array.prototype.slice.call(arguments)
return new Promise(function (resolve, reject) {
while (args.length && args.length > argumentCount) {
args.pop()
}
args.push(function (err, res) {
if (err) reject(err)
else resolve(res)
})
fn.apply(self, args)
})
}
}
Promise.nodeify = function (fn) {
return function () {
var args = Array.prototype.slice.call(arguments)
var callback = typeof args[args.length - 1] === 'function' ? args.pop() : null
var ctx = this
try {
return fn.apply(this, arguments).nodeify(callback, ctx)
} catch (ex) {
if (callback === null || typeof callback == 'undefined') {
return new Promise(function (resolve, reject) { reject(ex) })
} else {
asap(function () {
callback.call(ctx, ex)
})
}
}
}
}
Promise.prototype.nodeify = function (callback, ctx) {
if (typeof callback != 'function') return this
this.then(function (value) {
asap(function () {
callback.call(ctx, null, value)
})
}, function (err) {
asap(function () {
callback.call(ctx, err)
})
})
}
},{"./core.js":20,"asap":24}],24:[function(_dereq_,module,exports){
module.exports=_dereq_(1)
},{"FWaASH":2}],25:[function(_dereq_,module,exports){
var Promise = _dereq_('promise');
var Symbol = _dereq_('es6-symbol');
var asap = _dereq_('asap');
function decorate(iterator, onDone) {
var done = false,
nextFn = iterator.next;
return Object.create(
iterator,
{
throw: {
value: function(e) {
var throwFn = iterator.throw;
if (!done) {
done = true;
if (onDone) {
onDone.call(this);
}
if (throwFn) {
return throwFn.call(iterator, e);
}
}
}
},
return: {
value: function(v) {
var returnFn = iterator.return;
if (!done) {
done = true;
if (onDone) {
onDone.call(this);
}
if (returnFn) {
return returnFn.call(iterator, v);
}
}
}
},
});
};
var microTaskScheduler = function(fn, args) {
//asap(fn.bind(null, args));
setTimeout(function() {
fn(args);
})
};
function Observable(observeDefn) {
this.observe = observeDefn;
}
Observable.fromEventPattern = function(add, remove, scheduler) {
scheduler = scheduler || microTaskScheduler;
return new Observable(function observe(generator) {
var handler,
decoratedGenerator =
decorate(
generator,
function() {
remove(handler);
}),
next = decoratedGenerator.next;
handler = function() {
if (next) {
next.apply(decoratedGenerator, Array.prototype.slice.call(arguments));
}
};
scheduler(function() { add(handler) });
return decoratedGenerator;
});
};
// Convert any DOM event into an async generator
Observable.fromEvent = function(dom, eventName, syncAction, scheduler) {
scheduler = scheduler || microTaskScheduler;
return new Observable(function fromDOMEventObserve(generator) {
var handler,
decoratedGenerator =
decorate(
generator,
function onDone() {
dom.removeEventListener(eventName, handler);
});
handler = function(e) {
if (syncAction) {
syncAction(e);
}
decoratedGenerator.next(e);
};
scheduler(function() {
dom.addEventListener(eventName, handler)
});
return decoratedGenerator;
});
};
Observable.empty = function(scheduler) {
scheduler = scheduler || microTaskScheduler;
return new Observable(function(generator) {
var done = false,
decoratedGenerator = decorate(generator);
scheduler(decoratedGenerator.return.bind(decoratedGenerator));
return decoratedGenerator;
});
};
Observable.from = function(arr, scheduler) {
scheduler = scheduler || microTaskScheduler;
return new Observable(function(generator) {
var done = false,
decoratedGenerator =
decorate(generator, function() { done = true });
scheduler(function() {
for(var count = 0; count < arr.length; count++) {
if (done) {
return;
}
decoratedGenerator.next(arr[count]);
}
if (done) {
return;
}
decoratedGenerator.return();
});
return decoratedGenerator;
})
};
Observable.merge = function() {
return Observable.from(Array.prototype.slice.call(arguments)).mergeAll();
}
Observable.concat = function() {
return Observable.from(Array.prototype.slice.call(arguments)).concatAll();
}
Observable.of = function() {
return Observable.from(Array.prototype.slice.call(arguments));
};
Observable.interval = function(time) {
return new Observable(function forEach(generator) {
var handle,
decoratedGenerator = decorate(generator, function() { clearInterval(handle); });
handle = setInterval(function() {
decoratedGenerator.next();
}, time);
return decoratedGenerator;
});
};
Observable.timeout = function(time) {
return new Observable(function forEach(observer) {
var handle,
decoratedObserver = decorate(observer, function() { clearInterval(handle); });
handle = setTimeout(function() {
decoratedObserver.next();
decoratedObserver.return();
}, time);
return decoratedObserver;
});
};
Observable.prototype = {
lift: function(generatorTransform) {
var self = this;
return new Observable(function(generator) {
return self.observe(generatorTransform.call(this, generator));
});
},
map: function(projection, thisArg) {
var index = 0;
return this.lift(
function(generator) {
thisArg = thisArg !== undefined ? thisArg : this;
return Object.create(
generator,
{
next: {
value: function(value) {
var next = generator.next;
if (next) {
try {
return next.call(generator, projection.call(thisArg, value), index++, this);
}
catch(e) {
return this.throw(e);
}
}
}
}
})
});
},
filter: function(predicate, thisArg) {
return this.lift(
function(generator) {
thisArg = thisArg !== undefined ? thisArg : this;
return Object.create(
generator,
{
next: {
value: function(value) {
var next = generator.next,
throwFn;
if (next && predicate.call(thisArg, value)) {
try {
return next.call(generator, value);
}