"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.takeUntilWithFilter = void 0;
const rxjs_1 = require("rxjs");
/**
 * This operator is similar to takeUntil, but it also allows you to filter the source based on a predicate, and only
 * after that it subscribes to the notifier.
 *
 * @see {@link rxjs.takeUntil}
 */
function takeUntilWithFilter(predicate, notifier) {
    return (source) => new rxjs_1.Observable((destination) => {
        // we share so both the filter and the notifier are subscribed to the same source
        const sharedSource = source.pipe((0, rxjs_1.share)());
        sharedSource
            .pipe(
        // filter the source based on the predicate, not emitting if the predicate returns false
        (0, rxjs_1.filter)(predicate), 
        // now we subscribe to the source, and that's the difference between takeUntil and takeUntilWithFilter
        // For example, if notifier is a timer, we don't want to start the timer until the predicate is true
        (0, rxjs_1.mergeMap)(() => notifier))
            .subscribe((0, rxjs_1.operate)({
            destination,
            // now we complete the destination when the notifier emits
            next: () => destination.complete(),
            complete: rxjs_1.noop,
        }));
        !destination.closed && sharedSource.subscribe(destination);
    });
}
exports.takeUntilWithFilter = takeUntilWithFilter;
//# sourceMappingURL=takeUntilWithFilter.js.map