File tree Expand file tree Collapse file tree 2 files changed +25
-0
lines changed
libs/angular-three/src/lib/di Expand file tree Collapse file tree 2 files changed +25
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments