Skip to content

Commit 8163a89

Browse files
atscottkirjs
authored andcommitted
feat(core): Add destroyed property on DestroyRef (#61849)
Since `DestroyRef.onDestroy` throws if the `DestroyRef` is already destroyed, there is a need to be able to tell if it is already destroyed before attempting to register a callback. PR Close #61849
1 parent e947dfb commit 8163a89

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

goldens/public-api/core/index.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,7 @@ export function destroyPlatform(): void;
593593

594594
// @public
595595
export abstract class DestroyRef {
596+
abstract get destroyed(): boolean;
596597
abstract onDestroy(callback: () => void): () => void;
597598
}
598599

packages/core/rxjs-interop/src/take_until_destroyed.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ export function takeUntilDestroyed<T>(destroyRef?: DestroyRef): MonoTypeOperator
2727
}
2828

2929
const destroyed$ = new Observable<void>((subscriber) => {
30-
if ((destroyRef as unknown as {destroyed: boolean}).destroyed) {
30+
if (destroyRef.destroyed) {
3131
subscriber.next();
3232
return;
3333
}
34-
const unregisterFn = destroyRef!.onDestroy(subscriber.next.bind(subscriber));
34+
const unregisterFn = destroyRef.onDestroy(subscriber.next.bind(subscriber));
3535
return unregisterFn;
3636
});
3737

packages/core/src/linker/destroy_ref.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ export abstract class DestroyRef {
4646
*/
4747
abstract onDestroy(callback: () => void): () => void;
4848

49-
/** @internal */
49+
/**
50+
* Indicates whether the instance has already been destroyed and whether its `onDestroy` callbacks have executed.
51+
*/
5052
abstract get destroyed(): boolean;
5153

5254
/**

0 commit comments

Comments
 (0)