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AI,MAAM,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,mBAAmB,CAAC,GAAG,CAAC,UAAC,MAAM,EAAE,GAAG;YACvC,IAAI,CAAC,MAAM,EAAE;gBACX,MAAM,GAAG,IAAI,CAAC;gBACd,QAAQ,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;aACvB;QACH,CAAC,CAAC,CAAC;IACL,CAAC;IAQH,cAAC;AAAD,CAAC,AAhND,CAAgC,UAAU,GAgNzC;;AASD,qBAAqB,CAAC,OAAO,CAAC,CAAC","sourcesContent":["import { Observable, Observer, ObservableSubscription, Subscriber } from \"./Observable\";\nimport { iterateObserversSafely } from \"./iteration\";\nimport { fixObservableSubclass } from \"./subclassing\";\n\ntype MaybeAsync<T> = T | PromiseLike<T>;\n\nfunction isPromiseLike<T>(value: MaybeAsync<T>): value is PromiseLike<T> {\n return value && typeof (value as any).then === \"function\";\n}\n\n// Any individual Source<T> can be an Observable<T> or a promise for one.\ntype Source<T> = MaybeAsync<Observable<T>>;\n\nexport type ConcastSourcesIterable<T> = Iterable<Source<T>>;\nexport type ConcastSourcesArray<T> = Array<Source<T>>;\n\n// A Concast<T> observable concatenates the given sources into a single\n// non-overlapping sequence of Ts, automatically unwrapping any promises,\n// and broadcasts the T elements of that sequence to any number of\n// subscribers, all without creating a bunch of intermediary Observable\n// wrapper objects.\n//\n// Even though any number of observers can subscribe to the Concast, each\n// source observable is guaranteed to receive at most one subscribe call,\n// and the results are multicast to all observers.\n//\n// In addition to broadcasting every next/error message to this.observers,\n// the Concast stores the most recent message using this.latest, so any\n// new observers can immediately receive the latest message, even if it\n// was originally delivered in the past. This behavior means we can assume\n// every active observer in this.observers has received the same most\n// recent message.\n//\n// With the exception of this.latest replay, a Concast is a \"hot\"\n// observable in the sense that it does not replay past results from the\n// beginning of time for each new observer.\n//\n// Could we have used some existing RxJS class instead? Concast<T> is\n// similar to a BehaviorSubject<T>, because it is multicast and redelivers\n// the latest next/error message to new subscribers. Unlike Subject<T>,\n// Concast<T> does not expose an Observer<T> interface (this.handlers is\n// intentionally private), since Concast<T> gets its inputs from the\n// concatenated sources. If we ever switch to RxJS, there may be some\n// value in reusing their code, but for now we use zen-observable, which\n// does not contain any Subject implementations.\nexport class Concast<T> extends Observable<T> {\n // Active observers receiving broadcast messages. Thanks to this.latest,\n // we can assume all observers in this Set have received the same most\n // recent message, though possibly at different times in the past.\n private observers = new Set<Observer<T>>();\n\n // This property starts off undefined to indicate the initial\n // subscription has not yet begun, then points to each source\n // subscription in turn, and finally becomes null after the sources have\n // been exhausted. After that, it stays null.\n private sub?: ObservableSubscription | null;\n\n // Not only can the individual elements of the iterable be promises, but\n // also the iterable itself can be wrapped in a promise.\n constructor(sources: MaybeAsync<ConcastSourcesIterable<T>> | Subscriber<T>) {\n super(observer => {\n this.addObserver(observer);\n return () => this.removeObserver(observer);\n });\n\n // Suppress rejection warnings for this.promise, since it's perfectly\n // acceptable to pay no attention to this.promise if you're consuming\n // the results through the normal observable API.\n this.promise.catch(_ => {});\n\n // If someone accidentally tries to create a Concast using a subscriber\n // function, recover by creating an Observable from that subscriber and\n // using it as the source.\n if (typeof sources === \"function\") {\n sources = [new Observable(sources)];\n }\n\n if (isPromiseLike(sources)) {\n sources.then(\n iterable => this.start(iterable),\n this.handlers.error,\n );\n } else {\n this.start(sources);\n }\n }\n\n // A consumable array of source observables, incrementally consumed\n // each time this.handlers.complete is called.\n private sources: Source<T>[];\n\n private start(sources: ConcastSourcesIterable<T>) {\n if (this.sub !== void 0) return;\n\n // In practice, sources is most often simply an Array of observables.\n // TODO Consider using sources[Symbol.iterator]() to take advantage\n // of the laziness of non-Array iterables.\n this.sources = Array.from(sources);\n\n // Calling this.handlers.complete() kicks off consumption of the first\n // source observable. It's tempting to do this step lazily in\n // addObserver, but this.promise can be accessed without calling\n // addObserver, so consumption needs to begin eagerly.\n this.handlers.complete();\n }\n\n private deliverLastMessage(observer: Observer<T>) {\n if (this.latest) {\n const nextOrError = this.latest[0];\n const method = observer[nextOrError];\n if (method) {\n method.call(observer, this.latest[1]);\n }\n // If the subscription is already closed, and the last message was\n // a 'next' message, simulate delivery of the final 'complete'\n // message again.\n if (this.sub === null &&\n nextOrError === \"next\" &&\n observer.complete) {\n observer.complete();\n }\n }\n }\n\n public addObserver(observer: Observer<T>) {\n if (!this.observers.has(observer)) {\n // Immediately deliver the most recent message, so we can always\n // be sure all observers have the latest information.\n this.deliverLastMessage(observer);\n this.observers.add(observer);\n }\n }\n\n public removeObserver(observer: Observer<T>) {\n if (\n this.observers.delete(observer) &&\n this.observers.size < 1\n ) {\n // In case there are still any listeners in this.nextResultListeners, and\n // no error or completion has been broadcast yet, make sure those\n // observers have a chance to run and then remove themselves from\n // this.observers.\n this.handlers.complete();\n }\n }\n\n // Any Concast object can be trivially converted to a Promise, without\n // having to create a new wrapper Observable. This promise provides an\n // easy way to observe the final state of the Concast.\n private resolve: (result?: T | PromiseLike<T>) => void;\n private reject: (reason: any) => void;\n public readonly promise = new Promise<T>((resolve, reject) => {\n this.resolve = resolve;\n this.reject = reject;\n });\n\n // Name and argument of the most recently invoked observer method, used\n // to deliver latest results immediately to new observers.\n private latest?: [\"next\", T] | [\"error\", any];\n\n // Bound handler functions that can be reused for every internal\n // subscription.\n private handlers = {\n next: (result: T) => {\n if (this.sub !== null) {\n this.latest = [\"next\", result];\n this.notify(\"next\", result);\n iterateObserversSafely(this.observers, \"next\", result);\n }\n },\n\n error: (error: any) => {\n const { sub } = this;\n if (sub !== null) {\n // Delay unsubscribing from the underlying subscription slightly,\n // so that immediately subscribing another observer can keep the\n // subscription active.\n if (sub) setTimeout(() => sub.unsubscribe());\n this.sub = null;\n this.latest = [\"error\", error];\n this.reject(error);\n this.notify(\"error\", error);\n iterateObserversSafely(this.observers, \"error\", error);\n }\n },\n\n complete: () => {\n const { sub } = this;\n if (sub !== null) {\n const value = this.sources.shift();\n if (!value) {\n if (sub) setTimeout(() => sub.unsubscribe());\n this.sub = null;\n if (this.latest &&\n this.latest[0] === \"next\") {\n this.resolve(this.latest[1]);\n } else {\n this.resolve();\n }\n this.notify(\"complete\");\n // We do not store this.latest = [\"complete\"], because doing so\n // discards useful information about the previous next (or\n // error) message. Instead, if new observers subscribe after\n // this Concast has completed, they will receive the final\n // 'next' message (unless there was an error) immediately\n // followed by a 'complete' message (see addObserver).\n iterateObserversSafely(this.observers, \"complete\");\n } else if (isPromiseLike(value)) {\n value.then(obs => this.sub = obs.subscribe(this.handlers));\n } else {\n this.sub = value.subscribe(this.handlers);\n }\n }\n },\n };\n\n private nextResultListeners = new Set<NextResultListener>();\n\n private notify(\n method: Parameters<NextResultListener>[0],\n arg?: Parameters<NextResultListener>[1],\n ) {\n const { nextResultListeners } = this;\n if (nextResultListeners.size) {\n // Replacing this.nextResultListeners first ensures it does not grow while\n // we are iterating over it, potentially leading to infinite loops.\n this.nextResultListeners = new Set;\n nextResultListeners.forEach(listener => listener(method, arg));\n }\n }\n\n // We need a way to run callbacks just *before* the next result (or error or\n // completion) is delivered by this Concast, so we can be sure any code that\n // runs as a result of delivering that result/error observes the effects of\n // running the callback(s). It was tempting to reuse the Observer type instead\n // of introducing NextResultListener, but that messes with the sizing and\n // maintenance of this.observers, and ends up being more code overall.\n beforeNext(callback: NextResultListener) {\n let called = false;\n this.nextResultListeners.add((method, arg) => {\n if (!called) {\n called = true;\n callback(method, arg);\n }\n });\n }\n\n // A public way to abort observation and broadcast.\n public cancel = (reason: any) => {\n this.reject(reason);\n this.sources = [];\n this.handlers.complete();\n }\n}\n\ntype NextResultListener = (\n method: \"next\" | \"error\" | \"complete\",\n arg?: any,\n) => any;\n\n// Necessary because the Concast constructor has a different signature\n// than the Observable constructor.\nfixObservableSubclass(Concast);\n"]}
\No newline at end of file