/** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ import { assertInInjectionContext, DestroyRef, inject } from '@angular/core'; import { Observable } from 'rxjs'; import { takeUntil } from 'rxjs/operators'; /** * Operator which completes the Observable when the calling context (component, directive, service, * etc) is destroyed. * * @param destroyRef optionally, the `DestroyRef` representing the current context. This can be * passed explicitly to use `takeUntilDestroyed` outside of an [injection * context](guide/dependency-injection-context). Otherwise, the current `DestroyRef` is injected. * * @developerPreview */ export function takeUntilDestroyed(destroyRef) { if (!destroyRef) { assertInInjectionContext(takeUntilDestroyed); destroyRef = inject(DestroyRef); } const destroyed$ = new Observable(observer => { const unregisterFn = destroyRef.onDestroy(observer.next.bind(observer)); return unregisterFn; }); return (source) => { return source.pipe(takeUntil(destroyed$)); }; } //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFrZV91bnRpbF9kZXN0cm95ZWQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi8uLi8uLi9wYWNrYWdlcy9jb3JlL3J4anMtaW50ZXJvcC9zcmMvdGFrZV91bnRpbF9kZXN0cm95ZWQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7Ozs7OztHQU1HO0FBRUgsT0FBTyxFQUFDLHdCQUF3QixFQUFFLFVBQVUsRUFBRSxNQUFNLEVBQUMsTUFBTSxlQUFlLENBQUM7QUFDM0UsT0FBTyxFQUEyQixVQUFVLEVBQUMsTUFBTSxNQUFNLENBQUM7QUFDMUQsT0FBTyxFQUFDLFNBQVMsRUFBQyxNQUFNLGdCQUFnQixDQUFDO0FBRXpDOzs7Ozs7Ozs7R0FTRztBQUNILE1BQU0sVUFBVSxrQkFBa0IsQ0FBSSxVQUF1QjtJQUMzRCxJQUFJLENBQUMsVUFBVSxFQUFFO1FBQ2Ysd0JBQXdCLENBQUMsa0JBQWtCLENBQUMs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