-
Notifications
You must be signed in to change notification settings - Fork 24.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): implement
takeUntilDestroyed
in rxjs-interop (#49154)
This commit implements an RxJS operator `takeUntilDestroyed` which terminates an Observable when the current context (component, directive, etc) is destroyed. `takeUntilDestroyed` will inject the current `DestroyRef` if none is provided, or use one provided as an argument. PR Close #49154
- Loading branch information
Showing
4 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* @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 {MonoTypeOperatorFunction, 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. Otherwise, the | ||
* current `DestroyRef` is injected. | ||
* | ||
* @developerPreview | ||
*/ | ||
export function takeUntilDestroyed<T>(destroyRef?: DestroyRef): MonoTypeOperatorFunction<T> { | ||
if (!destroyRef) { | ||
assertInInjectionContext(takeUntilDestroyed); | ||
destroyRef = inject(DestroyRef); | ||
} | ||
|
||
const destroyed$ = new Observable<void>(observer => { | ||
destroyRef!.onDestroy(observer.next.bind(observer)); | ||
}); | ||
|
||
return <T>(source: Observable<T>) => { | ||
return source.pipe(takeUntil(destroyed$)); | ||
}; | ||
} |
66 changes: 66 additions & 0 deletions
66
packages/core/rxjs-interop/test/take_until_destroyed_spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* @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 {DestroyRef, EnvironmentInjector, Injector, runInInjectionContext} from '@angular/core'; | ||
import {BehaviorSubject} from 'rxjs'; | ||
|
||
import {takeUntilDestroyed} from '../src/take_until_destroyed'; | ||
|
||
describe('takeUntilDestroyed', () => { | ||
it('should complete an observable when the current context is destroyed', () => { | ||
const injector = Injector.create({providers: []}) as EnvironmentInjector; | ||
const source$ = new BehaviorSubject(0); | ||
const tied$ = runInInjectionContext(injector, () => source$.pipe(takeUntilDestroyed())); | ||
|
||
let completed = false; | ||
let last = 0; | ||
|
||
tied$.subscribe({ | ||
next(value) { | ||
last = value; | ||
}, | ||
complete() { | ||
completed = true; | ||
} | ||
}); | ||
|
||
source$.next(1); | ||
expect(last).toBe(1); | ||
|
||
injector.destroy(); | ||
expect(completed).toBeTrue(); | ||
source$.next(2); | ||
expect(last).toBe(1); | ||
}); | ||
|
||
it('should allow a manual DestroyRef to be passed', () => { | ||
const injector = Injector.create({providers: []}) as EnvironmentInjector; | ||
const source$ = new BehaviorSubject(0); | ||
const tied$ = source$.pipe(takeUntilDestroyed(injector.get(DestroyRef))); | ||
|
||
let completed = false; | ||
let last = 0; | ||
|
||
tied$.subscribe({ | ||
next(value) { | ||
last = value; | ||
}, | ||
complete() { | ||
completed = true; | ||
} | ||
}); | ||
|
||
source$.next(1); | ||
expect(last).toBe(1); | ||
|
||
injector.destroy(); | ||
expect(completed).toBeTrue(); | ||
source$.next(2); | ||
expect(last).toBe(1); | ||
}); | ||
}); |