-
Notifications
You must be signed in to change notification settings - Fork 8
/
module.js
576 lines (478 loc) · 15 KB
/
module.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
function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (it) return (it = it.call(o)).next.bind(it); if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
// === Symbol Support ===
var hasSymbols = function () {
return typeof Symbol === 'function';
};
var hasSymbol = function (name) {
return hasSymbols() && Boolean(Symbol[name]);
};
var getSymbol = function (name) {
return hasSymbol(name) ? Symbol[name] : '@@' + name;
};
if (hasSymbols() && !hasSymbol('observable')) {
Symbol.observable = Symbol('observable');
}
var SymbolIterator = getSymbol('iterator');
var SymbolObservable = getSymbol('observable');
var SymbolSpecies = getSymbol('species'); // === Abstract Operations ===
function getMethod(obj, key) {
var value = obj[key];
if (value == null) return undefined;
if (typeof value !== 'function') throw new TypeError(value + ' is not a function');
return value;
}
function getSpecies(obj) {
var ctor = obj.constructor;
if (ctor !== undefined) {
ctor = ctor[SymbolSpecies];
if (ctor === null) {
ctor = undefined;
}
}
return ctor !== undefined ? ctor : Observable;
}
function isObservable(x) {
return x instanceof Observable; // SPEC: Brand check
}
function hostReportError(e) {
if (hostReportError.log) {
hostReportError.log(e);
} else {
setTimeout(function () {
throw e;
});
}
}
function enqueue(fn) {
Promise.resolve().then(function () {
try {
fn();
} catch (e) {
hostReportError(e);
}
});
}
function cleanupSubscription(subscription) {
var cleanup = subscription._cleanup;
if (cleanup === undefined) return;
subscription._cleanup = undefined;
if (!cleanup) {
return;
}
try {
if (typeof cleanup === 'function') {
cleanup();
} else {
var unsubscribe = getMethod(cleanup, 'unsubscribe');
if (unsubscribe) {
unsubscribe.call(cleanup);
}
}
} catch (e) {
hostReportError(e);
}
}
function closeSubscription(subscription) {
subscription._observer = undefined;
subscription._queue = undefined;
subscription._state = 'closed';
}
function flushSubscription(subscription) {
var queue = subscription._queue;
if (!queue) {
return;
}
subscription._queue = undefined;
subscription._state = 'ready';
for (var i = 0; i < queue.length; ++i) {
notifySubscription(subscription, queue[i].type, queue[i].value);
if (subscription._state === 'closed') break;
}
}
function notifySubscription(subscription, type, value) {
subscription._state = 'running';
var observer = subscription._observer;
try {
var m = getMethod(observer, type);
switch (type) {
case 'next':
if (m) m.call(observer, value);
break;
case 'error':
closeSubscription(subscription);
if (m) m.call(observer, value);else throw value;
break;
case 'complete':
closeSubscription(subscription);
if (m) m.call(observer);
break;
}
} catch (e) {
hostReportError(e);
}
if (subscription._state === 'closed') cleanupSubscription(subscription);else if (subscription._state === 'running') subscription._state = 'ready';
}
function onNotify(subscription, type, value) {
if (subscription._state === 'closed') return;
if (subscription._state === 'buffering') {
subscription._queue.push({
type: type,
value: value
});
return;
}
if (subscription._state !== 'ready') {
subscription._state = 'buffering';
subscription._queue = [{
type: type,
value: value
}];
enqueue(function () {
return flushSubscription(subscription);
});
return;
}
notifySubscription(subscription, type, value);
}
var Subscription = /*#__PURE__*/function () {
function Subscription(observer, subscriber) {
// ASSERT: observer is an object
// ASSERT: subscriber is callable
this._cleanup = undefined;
this._observer = observer;
this._queue = undefined;
this._state = 'initializing';
var subscriptionObserver = new SubscriptionObserver(this);
try {
this._cleanup = subscriber.call(undefined, subscriptionObserver);
} catch (e) {
subscriptionObserver.error(e);
}
if (this._state === 'initializing') this._state = 'ready';
}
var _proto = Subscription.prototype;
_proto.unsubscribe = function unsubscribe() {
if (this._state !== 'closed') {
closeSubscription(this);
cleanupSubscription(this);
}
};
_createClass(Subscription, [{
key: "closed",
get: function () {
return this._state === 'closed';
}
}]);
return Subscription;
}();
var SubscriptionObserver = /*#__PURE__*/function () {
function SubscriptionObserver(subscription) {
this._subscription = subscription;
}
var _proto2 = SubscriptionObserver.prototype;
_proto2.next = function next(value) {
onNotify(this._subscription, 'next', value);
};
_proto2.error = function error(value) {
onNotify(this._subscription, 'error', value);
};
_proto2.complete = function complete() {
onNotify(this._subscription, 'complete');
};
_createClass(SubscriptionObserver, [{
key: "closed",
get: function () {
return this._subscription._state === 'closed';
}
}]);
return SubscriptionObserver;
}();
var Observable = /*#__PURE__*/function () {
function Observable(subscriber) {
if (!(this instanceof Observable)) throw new TypeError('Observable cannot be called as a function');
if (typeof subscriber !== 'function') throw new TypeError('Observable initializer must be a function');
this._subscriber = subscriber;
}
var _proto3 = Observable.prototype;
_proto3.subscribe = function subscribe(observer) {
if (typeof observer !== 'object' || observer === null) {
observer = {
next: observer,
error: arguments[1],
complete: arguments[2]
};
}
return new Subscription(observer, this._subscriber);
};
_proto3.forEach = function forEach(fn) {
var _this = this;
return new Promise(function (resolve, reject) {
if (typeof fn !== 'function') {
reject(new TypeError(fn + ' is not a function'));
return;
}
function done() {
subscription.unsubscribe();
resolve();
}
var subscription = _this.subscribe({
next: function (value) {
try {
fn(value, done);
} catch (e) {
reject(e);
subscription.unsubscribe();
}
},
error: reject,
complete: resolve
});
});
};
_proto3.map = function map(fn) {
var _this2 = this;
if (typeof fn !== 'function') throw new TypeError(fn + ' is not a function');
var C = getSpecies(this);
return new C(function (observer) {
return _this2.subscribe({
next: function (value) {
try {
value = fn(value);
} catch (e) {
return observer.error(e);
}
observer.next(value);
},
error: function (e) {
observer.error(e);
},
complete: function () {
observer.complete();
}
});
});
};
_proto3.filter = function filter(fn) {
var _this3 = this;
if (typeof fn !== 'function') throw new TypeError(fn + ' is not a function');
var C = getSpecies(this);
return new C(function (observer) {
return _this3.subscribe({
next: function (value) {
try {
if (!fn(value)) return;
} catch (e) {
return observer.error(e);
}
observer.next(value);
},
error: function (e) {
observer.error(e);
},
complete: function () {
observer.complete();
}
});
});
};
_proto3.reduce = function reduce(fn) {
var _this4 = this;
if (typeof fn !== 'function') throw new TypeError(fn + ' is not a function');
var C = getSpecies(this);
var hasSeed = arguments.length > 1;
var hasValue = false;
var seed = arguments[1];
var acc = seed;
return new C(function (observer) {
return _this4.subscribe({
next: function (value) {
var first = !hasValue;
hasValue = true;
if (!first || hasSeed) {
try {
acc = fn(acc, value);
} catch (e) {
return observer.error(e);
}
} else {
acc = value;
}
},
error: function (e) {
observer.error(e);
},
complete: function () {
if (!hasValue && !hasSeed) return observer.error(new TypeError('Cannot reduce an empty sequence'));
observer.next(acc);
observer.complete();
}
});
});
};
_proto3.concat = function concat() {
var _this5 = this;
for (var _len = arguments.length, sources = new Array(_len), _key = 0; _key < _len; _key++) {
sources[_key] = arguments[_key];
}
var C = getSpecies(this);
return new C(function (observer) {
var subscription;
var index = 0;
function startNext(next) {
subscription = next.subscribe({
next: function (v) {
observer.next(v);
},
error: function (e) {
observer.error(e);
},
complete: function () {
if (index === sources.length) {
subscription = undefined;
observer.complete();
} else {
startNext(C.from(sources[index++]));
}
}
});
}
startNext(_this5);
return function () {
if (subscription) {
subscription.unsubscribe();
subscription = undefined;
}
};
});
};
_proto3.flatMap = function flatMap(fn) {
var _this6 = this;
if (typeof fn !== 'function') throw new TypeError(fn + ' is not a function');
var C = getSpecies(this);
return new C(function (observer) {
var subscriptions = [];
var outer = _this6.subscribe({
next: function (value) {
if (fn) {
try {
value = fn(value);
} catch (e) {
return observer.error(e);
}
}
var inner = C.from(value).subscribe({
next: function (value) {
observer.next(value);
},
error: function (e) {
observer.error(e);
},
complete: function () {
var i = subscriptions.indexOf(inner);
if (i >= 0) subscriptions.splice(i, 1);
completeIfDone();
}
});
subscriptions.push(inner);
},
error: function (e) {
observer.error(e);
},
complete: function () {
completeIfDone();
}
});
function completeIfDone() {
if (outer.closed && subscriptions.length === 0) observer.complete();
}
return function () {
subscriptions.forEach(function (s) {
return s.unsubscribe();
});
outer.unsubscribe();
};
});
};
_proto3[SymbolObservable] = function () {
return this;
};
Observable.from = function from(x) {
var C = typeof this === 'function' ? this : Observable;
if (x == null) throw new TypeError(x + ' is not an object');
var method = getMethod(x, SymbolObservable);
if (method) {
var observable = method.call(x);
if (Object(observable) !== observable) throw new TypeError(observable + ' is not an object');
if (isObservable(observable) && observable.constructor === C) return observable;
return new C(function (observer) {
return observable.subscribe(observer);
});
}
if (hasSymbol('iterator')) {
method = getMethod(x, SymbolIterator);
if (method) {
return new C(function (observer) {
enqueue(function () {
if (observer.closed) return;
for (var _iterator = _createForOfIteratorHelperLoose(method.call(x)), _step; !(_step = _iterator()).done;) {
var item = _step.value;
observer.next(item);
if (observer.closed) return;
}
observer.complete();
});
});
}
}
if (Array.isArray(x)) {
return new C(function (observer) {
enqueue(function () {
if (observer.closed) return;
for (var i = 0; i < x.length; ++i) {
observer.next(x[i]);
if (observer.closed) return;
}
observer.complete();
});
});
}
throw new TypeError(x + ' is not observable');
};
Observable.of = function of() {
for (var _len2 = arguments.length, items = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
items[_key2] = arguments[_key2];
}
var C = typeof this === 'function' ? this : Observable;
return new C(function (observer) {
enqueue(function () {
if (observer.closed) return;
for (var i = 0; i < items.length; ++i) {
observer.next(items[i]);
if (observer.closed) return;
}
observer.complete();
});
});
};
_createClass(Observable, null, [{
key: SymbolSpecies,
get: function () {
return this;
}
}]);
return Observable;
}();
if (hasSymbols()) {
Object.defineProperty(Observable, Symbol('extensions'), {
value: {
symbol: SymbolObservable,
hostReportError: hostReportError
},
configurable: true
});
}
export { Observable };