Skip to content

Commit 1ff55c3

Browse files
committed
feat: add destry injection fn
1 parent 5f3334a commit 1ff55c3

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { ChangeDetectorRef, inject, ViewRef } from '@angular/core';
2+
import { Observable, ReplaySubject } from 'rxjs';
3+
4+
/**
5+
* A utility injection fn that can be used in other injection fn to provide the destroy capability.
6+
*/
7+
export function injectNgtDestroy(cb?: () => void): { destroy$: Observable<void>; cdr: ChangeDetectorRef } {
8+
try {
9+
const cdr = inject(ChangeDetectorRef);
10+
const destroy$ = new ReplaySubject<void>();
11+
12+
queueMicrotask(() => {
13+
(cdr as ViewRef).onDestroy(() => {
14+
destroy$.next();
15+
destroy$.complete();
16+
cb?.();
17+
});
18+
});
19+
20+
return { destroy$, cdr };
21+
} catch (e) {
22+
console.warn(`[NGT] injectNgtDestroy is being called outside of Constructor Context`);
23+
return {} as ReturnType<typeof injectNgtDestroy>;
24+
}
25+
}

libs/angular-three/src/lib/di/ref.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)