1
1
"use client"
2
- import { useEffect , useId , useMemo , useState , createElement , Fragment , Component } from 'react'
2
+ import { useEffect , useId , useState , createElement , Fragment , Component } from 'react'
3
3
import { ArgsType , IStateHandler , RowType , StateDataType , WhereType } from './types' ;
4
4
import Finder , { isOb } from './Finder' ;
5
5
export * from './types'
@@ -40,7 +40,8 @@ export const createState = <Row extends object, MetaProps extends object = {}>()
40
40
observe : {
41
41
state : Math . random ( ) ,
42
42
meta : Math . random ( )
43
- }
43
+ } ,
44
+ cache : new Map < string , RowType < Row > [ ] > ( )
44
45
}
45
46
46
47
const _dispatch = ( type : StateDataType ) => {
@@ -70,21 +71,25 @@ export const createState = <Row extends object, MetaProps extends object = {}>()
70
71
71
72
abstract class StateHandler {
72
73
73
- static create ( row : Row ) : RowType < Row > {
74
+ static create ( row : Row , args ?: ArgsType < Row > ) : RowType < Row > {
74
75
const r = _row < Row > ( row as any )
75
76
factory . data . state . push ( r )
76
- _dispatch ( "state" )
77
+ if ( ! args ?. noDispatch ) {
78
+ _dispatch ( "state" )
79
+ }
77
80
return r
78
81
}
79
82
80
- static createMany ( rows : Row [ ] ) : RowType < Row > [ ] {
83
+ static createMany ( rows : Row [ ] , args ?: ArgsType < Row > ) : RowType < Row > [ ] {
81
84
const rs = [ ]
82
85
for ( let row of rows ) {
83
86
const r = _row < Row > ( row )
84
87
factory . data . state . push ( r )
85
88
rs . push ( r )
86
89
}
87
- _dispatch ( "state" )
90
+ if ( ! args ?. noDispatch ) {
91
+ _dispatch ( "state" )
92
+ }
88
93
return rs
89
94
}
90
95
@@ -96,20 +101,28 @@ export const createState = <Row extends object, MetaProps extends object = {}>()
96
101
factory . data . state [ index ] = _row < Row > ( { ...r , ...row } )
97
102
}
98
103
} )
99
- _dispatch ( "state" )
104
+
105
+ if ( ! args ?. noDispatch ) {
106
+ _dispatch ( "state" )
107
+ }
100
108
}
101
109
102
- static updateAll ( row : Partial < Row > ) {
110
+ static updateAll ( row : Partial < Row > , args ?: ArgsType < Row > ) {
103
111
for ( let i = 0 ; i < factory . data . state . length ; i ++ ) {
104
112
factory . data . state [ i ] = _row < Row > ( { ...factory . data . state [ i ] , ...row } )
105
113
}
106
- _dispatch ( "state" )
114
+ if ( ! args ?. noDispatch ) {
115
+ _dispatch ( "state" )
116
+ }
107
117
}
108
118
109
119
static delete ( where : WhereType < Row > , args ?: ArgsType < Row > ) {
110
120
const found = Finder ( factory . data . state , where , args )
111
121
factory . data . state = factory . data . state . filter ( ( row ) => ! found . ids . includes ( row . _id ) )
112
- _dispatch ( "state" )
122
+
123
+ if ( ! args ?. noDispatch ) {
124
+ _dispatch ( "state" )
125
+ }
113
126
}
114
127
115
128
static clearAll ( ) {
@@ -119,17 +132,36 @@ export const createState = <Row extends object, MetaProps extends object = {}>()
119
132
120
133
static getAll ( args ?: ArgsType < Row > ) {
121
134
try {
122
- useHook ( "state" )
123
- return useMemo ( ( ) => Finder ( factory . data . state , null , args ) . rows , [ factory . observe . state ] )
135
+ if ( ! args ?. noDispatch ) {
136
+ useHook ( "state" )
137
+ }
138
+ const cacheKey = factory . observe . state . toString ( ) + ( args ?. skip || "" ) + ( args ?. take || "" )
139
+ const items = factory . cache . get ( cacheKey )
140
+ if ( items ?. length ) {
141
+ return items
142
+ }
143
+ const rows = Finder ( factory . data . state , null , args ) . rows
144
+ factory . cache . set ( cacheKey , rows )
145
+ return rows
124
146
} catch ( error ) {
125
147
return Finder ( factory . data . state , null , args )
126
148
}
127
149
}
128
150
129
151
static find ( where : WhereType < Row > , args ?: ArgsType < Row > ) : RowType < Row > [ ] {
130
152
try {
131
- useHook ( "state" )
132
- return useMemo ( ( ) => Finder ( factory . data . state , where , args ) . rows , [ factory . observe . state ] )
153
+
154
+ if ( ! args ?. noDispatch ) {
155
+ useHook ( "state" )
156
+ }
157
+ const cacheKey = factory . observe . state . toString ( ) + ( args ?. skip || "" ) + ( args ?. take || "" ) + JSON . stringify ( where )
158
+ const items = factory . cache . get ( cacheKey )
159
+ if ( items ?. length ) {
160
+ return items
161
+ }
162
+ const rows = Finder ( factory . data . state , where , args ) . rows
163
+ factory . cache . set ( cacheKey , rows )
164
+ return rows
133
165
} catch ( error ) {
134
166
return Finder ( factory . data . state , where , args ) . rows
135
167
}
@@ -169,7 +201,7 @@ export const createState = <Row extends object, MetaProps extends object = {}>()
169
201
static getAllMeta ( ) : MetaProps {
170
202
try {
171
203
useHook ( "meta" )
172
- return useMemo ( ( ) => Object . fromEntries ( factory . data . meta ) as MetaProps , [ factory . observe . meta ] )
204
+ return Object . fromEntries ( factory . data . meta ) as MetaProps
173
205
} catch ( error ) {
174
206
return Object . fromEntries ( factory . data . meta ) as MetaProps
175
207
}
0 commit comments