@@ -19,13 +19,6 @@ const _row = <R>(row: Partial<RowType<R>>): RowType<R> => {
1919 return { ...row , _id : row . _id || _uid ( ) , _observe : row . _observe || _random ( ) } as any
2020}
2121
22- let doDispatch = true
23- export const noDispatch = ( cb : Function ) => {
24- doDispatch = false
25- cb ( )
26- doDispatch = true
27- }
28-
2922export const createState = < Row extends object , MetaProps extends object = { } > ( ) => {
3023
3124 const factory = {
@@ -44,18 +37,15 @@ export const createState = <Row extends object, MetaProps extends object = {}>()
4437 cache : new Map < string , RowType < Row > [ ] > ( )
4538 }
4639
47- const _dispatch = ( type : StateDataType ) => {
40+ const fire = ( type : StateDataType ) => {
4841 factory . observe [ type ] = Math . random ( )
49-
50- if ( doDispatch ) {
51- factory . dispatches [ type ] . forEach ( ( cb , key ) => {
52- try {
53- cb ( )
54- } catch ( _err ) {
55- factory . dispatches [ type ] . delete ( key )
56- }
57- } )
58- }
42+ factory . dispatches [ type ] . forEach ( ( cb , key ) => {
43+ try {
44+ cb ( )
45+ } catch ( _err ) {
46+ factory . dispatches [ type ] . delete ( key )
47+ }
48+ } )
5949 }
6050
6151 const useHook = ( type : StateDataType ) => {
@@ -71,87 +61,79 @@ export const createState = <Row extends object, MetaProps extends object = {}>()
7161
7262 abstract class StateHandler {
7363
74- static create ( row : Row , dispatch ?: boolean ) : RowType < Row > {
75- dispatch ??= true
64+ static create ( row : Row , freeze ?: boolean ) : RowType < Row > {
7665 const r = _row < Row > ( row as any )
7766 factory . data . state . push ( r )
78- if ( dispatch ) {
79- _dispatch ( "state" )
67+ if ( ! freeze ) {
68+ fire ( "state" )
8069 }
8170 return r
8271 }
8372
84- static createMany ( rows : Row [ ] , dispatch ?: boolean ) : RowType < Row > [ ] {
85- dispatch ??= true
73+ static createMany ( rows : Row [ ] , freeze ?: boolean ) : RowType < Row > [ ] {
8674
8775 const rs = [ ]
8876 for ( let row of rows ) {
8977 const r = _row < Row > ( row )
9078 factory . data . state . push ( r )
9179 rs . push ( r )
9280 }
93- if ( dispatch ) {
94- _dispatch ( "state" )
81+ if ( ! freeze ) {
82+ fire ( "state" )
9583 }
9684 return rs
9785 }
9886
99- static update ( row : Partial < Row > , where : WhereType < Row > , dispatch ?: boolean ) {
100- dispatch ??= true
87+ static update ( row : Partial < Row > , where : WhereType < Row > , freeze ?: boolean ) {
10188 Finder ( factory . data . state , where , {
10289 getRow : ( r , index ) => {
10390 factory . data . state [ index ] = _row < Row > ( { ...r , ...row } )
10491 }
10592 } )
10693
107- if ( dispatch ) {
108- _dispatch ( "state" )
94+ if ( ! freeze ) {
95+ fire ( "state" )
10996 }
11097 }
11198
112- static updateAll ( row : Partial < Row > , dispatch ?: boolean ) {
113- dispatch ??= true
99+ static updateAll ( row : Partial < Row > , freeze ?: boolean ) {
114100
115101 for ( let i = 0 ; i < factory . data . state . length ; i ++ ) {
116102 factory . data . state [ i ] = _row < Row > ( { ...factory . data . state [ i ] , ...row } )
117103 }
118- if ( dispatch ) {
119- _dispatch ( "state" )
104+ if ( ! freeze ) {
105+ fire ( "state" )
120106 }
121107 }
122108
123- static delete ( where : WhereType < Row > , dispatch ?: boolean ) {
124- dispatch ??= true
109+ static delete ( where : WhereType < Row > , freeze ?: boolean ) {
125110 const found = Finder ( factory . data . state , where )
126111 factory . data . state = factory . data . state . filter ( ( row ) => ! found . ids . includes ( row . _id ) )
127112
128- if ( dispatch ) {
129- _dispatch ( "state" )
113+ if ( ! freeze ) {
114+ fire ( "state" )
130115 }
131116 }
132- static move ( oldIdx : number , newIdx : number , dispatch ?: boolean ) {
133- dispatch ??= true
117+ static move ( oldIdx : number , newIdx : number , freeze ?: boolean ) {
134118 const row : any = factory . data . state [ oldIdx ]
135119 if ( row ) {
136120 factory . data . state . splice ( oldIdx , 1 )
137121 factory . data . state . splice ( newIdx , 0 , _row ( row ) )
138- if ( dispatch ) {
139- _dispatch ( "state" )
122+ if ( ! freeze ) {
123+ fire ( "state" )
140124 }
141125 }
142126 }
143- static clearAll ( dispatch ?: boolean ) {
144- dispatch ??= true
127+ static clearAll ( freeze ?: boolean ) {
145128 factory . data . state = [ ]
146- if ( dispatch ) {
147- _dispatch ( "state" )
129+ if ( ! freeze ) {
130+ fire ( "state" )
148131 }
149132 }
150133
151134 static getAll ( args ?: ArgsType < Row > ) {
152135 try {
153- let detect = args ?. detect ?? true
154- if ( detect ) {
136+ if ( ! args ?. freeze ) {
155137 useHook ( "state" )
156138 }
157139 const cacheKey = factory . observe . state . toString ( ) + ( args ?. skip || "" ) + ( args ?. take || "" )
@@ -169,8 +151,7 @@ export const createState = <Row extends object, MetaProps extends object = {}>()
169151
170152 static find ( where : WhereType < Row > , args ?: ArgsType < Row > ) : RowType < Row > [ ] {
171153 try {
172- let detect = args ?. detect ?? true
173- if ( detect ) {
154+ if ( ! args ?. freeze ) {
174155 useHook ( "state" )
175156 }
176157 const cacheKey = factory . observe . state . toString ( ) + ( args ?. skip || "" ) + ( args ?. take || "" ) + JSON . stringify ( where )
@@ -186,26 +167,24 @@ export const createState = <Row extends object, MetaProps extends object = {}>()
186167 }
187168 }
188169
189- static findFirst ( where : WhereType < Row > , detect ?: boolean ) {
190- return StateHandler . find ( where , { detect } ) [ 0 ]
170+ static findFirst ( where : WhereType < Row > , freeze ?: boolean ) {
171+ return StateHandler . find ( where , { freeze } ) [ 0 ]
191172 }
192173
193- static findById ( _id : string , detect ?: boolean ) {
194- return StateHandler . findFirst ( { _id } , detect )
174+ static findById ( _id : string , freeze ?: boolean ) {
175+ return StateHandler . findFirst ( { _id } , freeze )
195176 }
196177
197- static setMeta < T extends keyof MetaProps > ( key : T , value : MetaProps [ T ] , dispatch ?: boolean ) {
178+ static setMeta < T extends keyof MetaProps > ( key : T , value : MetaProps [ T ] , freeze ?: boolean ) {
198179 factory . data . meta . set ( key , value )
199- dispatch ??= true
200- if ( dispatch ) {
201- _dispatch ( "meta" )
180+ if ( ! freeze ) {
181+ fire ( "meta" )
202182 }
203183 }
204184
205- static getMeta < T extends keyof MetaProps > ( key : T , detect ?: boolean ) : MetaProps [ T ] {
185+ static getMeta < T extends keyof MetaProps > ( key : T , freeze ?: boolean ) : MetaProps [ T ] {
206186 try {
207- detect ??= true
208- if ( detect ) {
187+ if ( ! freeze ) {
209188 useHook ( "meta" )
210189 }
211190 return factory . data . meta . get ( key )
@@ -214,10 +193,9 @@ export const createState = <Row extends object, MetaProps extends object = {}>()
214193 }
215194 }
216195
217- static getAllMeta ( detect ?: boolean ) : MetaProps {
218- detect ??= true
196+ static getAllMeta ( freeze ?: boolean ) : MetaProps {
219197 try {
220- if ( detect ) {
198+ if ( ! freeze ) {
221199 useHook ( "meta" )
222200 }
223201 return Object . fromEntries ( factory . data . meta ) as MetaProps
@@ -226,19 +204,17 @@ export const createState = <Row extends object, MetaProps extends object = {}>()
226204 }
227205 }
228206
229- static deleteMeta < T extends keyof MetaProps > ( key : T , dispatch ?: boolean ) {
207+ static deleteMeta < T extends keyof MetaProps > ( key : T , freeze ?: boolean ) {
230208 factory . data . meta . delete ( key )
231- dispatch ??= true
232- if ( dispatch ) {
233- _dispatch ( "meta" )
209+ if ( ! freeze ) {
210+ fire ( "meta" )
234211 }
235212 }
236213
237- static clearMeta ( dispatch ?: boolean ) {
214+ static clearMeta ( freeze ?: boolean ) {
238215 factory . data . meta . clear ( )
239- dispatch ??= true
240- if ( dispatch ) {
241- _dispatch ( "meta" )
216+ if ( ! freeze ) {
217+ fire ( "meta" )
242218 }
243219 }
244220 }
0 commit comments