UNPKG

@itwin/presentation-hierarchies

Version:

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

57 lines 2.69 kB
"use strict"; /*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ Object.defineProperty(exports, "__esModule", { value: true }); exports.SubscriptionScheduler = void 0; const rxjs_1 = require("rxjs"); /** @internal */ class SubscriptionScheduler { _scheduler = new rxjs_1.Subject(); constructor(concurrency) { this._scheduler .pipe((0, rxjs_1.mergeMap)((sourceObservable) => { return sourceObservable.pipe( // connect source observable when scheduler subscribes (0, rxjs_1.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. (0, rxjs_1.observeOn)(rxjs_1.queueScheduler), // Delay the connection until another event loop task (0, rxjs_1.subscribeOn)(rxjs_1.asapScheduler), // Ignore errors in this pipeline without suppressing them for other subscribers (0, rxjs_1.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 (0, rxjs_1.defer)(() => { let unsubscribed = false; const connectableObservable = (0, rxjs_1.connectable)((0, rxjs_1.iif)(() => unsubscribed, rxjs_1.EMPTY, source), { connector: () => new rxjs_1.Subject(), resetOnDisconnect: false, }); this._scheduler.next(connectableObservable); return connectableObservable.pipe((0, rxjs_1.finalize)(() => { unsubscribed = true; })); }); } } exports.SubscriptionScheduler = SubscriptionScheduler; //# sourceMappingURL=SubscriptionScheduler.js.map