@@ -104,15 +104,29 @@ export function keyval<Input, ModelEnhance, Api, Shape>(
104104 isClone : boolean ,
105105 cloneOf : Keyval < any , any , any , any > | null ,
106106 ) => {
107+ type Enriched = Input & ModelEnhance ;
108+ type Output = {
109+ [ K in keyof ModelEnhance ] :
110+ | Store < ModelEnhance [ K ] >
111+ | Keyval < any , ModelEnhance [ K ] , any , any > ;
112+ } ;
113+ type KeyvalListState = ListState < Enriched , Output , Api > ;
114+ const $entities = createStore < KeyvalListState > ( {
115+ items : [ ] ,
116+ instances : [ ] ,
117+ keys : [ ] ,
118+ } ) ;
119+ const $items = $entities . map ( ( { items } ) => items ) ;
120+ const $keys = $entities . map ( ( { keys } ) => keys ) ;
107121 return lazyInit (
108122 {
109123 type : 'keyval' ,
110124 api : 0 ,
111125 __lens : 0 ,
112126 __struct : 0 ,
113- $items : 0 ,
114- $keys : 0 ,
115- __$listState : 0 ,
127+ $items,
128+ $keys,
129+ __$listState : $entities ,
116130 defaultState : ( ) => null as any ,
117131 edit : 0 ,
118132 editField : 0 ,
@@ -141,12 +155,7 @@ export function keyval<Input, ModelEnhance, Api, Shape>(
141155 create,
142156 } = options as Exclude < typeof options , Keyval < any , any , any , any > > ) ;
143157 }
144- type Enriched = Input & ModelEnhance ;
145- type Output = {
146- [ K in keyof ModelEnhance ] :
147- | Store < ModelEnhance [ K ] >
148- | Keyval < any , ModelEnhance [ K ] , any , any > ;
149- } ;
158+
150159 let kvModel :
151160 | Model <
152161 {
@@ -157,7 +166,7 @@ export function keyval<Input, ModelEnhance, Api, Shape>(
157166 Shape
158167 >
159168 | undefined ;
160- type KeyvalListState = ListState < Enriched , Output , Api > ;
169+
161170 if ( create ) {
162171 // @ts -expect-error typecast
163172 kvModel = model ( { create } ) ;
@@ -174,11 +183,6 @@ export function keyval<Input, ModelEnhance, Api, Shape>(
174183 ? null
175184 : getKeyRaw
176185 : kvModel . keyField ;
177- const $entities = createStore < KeyvalListState > ( {
178- items : [ ] ,
179- instances : [ ] ,
180- keys : [ ] ,
181- } ) ;
182186
183187 const api = createInstanceApi ( $entities , kvModel ) ;
184188 const editApi = createEditApi (
@@ -213,8 +217,8 @@ export function keyval<Input, ModelEnhance, Api, Shape>(
213217 // @ts -expect-error bad implementation
214218 __lens : shape ,
215219 __struct : structShape ,
216- $items : $entities . map ( ( { items } ) => items ) ,
217- $keys : $entities . map ( ( { keys } ) => keys ) ,
220+ $items,
221+ $keys,
218222 __$listState : $entities as any ,
219223 defaultState ( ) {
220224 return kvModel ?. defaultState ( ) ?? ( null as any ) ;
0 commit comments