UNPKG

@itwin/presentation-hierarchies

Version:

A package for creating hierarchies based on data in iTwin.js iModels.

50 lines 2.47 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ import { asapScheduler, connectable, defer, EMPTY, finalize, iif, mergeMap, observeOn, onErrorResumeNextWith, queueScheduler, Subject, subscribeOn, tap, } from "rxjs"; /** @internal */ export class SubscriptionScheduler { _scheduler = new Subject(); constructor(concurrency) { this._scheduler .pipe(mergeMap((sourceObservable) => { return sourceObservable.pipe( // connect source observable when scheduler subscribes tap({ subscribe: () => { sourceObservable.connect(); }, }), // Guard against stack overflow when a lot of observables are scheduled. Without this operation `mergeMap` // will process each observable that is present in the pipeline recursively. observeOn(queueScheduler), // Delay the connection until another event loop task subscribeOn(asapScheduler), // Ignore errors in this pipeline without suppressing them for other subscribers onErrorResumeNextWith()); }, concurrency)) // Start consuming scheduled observables .subscribe(); } /** * Schedules `source` for subscription in the current scheduler. * * The actual scheduling is performed when the returned observable is subscribed to. To cancel, remove all subscribers * from the returned observable. * * @param source Input observable for which to schedule a subscription. * @returns Hot observable which starts emitting `source` values after subscription. */ scheduleSubscription(source) { return defer(() => { let unsubscribed = false; const connectableObservable = connectable(iif(() => unsubscribed, EMPTY, source), { connector: () => new Subject(), resetOnDisconnect: false }); this._scheduler.next(connectableObservable); return connectableObservable.pipe(finalize(() => { unsubscribed = true; })); }); } } //# sourceMappingURL=SubscriptionScheduler.js.map