@@ -160,11 +160,10 @@ export function keyval<Input, ModelEnhance, Api, Shape>(
160160 // @ts -expect-error bad implementation
161161 let getKeyRaw ;
162162 let shape : Shape ;
163- let props ;
164163 if ( typeof options === 'function' ) {
165164 create = options as any ;
166165 } else {
167- ( { key : getKeyRaw , shape = { } as Shape , props , create } = options ) ;
166+ ( { key : getKeyRaw , shape = { } as Shape , create } = options ) ;
168167 }
169168 type Enriched = Input & ModelEnhance ;
170169 let kvModel :
@@ -181,20 +180,22 @@ export function keyval<Input, ModelEnhance, Api, Shape>(
181180 Shape
182181 >
183182 | undefined ;
184- if ( props && create ) {
183+ if ( create ) {
185184 // @ts -expect-error typecast
186- kvModel = model ( { props , create } ) ;
185+ kvModel = model ( { create } ) ;
187186 }
188187 type ListState = {
189188 items : Enriched [ ] ;
189+ // @ts -expect-error type mismatch
190190 instances : Array < InstanceOf < NonNullable < typeof kvModel > > > ;
191191 keys : Array < string | number > ;
192192 } ;
193- const getKey =
194- typeof getKeyRaw === 'function'
193+ const getKey = ! kvModel
194+ ? typeof getKeyRaw === 'function'
195195 ? getKeyRaw
196196 : // @ts -expect-error bad implementation
197- ( entity : Input ) => entity [ getKeyRaw ] as string | number ;
197+ ( entity : Input ) => entity [ getKeyRaw ] as string | number
198+ : ( entity : Input ) => entity [ kvModel . keyField ] as string | number ;
198199 const $entities = createStore < ListState > ( {
199200 items : [ ] ,
200201 instances : [ ] ,
@@ -250,19 +251,11 @@ export function keyval<Input, ModelEnhance, Api, Shape>(
250251 // @ts -expect-error some issues with types
251252 const instance = spawn ( kvModel , item1 ) ;
252253 withRegion ( instance . region , ( ) => {
253- const storeOutputs = { } as Record < string , Store < any > > ;
254- for ( const key in instance . props ) {
255- const value = instance . props [ key ] ;
256- storeOutputs [ key ] = isKeyval ( value )
257- ? value . $items
258- : ( value as Store < any > ) ;
259- }
260- const $enriching = combine ( storeOutputs ) ;
261254 // obviosly dirty hack, wont make it way to release
262- const enriching = $enriching . getState ( ) ;
263- freshState . items . push ( { ... inputItem , ... enriching } as Enriched ) ;
255+ const enriching = instance . output . getState ( ) ;
256+ freshState . items . push ( enriching as Enriched ) ;
264257 sample ( {
265- source : $enriching ,
258+ source : instance . output ,
266259 fn : ( partial ) => ( {
267260 key,
268261 partial : partial as Partial < Enriched > ,
@@ -317,18 +310,18 @@ export function keyval<Input, ModelEnhance, Api, Shape>(
317310 freshState . items [ idx ] = newItem ;
318311 if ( kvModel ) {
319312 const instance = freshState . instances [ idx ] ;
320- for ( const key in instance . inputs ) {
321- // @ts -expect-error cannot read newItem[key], but its ok
322- const upd = newItem [ key ] ;
323- // @ts -expect-error cannot read oldItem[key], but its ok
324- if ( upd !== oldItem [ key ] ) {
325- launch ( {
326- target : instance . inputs [ key ] ,
327- params : upd ,
328- defer : true ,
329- } ) ;
330- }
331- }
313+ // for (const key in instance.inputs) {
314+ // // @ts -expect-error cannot read newItem[key], but its ok
315+ // const upd = newItem[key];
316+ // // @ts -expect-error cannot read oldItem[key], but its ok
317+ // if (upd !== oldItem[key]) {
318+ // launch({
319+ // target: instance.inputs[key],
320+ // params: upd,
321+ // defer: true,
322+ // });
323+ // }
324+ // }
332325 }
333326 }
334327
@@ -373,7 +366,6 @@ export function keyval<Input, ModelEnhance, Api, Shape>(
373366 if ( kvModel ) {
374367 for ( const instance of oldState . instances ) {
375368 clearNode ( instance . region ) ;
376- instance . unmount ( ) ;
377369 }
378370 }
379371 const state : ListState = {
@@ -384,19 +376,18 @@ export function keyval<Input, ModelEnhance, Api, Shape>(
384376 for ( const item of newItems ) {
385377 const key = getKey ( item ) ;
386378 runNewItemInstance ( state , key , item ) ;
387- /** new instance is always last */
388- const instance = state . instances [ state . instances . length - 1 ] ;
389- for ( const key in item ) {
390- if ( key in writableOutputs ) {
391- launch ( {
392- target :
393- writableOutputs [ key ] === 'keyval'
394- ? ( instance . props [ key ] as any ) . edit . replaceAll
395- : // @ts -expect-error typescript is broken here
396- instance . props [ key ] ,
397- params : item [ key ] ,
398- defer : true ,
399- } ) ;
379+ if ( kvModel ) {
380+ /** new instance is always last */
381+ const instance = state . instances [ state . instances . length - 1 ] ;
382+ for ( const field of kvModel . keyvalFields ) {
383+ // @ts -expect-error type mismatch, item is iterable
384+ if ( field in item ) {
385+ launch ( {
386+ target : instance . keyvalShape [ field ] . edit . replaceAll ,
387+ params : ( item as any ) [ field ] ,
388+ defer : true ,
389+ } ) ;
390+ }
400391 }
401392 }
402393 }
@@ -467,7 +458,6 @@ export function keyval<Input, ModelEnhance, Api, Shape>(
467458 const [ instance ] = state . instances . splice ( idx , 1 ) ;
468459 if ( instance ) {
469460 clearNode ( instance . region ) ;
470- instance . unmount ( ) ;
471461 }
472462 }
473463 return state ;
@@ -503,7 +493,6 @@ export function keyval<Input, ModelEnhance, Api, Shape>(
503493 } ) ;
504494
505495 const api = { } as Record < string , EventCallable < any > > ;
506- const writableOutputs = { } as Record < string , 'store' | 'keyval' > ;
507496
508497 let structShape : any = null ;
509498
@@ -519,12 +508,9 @@ export function keyval<Input, ModelEnhance, Api, Shape>(
519508 initShape [ key ] = createEffect ( ( ) => { } ) ;
520509 }
521510 } ) ;
522- const consoleError = console . error ;
523- console . error = ( ) => { } ;
524511 // @ts -expect-error type issues
525512 const instance = spawn ( kvModel , initShape ) ;
526- console . error = consoleError ;
527- instance . unmount ( ) ;
513+ clearNode ( instance . region ) ;
528514 structShape = {
529515 type : 'structKeyval' ,
530516 getKey,
@@ -564,14 +550,6 @@ export function keyval<Input, ModelEnhance, Api, Shape>(
564550 return state ;
565551 } ) ;
566552 }
567- for ( const key in instance . props ) {
568- const value = instance . props [ key ] ;
569- if ( isKeyval ( value ) ) {
570- writableOutputs [ key ] = 'keyval' ;
571- } else if ( is . store ( value ) && is . targetable ( value ) ) {
572- writableOutputs [ key ] = 'store' ;
573- }
574- }
575553 }
576554
577555 return {
@@ -580,7 +558,6 @@ export function keyval<Input, ModelEnhance, Api, Shape>(
580558 // @ts -expect-error bad implementation
581559 __lens : shape ,
582560 __struct : structShape ,
583- __getKey : getKey ,
584561 $items : $entities . map ( ( { items } ) => items ) ,
585562 $keys : $entities . map ( ( { keys } ) => keys ) ,
586563 edit : {
0 commit comments