UNPKG

@itwin/presentation-hierarchies

Version:

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

57 lines 2.07 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ import { from, Observable, ReplaySubject } from "rxjs"; import { assert } from "@itwin/core-bentley"; /** * This is similar to `rxjs` `partition` operator, but it subscribes to source observable only once. * @internal */ export function partition(source, predicate) { const matches = new ReplaySubject(); const nonMatches = new ReplaySubject(); let sourceSubscription; let sourceSubscriptionsCount = 0; function subscribeToSource() { if (sourceSubscriptionsCount++ > 0) { return; } assert(!sourceSubscription); sourceSubscription = from(source).subscribe({ next(value) { if (predicate(value)) { matches.next(value); } else { nonMatches.next(value); } }, error(error) { matches.error(error); nonMatches.error(error); }, complete() { matches.complete(); nonMatches.complete(); }, }); } function unsubscribeFromSource() { assert(sourceSubscriptionsCount > 0); if (--sourceSubscriptionsCount === 0) { assert(!!sourceSubscription); sourceSubscription.unsubscribe(); sourceSubscription = undefined; } } return [matches, nonMatches].map((subject) => new Observable((subscriber) => { const subjectSubscription = subject.subscribe(subscriber); subscribeToSource(); return () => { subjectSubscription.unsubscribe(); unsubscribeFromSource(); }; })); } //# sourceMappingURL=Partition.js.map