@@ -10,6 +10,7 @@ interface AutoFetchOptions<T extends { id: I } & Record<string, any>, I> {
10
10
fetchQueryItems : ( selector : Selector < T > ) => ReturnType < ReplicatedCollectionOptions < T , I > [ 'pull' ] > ,
11
11
purgeDelay ?: number ,
12
12
registerRemoteChange ?: ( onChange : ( ) => Promise < void > ) => Promise < void > ,
13
+ mergeItems ?: ( itemA : T , itemB : T ) => T ,
13
14
}
14
15
export type AutoFetchCollectionOptions <
15
16
T extends BaseItem < I > ,
@@ -35,6 +36,7 @@ export default class AutoFetchCollection<
35
36
private reactivityAdapter : ReactivityAdapter | null = null
36
37
private loadingSignals = new Map < string , Signal < boolean > > ( )
37
38
private isFetchingSignal : Signal < boolean >
39
+ private mergeItems : ( itemA : T , itemB : T ) => T
38
40
39
41
/**
40
42
* @param options {Object} - Options for the collection.
@@ -54,7 +56,7 @@ export default class AutoFetchCollection<
54
56
newItems . push ( item )
55
57
return
56
58
}
57
- newItems [ index ] = { ... newItems [ index ] , ... item }
59
+ newItems [ index ] = this . mergeItems ( newItems [ index ] , item )
58
60
} )
59
61
return newItems
60
62
} , [ ] ) ,
@@ -64,6 +66,7 @@ export default class AutoFetchCollection<
64
66
return Promise . resolve ( )
65
67
} ,
66
68
} )
69
+ this . mergeItems = options . mergeItems ?? ( ( itemA , itemB ) => ( { ...itemA , ...itemB } ) )
67
70
this . purgeDelay = options . purgeDelay ?? 10000 // 10 seconds
68
71
this . isFetchingSignal = createSignal ( options . reactivity ?. create ( ) , false )
69
72
if ( ! triggerRemoteChange ) throw new Error ( 'No triggerRemoteChange method found. Looks like your persistence adapter was not registered' )
0 commit comments