UNPKG

15.3 kBJavaScriptView Raw
1function _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."); }
2
3function _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); }
4
5function _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; }
6
7function _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); } }
8
9function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
10
11// === Symbol Support ===
12var hasSymbols = function () {
13 return typeof Symbol === 'function';
14};
15
16var hasSymbol = function (name) {
17 return hasSymbols() && Boolean(Symbol[name]);
18};
19
20var getSymbol = function (name) {
21 return hasSymbol(name) ? Symbol[name] : '@@' + name;
22};
23
24if (hasSymbols() && !hasSymbol('observable')) {
25 Symbol.observable = Symbol('observable');
26}
27
28var SymbolIterator = getSymbol('iterator');
29var SymbolObservable = getSymbol('observable');
30var SymbolSpecies = getSymbol('species'); // === Abstract Operations ===
31
32function getMethod(obj, key) {
33 var value = obj[key];
34 if (value == null) return undefined;
35 if (typeof value !== 'function') throw new TypeError(value + ' is not a function');
36 return value;
37}
38
39function getSpecies(obj) {
40 var ctor = obj.constructor;
41
42 if (ctor !== undefined) {
43 ctor = ctor[SymbolSpecies];
44
45 if (ctor === null) {
46 ctor = undefined;
47 }
48 }
49
50 return ctor !== undefined ? ctor : Observable;
51}
52
53function isObservable(x) {
54 return x instanceof Observable; // SPEC: Brand check
55}
56
57function hostReportError(e) {
58 if (hostReportError.log) {
59 hostReportError.log(e);
60 } else {
61 setTimeout(function () {
62 throw e;
63 });
64 }
65}
66
67function enqueue(fn) {
68 Promise.resolve().then(function () {
69 try {
70 fn();
71 } catch (e) {
72 hostReportError(e);
73 }
74 });
75}
76
77function cleanupSubscription(subscription) {
78 var cleanup = subscription._cleanup;
79 if (cleanup === undefined) return;
80 subscription._cleanup = undefined;
81
82 if (!cleanup) {
83 return;
84 }
85
86 try {
87 if (typeof cleanup === 'function') {
88 cleanup();
89 } else {
90 var unsubscribe = getMethod(cleanup, 'unsubscribe');
91
92 if (unsubscribe) {
93 unsubscribe.call(cleanup);
94 }
95 }
96 } catch (e) {
97 hostReportError(e);
98 }
99}
100
101function closeSubscription(subscription) {
102 subscription._observer = undefined;
103 subscription._queue = undefined;
104 subscription._state = 'closed';
105}
106
107function flushSubscription(subscription) {
108 var queue = subscription._queue;
109
110 if (!queue) {
111 return;
112 }
113
114 subscription._queue = undefined;
115 subscription._state = 'ready';
116
117 for (var i = 0; i < queue.length; ++i) {
118 notifySubscription(subscription, queue[i].type, queue[i].value);
119 if (subscription._state === 'closed') break;
120 }
121}
122
123function notifySubscription(subscription, type, value) {
124 subscription._state = 'running';
125 var observer = subscription._observer;
126
127 try {
128 var m = getMethod(observer, type);
129
130 switch (type) {
131 case 'next':
132 if (m) m.call(observer, value);
133 break;
134
135 case 'error':
136 closeSubscription(subscription);
137 if (m) m.call(observer, value);else throw value;
138 break;
139
140 case 'complete':
141 closeSubscription(subscription);
142 if (m) m.call(observer);
143 break;
144 }
145 } catch (e) {
146 hostReportError(e);
147 }
148
149 if (subscription._state === 'closed') cleanupSubscription(subscription);else if (subscription._state === 'running') subscription._state = 'ready';
150}
151
152function onNotify(subscription, type, value) {
153 if (subscription._state === 'closed') return;
154
155 if (subscription._state === 'buffering') {
156 subscription._queue.push({
157 type: type,
158 value: value
159 });
160
161 return;
162 }
163
164 if (subscription._state !== 'ready') {
165 subscription._state = 'buffering';
166 subscription._queue = [{
167 type: type,
168 value: value
169 }];
170 enqueue(function () {
171 return flushSubscription(subscription);
172 });
173 return;
174 }
175
176 notifySubscription(subscription, type, value);
177}
178
179var Subscription = /*#__PURE__*/function () {
180 function Subscription(observer, subscriber) {
181 // ASSERT: observer is an object
182 // ASSERT: subscriber is callable
183 this._cleanup = undefined;
184 this._observer = observer;
185 this._queue = undefined;
186 this._state = 'initializing';
187 var subscriptionObserver = new SubscriptionObserver(this);
188
189 try {
190 this._cleanup = subscriber.call(undefined, subscriptionObserver);
191 } catch (e) {
192 subscriptionObserver.error(e);
193 }
194
195 if (this._state === 'initializing') this._state = 'ready';
196 }
197
198 var _proto = Subscription.prototype;
199
200 _proto.unsubscribe = function unsubscribe() {
201 if (this._state !== 'closed') {
202 closeSubscription(this);
203 cleanupSubscription(this);
204 }
205 };
206
207 _createClass(Subscription, [{
208 key: "closed",
209 get: function () {
210 return this._state === 'closed';
211 }
212 }]);
213
214 return Subscription;
215}();
216
217var SubscriptionObserver = /*#__PURE__*/function () {
218 function SubscriptionObserver(subscription) {
219 this._subscription = subscription;
220 }
221
222 var _proto2 = SubscriptionObserver.prototype;
223
224 _proto2.next = function next(value) {
225 onNotify(this._subscription, 'next', value);
226 };
227
228 _proto2.error = function error(value) {
229 onNotify(this._subscription, 'error', value);
230 };
231
232 _proto2.complete = function complete() {
233 onNotify(this._subscription, 'complete');
234 };
235
236 _createClass(SubscriptionObserver, [{
237 key: "closed",
238 get: function () {
239 return this._subscription._state === 'closed';
240 }
241 }]);
242
243 return SubscriptionObserver;
244}();
245
246var Observable = /*#__PURE__*/function () {
247 function Observable(subscriber) {
248 if (!(this instanceof Observable)) throw new TypeError('Observable cannot be called as a function');
249 if (typeof subscriber !== 'function') throw new TypeError('Observable initializer must be a function');
250 this._subscriber = subscriber;
251 }
252
253 var _proto3 = Observable.prototype;
254
255 _proto3.subscribe = function subscribe(observer) {
256 if (typeof observer !== 'object' || observer === null) {
257 observer = {
258 next: observer,
259 error: arguments[1],
260 complete: arguments[2]
261 };
262 }
263
264 return new Subscription(observer, this._subscriber);
265 };
266
267 _proto3.forEach = function forEach(fn) {
268 var _this = this;
269
270 return new Promise(function (resolve, reject) {
271 if (typeof fn !== 'function') {
272 reject(new TypeError(fn + ' is not a function'));
273 return;
274 }
275
276 function done() {
277 subscription.unsubscribe();
278 resolve();
279 }
280
281 var subscription = _this.subscribe({
282 next: function (value) {
283 try {
284 fn(value, done);
285 } catch (e) {
286 reject(e);
287 subscription.unsubscribe();
288 }
289 },
290 error: reject,
291 complete: resolve
292 });
293 });
294 };
295
296 _proto3.map = function map(fn) {
297 var _this2 = this;
298
299 if (typeof fn !== 'function') throw new TypeError(fn + ' is not a function');
300 var C = getSpecies(this);
301 return new C(function (observer) {
302 return _this2.subscribe({
303 next: function (value) {
304 try {
305 value = fn(value);
306 } catch (e) {
307 return observer.error(e);
308 }
309
310 observer.next(value);
311 },
312 error: function (e) {
313 observer.error(e);
314 },
315 complete: function () {
316 observer.complete();
317 }
318 });
319 });
320 };
321
322 _proto3.filter = function filter(fn) {
323 var _this3 = this;
324
325 if (typeof fn !== 'function') throw new TypeError(fn + ' is not a function');
326 var C = getSpecies(this);
327 return new C(function (observer) {
328 return _this3.subscribe({
329 next: function (value) {
330 try {
331 if (!fn(value)) return;
332 } catch (e) {
333 return observer.error(e);
334 }
335
336 observer.next(value);
337 },
338 error: function (e) {
339 observer.error(e);
340 },
341 complete: function () {
342 observer.complete();
343 }
344 });
345 });
346 };
347
348 _proto3.reduce = function reduce(fn) {
349 var _this4 = this;
350
351 if (typeof fn !== 'function') throw new TypeError(fn + ' is not a function');
352 var C = getSpecies(this);
353 var hasSeed = arguments.length > 1;
354 var hasValue = false;
355 var seed = arguments[1];
356 var acc = seed;
357 return new C(function (observer) {
358 return _this4.subscribe({
359 next: function (value) {
360 var first = !hasValue;
361 hasValue = true;
362
363 if (!first || hasSeed) {
364 try {
365 acc = fn(acc, value);
366 } catch (e) {
367 return observer.error(e);
368 }
369 } else {
370 acc = value;
371 }
372 },
373 error: function (e) {
374 observer.error(e);
375 },
376 complete: function () {
377 if (!hasValue && !hasSeed) return observer.error(new TypeError('Cannot reduce an empty sequence'));
378 observer.next(acc);
379 observer.complete();
380 }
381 });
382 });
383 };
384
385 _proto3.concat = function concat() {
386 var _this5 = this;
387
388 for (var _len = arguments.length, sources = new Array(_len), _key = 0; _key < _len; _key++) {
389 sources[_key] = arguments[_key];
390 }
391
392 var C = getSpecies(this);
393 return new C(function (observer) {
394 var subscription;
395 var index = 0;
396
397 function startNext(next) {
398 subscription = next.subscribe({
399 next: function (v) {
400 observer.next(v);
401 },
402 error: function (e) {
403 observer.error(e);
404 },
405 complete: function () {
406 if (index === sources.length) {
407 subscription = undefined;
408 observer.complete();
409 } else {
410 startNext(C.from(sources[index++]));
411 }
412 }
413 });
414 }
415
416 startNext(_this5);
417 return function () {
418 if (subscription) {
419 subscription.unsubscribe();
420 subscription = undefined;
421 }
422 };
423 });
424 };
425
426 _proto3.flatMap = function flatMap(fn) {
427 var _this6 = this;
428
429 if (typeof fn !== 'function') throw new TypeError(fn + ' is not a function');
430 var C = getSpecies(this);
431 return new C(function (observer) {
432 var subscriptions = [];
433
434 var outer = _this6.subscribe({
435 next: function (value) {
436 if (fn) {
437 try {
438 value = fn(value);
439 } catch (e) {
440 return observer.error(e);
441 }
442 }
443
444 var inner = C.from(value).subscribe({
445 next: function (value) {
446 observer.next(value);
447 },
448 error: function (e) {
449 observer.error(e);
450 },
451 complete: function () {
452 var i = subscriptions.indexOf(inner);
453 if (i >= 0) subscriptions.splice(i, 1);
454 completeIfDone();
455 }
456 });
457 subscriptions.push(inner);
458 },
459 error: function (e) {
460 observer.error(e);
461 },
462 complete: function () {
463 completeIfDone();
464 }
465 });
466
467 function completeIfDone() {
468 if (outer.closed && subscriptions.length === 0) observer.complete();
469 }
470
471 return function () {
472 subscriptions.forEach(function (s) {
473 return s.unsubscribe();
474 });
475 outer.unsubscribe();
476 };
477 });
478 };
479
480 _proto3[SymbolObservable] = function () {
481 return this;
482 };
483
484 Observable.from = function from(x) {
485 var C = typeof this === 'function' ? this : Observable;
486 if (x == null) throw new TypeError(x + ' is not an object');
487 var method = getMethod(x, SymbolObservable);
488
489 if (method) {
490 var observable = method.call(x);
491 if (Object(observable) !== observable) throw new TypeError(observable + ' is not an object');
492 if (isObservable(observable) && observable.constructor === C) return observable;
493 return new C(function (observer) {
494 return observable.subscribe(observer);
495 });
496 }
497
498 if (hasSymbol('iterator')) {
499 method = getMethod(x, SymbolIterator);
500
501 if (method) {
502 return new C(function (observer) {
503 enqueue(function () {
504 if (observer.closed) return;
505
506 for (var _iterator = _createForOfIteratorHelperLoose(method.call(x)), _step; !(_step = _iterator()).done;) {
507 var item = _step.value;
508 observer.next(item);
509 if (observer.closed) return;
510 }
511
512 observer.complete();
513 });
514 });
515 }
516 }
517
518 if (Array.isArray(x)) {
519 return new C(function (observer) {
520 enqueue(function () {
521 if (observer.closed) return;
522
523 for (var i = 0; i < x.length; ++i) {
524 observer.next(x[i]);
525 if (observer.closed) return;
526 }
527
528 observer.complete();
529 });
530 });
531 }
532
533 throw new TypeError(x + ' is not observable');
534 };
535
536 Observable.of = function of() {
537 for (var _len2 = arguments.length, items = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
538 items[_key2] = arguments[_key2];
539 }
540
541 var C = typeof this === 'function' ? this : Observable;
542 return new C(function (observer) {
543 enqueue(function () {
544 if (observer.closed) return;
545
546 for (var i = 0; i < items.length; ++i) {
547 observer.next(items[i]);
548 if (observer.closed) return;
549 }
550
551 observer.complete();
552 });
553 });
554 };
555
556 _createClass(Observable, null, [{
557 key: SymbolSpecies,
558 get: function () {
559 return this;
560 }
561 }]);
562
563 return Observable;
564}();
565
566if (hasSymbols()) {
567 Object.defineProperty(Observable, Symbol('extensions'), {
568 value: {
569 symbol: SymbolObservable,
570 hostReportError: hostReportError
571 },
572 configurable: true
573 });
574}
575
576export { Observable };