@@ -71,68 +71,87 @@ export const createState = <Row extends object, MetaProps extends object = {}>()
71
71
72
72
abstract class StateHandler {
73
73
74
- static create ( row : Row , args ?: ArgsType < Row > ) : RowType < Row > {
74
+ static create ( row : Row , dispatch ?: boolean ) : RowType < Row > {
75
+ dispatch ??= true
75
76
const r = _row < Row > ( row as any )
76
77
factory . data . state . push ( r )
77
- if ( ! args ?. noDispatch ) {
78
+ if ( dispatch ) {
78
79
_dispatch ( "state" )
79
80
}
80
81
return r
81
82
}
82
83
83
- static createMany ( rows : Row [ ] , args ?: ArgsType < Row > ) : RowType < Row > [ ] {
84
+ static createMany ( rows : Row [ ] , dispatch ?: boolean ) : RowType < Row > [ ] {
85
+ dispatch ??= true
86
+
84
87
const rs = [ ]
85
88
for ( let row of rows ) {
86
89
const r = _row < Row > ( row )
87
90
factory . data . state . push ( r )
88
91
rs . push ( r )
89
92
}
90
- if ( ! args ?. noDispatch ) {
93
+ if ( dispatch ) {
91
94
_dispatch ( "state" )
92
95
}
93
96
return rs
94
97
}
95
98
96
- static update ( row : Partial < Row > , where : WhereType < Row > , args ?: ArgsType < Row > ) {
99
+ static update ( row : Partial < Row > , where : WhereType < Row > , dispatch ?: boolean ) {
100
+ dispatch ??= true
97
101
Finder ( factory . data . state , where , {
98
- ...args ,
99
102
getRow : ( r , index ) => {
100
- args ?. getRow && args . getRow ( r , index )
101
103
factory . data . state [ index ] = _row < Row > ( { ...r , ...row } )
102
104
}
103
105
} )
104
106
105
- if ( ! args ?. noDispatch ) {
107
+ if ( dispatch ) {
106
108
_dispatch ( "state" )
107
109
}
108
110
}
109
111
110
- static updateAll ( row : Partial < Row > , args ?: ArgsType < Row > ) {
112
+ static updateAll ( row : Partial < Row > , dispatch ?: boolean ) {
113
+ dispatch ??= true
114
+
111
115
for ( let i = 0 ; i < factory . data . state . length ; i ++ ) {
112
116
factory . data . state [ i ] = _row < Row > ( { ...factory . data . state [ i ] , ...row } )
113
117
}
114
- if ( ! args ?. noDispatch ) {
118
+ if ( dispatch ) {
115
119
_dispatch ( "state" )
116
120
}
117
121
}
118
122
119
- static delete ( where : WhereType < Row > , args ?: ArgsType < Row > ) {
120
- const found = Finder ( factory . data . state , where , args )
123
+ static delete ( where : WhereType < Row > , dispatch ?: boolean ) {
124
+ dispatch ??= true
125
+ const found = Finder ( factory . data . state , where )
121
126
factory . data . state = factory . data . state . filter ( ( row ) => ! found . ids . includes ( row . _id ) )
122
127
123
- if ( ! args ?. noDispatch ) {
128
+ if ( dispatch ) {
124
129
_dispatch ( "state" )
125
130
}
126
131
}
127
-
128
- static clearAll ( ) {
132
+ static move ( oldIdx : number , newIdx : number , dispatch ?: boolean ) {
133
+ dispatch ??= true
134
+ const row : any = factory . data . state [ oldIdx ]
135
+ if ( row ) {
136
+ factory . data . state . splice ( oldIdx , 1 )
137
+ factory . data . state . splice ( newIdx , 0 , _row ( row ) )
138
+ if ( dispatch ) {
139
+ _dispatch ( "state" )
140
+ }
141
+ }
142
+ }
143
+ static clearAll ( dispatch ?: boolean ) {
144
+ dispatch ??= true
129
145
factory . data . state = [ ]
130
- _dispatch ( "state" )
146
+ if ( dispatch ) {
147
+ _dispatch ( "state" )
148
+ }
131
149
}
132
150
133
151
static getAll ( args ?: ArgsType < Row > ) {
134
152
try {
135
- if ( ! args ?. noDispatch ) {
153
+ let detect = args ?. detect ?? true
154
+ if ( detect ) {
136
155
useHook ( "state" )
137
156
}
138
157
const cacheKey = factory . observe . state . toString ( ) + ( args ?. skip || "" ) + ( args ?. take || "" )
@@ -150,8 +169,8 @@ export const createState = <Row extends object, MetaProps extends object = {}>()
150
169
151
170
static find ( where : WhereType < Row > , args ?: ArgsType < Row > ) : RowType < Row > [ ] {
152
171
try {
153
-
154
- if ( ! args ?. noDispatch ) {
172
+ let detect = args ?. detect ?? true
173
+ if ( detect ) {
155
174
useHook ( "state" )
156
175
}
157
176
const cacheKey = factory . observe . state . toString ( ) + ( args ?. skip || "" ) + ( args ?. take || "" ) + JSON . stringify ( where )
@@ -167,54 +186,60 @@ export const createState = <Row extends object, MetaProps extends object = {}>()
167
186
}
168
187
}
169
188
170
- static findFirst ( where : WhereType < Row > ) {
171
- return StateHandler . find ( where ) [ 0 ]
189
+ static findFirst ( where : WhereType < Row > , detect ?: boolean ) {
190
+ return StateHandler . find ( where , { detect } ) [ 0 ]
172
191
}
173
192
174
- static findById ( _id : string ) {
175
- return StateHandler . findFirst ( { _id } )
193
+ static findById ( _id : string , detect ?: boolean ) {
194
+ return StateHandler . findFirst ( { _id } , detect )
176
195
}
177
196
178
- static move ( oldIdx : number , newIdx : number ) {
179
- const row : any = factory . data . state [ oldIdx ]
180
- if ( row ) {
181
- factory . data . state . splice ( oldIdx , 1 )
182
- factory . data . state . splice ( newIdx , 0 , _row ( row ) )
183
- _dispatch ( "state" )
184
- }
185
- }
186
-
187
- static setMeta < T extends keyof MetaProps > ( key : T , value : MetaProps [ T ] ) {
197
+ static setMeta < T extends keyof MetaProps > ( key : T , value : MetaProps [ T ] , dispatch ?: boolean ) {
188
198
factory . data . meta . set ( key , value )
189
- _dispatch ( "meta" )
199
+ dispatch ??= true
200
+ if ( dispatch ) {
201
+ _dispatch ( "meta" )
202
+ }
190
203
}
191
204
192
- static getMeta < T extends keyof MetaProps > ( key : T , def ?: any ) : MetaProps [ T ] {
205
+ static getMeta < T extends keyof MetaProps > ( key : T , detect ?: boolean ) : MetaProps [ T ] {
193
206
try {
194
- useHook ( "meta" )
195
- return factory . data . meta . get ( key ) || def
207
+ detect ??= true
208
+ if ( detect ) {
209
+ useHook ( "meta" )
210
+ }
211
+ return factory . data . meta . get ( key )
196
212
} catch ( error ) {
197
- return factory . data . meta . get ( key ) || def
213
+ return factory . data . meta . get ( key )
198
214
}
199
215
}
200
216
201
- static getAllMeta ( ) : MetaProps {
217
+ static getAllMeta ( detect ?: boolean ) : MetaProps {
218
+ detect ??= true
202
219
try {
203
- useHook ( "meta" )
220
+ if ( detect ) {
221
+ useHook ( "meta" )
222
+ }
204
223
return Object . fromEntries ( factory . data . meta ) as MetaProps
205
224
} catch ( error ) {
206
225
return Object . fromEntries ( factory . data . meta ) as MetaProps
207
226
}
208
227
}
209
228
210
- static deleteMeta < T extends keyof MetaProps > ( key : T ) {
229
+ static deleteMeta < T extends keyof MetaProps > ( key : T , dispatch ?: boolean ) {
211
230
factory . data . meta . delete ( key )
212
- _dispatch ( "meta" )
231
+ dispatch ??= true
232
+ if ( dispatch ) {
233
+ _dispatch ( "meta" )
234
+ }
213
235
}
214
236
215
- static clearMeta ( ) {
237
+ static clearMeta ( dispatch ?: boolean ) {
216
238
factory . data . meta . clear ( )
217
- _dispatch ( "meta" )
239
+ dispatch ??= true
240
+ if ( dispatch ) {
241
+ _dispatch ( "meta" )
242
+ }
218
243
}
219
244
}
220
245
0 commit comments