Skip to content

Commit 5dab828

Browse files
committed
feat: add safe-detect-changes function
1 parent 01d5fb4 commit 5dab828

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

libs/angular-three/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ export { createAttachFunction } from './lib/utils/attach';
1717
export * from './lib/utils/instance';
1818
export * from './lib/utils/is';
1919
export * from './lib/utils/make';
20+
export * from './lib/utils/safe-detect-changes';
2021
export * from './lib/utils/update';

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import {
1111
switchMap,
1212
takeUntil,
1313
} from 'rxjs';
14-
import type { NgtAnyRecord, NgtInstanceNode } from '../types';
14+
import type { NgtInstanceNode } from '../types';
1515
import { getLocalState } from '../utils/instance';
1616
import { is } from '../utils/is';
17+
import { safeDetectChanges } from '../utils/safe-detect-changes';
1718
import { injectNgtDestroy } from './destroy';
1819

1920
type Subscribe<T> = (callback: (current: T, previous: T | null) => void) => Subscription;
@@ -102,9 +103,7 @@ export function injectNgtRef<T>(initialValue: NgtInjectedRef<T> | (T | null) = n
102103
// during creation phase, 'context' on ViewRef will be null
103104
// we check the "context" to avoid running detectChanges during this phase.
104105
// because there's nothing to check
105-
if ((cd as NgtAnyRecord)['context']) {
106-
cd.detectChanges();
107-
}
106+
safeDetectChanges(cd);
108107
}
109108
}
110109
},
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { ChangeDetectorRef } from '@angular/core';
2+
import { NgtAnyRecord } from '../types';
3+
4+
export function safeDetectChanges(cdr: ChangeDetectorRef) {
5+
try {
6+
if ((cdr as NgtAnyRecord)['context']) {
7+
cdr.detectChanges();
8+
}
9+
} catch (e) {
10+
cdr.markForCheck();
11+
}
12+
}

0 commit comments

Comments
 (0)