UNPKG

5.17 kBJavaScriptView Raw
1import { __extends } from "tslib";
2import { Observable } from "./Observable.js";
3import { iterateObserversSafely } from "./iteration.js";
4import { fixObservableSubclass } from "./subclassing.js";
5function isPromiseLike(value) {
6 return value && typeof value.then === "function";
7}
8var Concast = (function (_super) {
9 __extends(Concast, _super);
10 function Concast(sources) {
11 var _this = _super.call(this, function (observer) {
12 _this.addObserver(observer);
13 return function () { return _this.removeObserver(observer); };
14 }) || this;
15 _this.observers = new Set();
16 _this.promise = new Promise(function (resolve, reject) {
17 _this.resolve = resolve;
18 _this.reject = reject;
19 });
20 _this.handlers = {
21 next: function (result) {
22 if (_this.sub !== null) {
23 _this.latest = ["next", result];
24 _this.notify("next", result);
25 iterateObserversSafely(_this.observers, "next", result);
26 }
27 },
28 error: function (error) {
29 var sub = _this.sub;
30 if (sub !== null) {
31 if (sub)
32 setTimeout(function () { return sub.unsubscribe(); });
33 _this.sub = null;
34 _this.latest = ["error", error];
35 _this.reject(error);
36 _this.notify("error", error);
37 iterateObserversSafely(_this.observers, "error", error);
38 }
39 },
40 complete: function () {
41 var _a = _this, sub = _a.sub, _b = _a.sources, sources = _b === void 0 ? [] : _b;
42 if (sub !== null) {
43 var value = sources.shift();
44 if (!value) {
45 if (sub)
46 setTimeout(function () { return sub.unsubscribe(); });
47 _this.sub = null;
48 if (_this.latest &&
49 _this.latest[0] === "next") {
50 _this.resolve(_this.latest[1]);
51 }
52 else {
53 _this.resolve();
54 }
55 _this.notify("complete");
56 iterateObserversSafely(_this.observers, "complete");
57 }
58 else if (isPromiseLike(value)) {
59 value.then(function (obs) { return _this.sub = obs.subscribe(_this.handlers); });
60 }
61 else {
62 _this.sub = value.subscribe(_this.handlers);
63 }
64 }
65 },
66 };
67 _this.nextResultListeners = new Set();
68 _this.cancel = function (reason) {
69 _this.reject(reason);
70 _this.sources = [];
71 _this.handlers.complete();
72 };
73 _this.promise.catch(function (_) { });
74 if (typeof sources === "function") {
75 sources = [new Observable(sources)];
76 }
77 if (isPromiseLike(sources)) {
78 sources.then(function (iterable) { return _this.start(iterable); }, _this.handlers.error);
79 }
80 else {
81 _this.start(sources);
82 }
83 return _this;
84 }
85 Concast.prototype.start = function (sources) {
86 if (this.sub !== void 0)
87 return;
88 this.sources = Array.from(sources);
89 this.handlers.complete();
90 };
91 Concast.prototype.deliverLastMessage = function (observer) {
92 if (this.latest) {
93 var nextOrError = this.latest[0];
94 var method = observer[nextOrError];
95 if (method) {
96 method.call(observer, this.latest[1]);
97 }
98 if (this.sub === null &&
99 nextOrError === "next" &&
100 observer.complete) {
101 observer.complete();
102 }
103 }
104 };
105 Concast.prototype.addObserver = function (observer) {
106 if (!this.observers.has(observer)) {
107 this.deliverLastMessage(observer);
108 this.observers.add(observer);
109 }
110 };
111 Concast.prototype.removeObserver = function (observer) {
112 if (this.observers.delete(observer) &&
113 this.observers.size < 1) {
114 this.handlers.complete();
115 }
116 };
117 Concast.prototype.notify = function (method, arg) {
118 var nextResultListeners = this.nextResultListeners;
119 if (nextResultListeners.size) {
120 this.nextResultListeners = new Set;
121 nextResultListeners.forEach(function (listener) { return listener(method, arg); });
122 }
123 };
124 Concast.prototype.beforeNext = function (callback) {
125 var called = false;
126 this.nextResultListeners.add(function (method, arg) {
127 if (!called) {
128 called = true;
129 callback(method, arg);
130 }
131 });
132 };
133 return Concast;
134}(Observable));
135export { Concast };
136fixObservableSubclass(Concast);
137//# sourceMappingURL=Concast.js.map
\No newline at end of file