1- import { untracked } from '@angular/core' ;
1+ import { signal , untracked } from '@angular/core' ;
22import { NgtSignalStore } from '../stores/signal.store' ;
33import type { NgtAnyRecord , NgtInstanceLocalState , NgtInstanceNode } from '../types' ;
4- import { createSignal } from './signal' ;
54import { checkUpdate } from './update' ;
65
76export function getLocalState < TInstance extends object = NgtAnyRecord > (
@@ -25,37 +24,41 @@ export function prepare<TInstance extends object = NgtAnyRecord>(
2524
2625 if ( localState ?. primitive || ! instance . __ngt__ ) {
2726 const {
28- objects = createSignal < NgtInstanceNode [ ] > ( [ ] ) ,
29- nonObjects = createSignal < NgtInstanceNode [ ] > ( [ ] ) ,
27+ objects = signal < NgtInstanceNode [ ] > ( [ ] ) ,
28+ nonObjects = signal < NgtInstanceNode [ ] > ( [ ] ) ,
3029 ...rest
3130 } = localState || { } ;
3231
3332 instance . __ngt__ = {
3433 previousAttach : null ,
3534 store : null ,
36- parent : createSignal ( null ) ,
35+ parent : signal ( null ) ,
3736 memoized : { } ,
3837 eventCount : 0 ,
3938 handlers : { } ,
4039 objects,
4140 nonObjects,
4241 nativeProps : new NgtSignalStore ( { } ) ,
4342 add : ( object , type ) => {
44- const current = untracked ( instance . __ngt__ [ type ] ) ;
45- const foundIndex = current . indexOf ( ( obj : NgtInstanceNode ) => obj === object ) ;
46- if ( foundIndex > - 1 ) {
47- // if we add an object with the same reference, then we switch it out
48- // and update the BehaviorSubject
49- current . splice ( foundIndex , 1 , object ) ;
50- instance . __ngt__ [ type ] . set ( current ) ;
51- } else {
52- instance . __ngt__ [ type ] . update ( ( prev ) => [ ...prev , object ] ) ;
53- }
54- notifyAncestors ( untracked ( instance . __ngt__ . parent ) ) ;
43+ queueMicrotask ( ( ) => {
44+ const current = untracked ( instance . __ngt__ [ type ] ) ;
45+ const foundIndex = current . indexOf ( ( obj : NgtInstanceNode ) => obj === object ) ;
46+ if ( foundIndex > - 1 ) {
47+ // if we add an object with the same reference, then we switch it out
48+ // and update the BehaviorSubject
49+ current . splice ( foundIndex , 1 , object ) ;
50+ instance . __ngt__ [ type ] . set ( current ) ;
51+ } else {
52+ instance . __ngt__ [ type ] . update ( ( prev ) => [ ...prev , object ] ) ;
53+ }
54+ notifyAncestors ( untracked ( instance . __ngt__ . parent ) ) ;
55+ } ) ;
5556 } ,
5657 remove : ( object , type ) => {
57- instance . __ngt__ [ type ] . update ( ( prev ) => prev . filter ( ( o ) => o !== object ) ) ;
58- notifyAncestors ( untracked ( instance . __ngt__ . parent ) ) ;
58+ queueMicrotask ( ( ) => {
59+ instance . __ngt__ [ type ] . update ( ( prev ) => prev . filter ( ( o ) => o !== object ) ) ;
60+ notifyAncestors ( untracked ( instance . __ngt__ . parent ) ) ;
61+ } ) ;
5962 } ,
6063 ...rest ,
6164 } as NgtInstanceLocalState ;
0 commit comments