@itwin/presentation-hierarchies
Version:
A package for creating hierarchies based on data in iTwin.js iModels.
60 lines • 2.25 kB
JavaScript
;
/*---------------------------------------------------------------------------------------------
* 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.partition = partition;
const rxjs_1 = require("rxjs");
const core_bentley_1 = require("@itwin/core-bentley");
/**
* This is similar to `rxjs` `partition` operator, but it subscribes to source observable only once.
* @internal
*/
function partition(source, predicate) {
const matches = new rxjs_1.ReplaySubject();
const nonMatches = new rxjs_1.ReplaySubject();
let sourceSubscription;
let sourceSubscriptionsCount = 0;
function subscribeToSource() {
if (sourceSubscriptionsCount++ > 0) {
return;
}
(0, core_bentley_1.assert)(!sourceSubscription);
sourceSubscription = (0, rxjs_1.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() {
(0, core_bentley_1.assert)(sourceSubscriptionsCount > 0);
if (--sourceSubscriptionsCount === 0) {
(0, core_bentley_1.assert)(!!sourceSubscription);
sourceSubscription.unsubscribe();
sourceSubscription = undefined;
}
}
return [matches, nonMatches].map((subject) => new rxjs_1.Observable((subscriber) => {
const subjectSubscription = subject.subscribe(subscriber);
subscribeToSource();
return () => {
subjectSubscription.unsubscribe();
unsubscribeFromSource();
};
}));
}
//# sourceMappingURL=Partition.js.map