({
queryNormalizer,
type,
id,
update
}: {
queryNormalizer: ReturnType<typeof useQueryNormalizer>
type: Key
id: string
update: Partial<TData> | UpdateFn<TData>
})
| 62 | } |
| 63 | |
| 64 | export function createNormalizedOptimisticUpdate< |
| 65 | Key extends keyof NormalizedDataTypes, |
| 66 | TData = NormalizedDataTypes[Key] & { [key: string]: any } |
| 67 | >({ |
| 68 | queryNormalizer, |
| 69 | type, |
| 70 | id, |
| 71 | update |
| 72 | }: { |
| 73 | queryNormalizer: ReturnType<typeof useQueryNormalizer> |
| 74 | type: Key |
| 75 | id: string |
| 76 | update: Partial<TData> | UpdateFn<TData> |
| 77 | }) { |
| 78 | const previous = getNormalizedData<Key, TData>({ queryNormalizer, type, id }) |
| 79 | |
| 80 | if (!previous) return |
| 81 | |
| 82 | const next = typeof update === 'function' ? update(previous) : update |
| 83 | // only pluck key-values from previous so that rollback will only affect the keys that are being updated |
| 84 | const previousTrimmed = pluckKeyValues(next, previous) |
| 85 | |
| 86 | return { |
| 87 | optimisticData: normalizedData(type, { id, ...next }), |
| 88 | rollbackData: normalizedData(type, { id, ...previousTrimmed }) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | export function getNormalizedData<Key extends keyof NormalizedDataTypes, TData = NormalizedDataTypes[Key]>({ |
| 93 | queryNormalizer, |
no test coverage detected