MCPcopy Create free account
hub / github.com/campsite/campsite / createNormalizedOptimisticUpdate

Function createNormalizedOptimisticUpdate

apps/web/utils/queryNormalization.ts:64–90  ·  view source on GitHub ↗
({
  queryNormalizer,
  type,
  id,
  update
}: {
  queryNormalizer: ReturnType<typeof useQueryNormalizer>
  type: Key
  id: string
  update: Partial<TData> | UpdateFn<TData>
})

Source from the content-addressed store, hash-verified

62}
63
64export 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
92export function getNormalizedData<Key extends keyof NormalizedDataTypes, TData = NormalizedDataTypes[Key]>({
93 queryNormalizer,

Callers 15

handleFollowUpDeleteFunction · 0.90
handleFollowUpUpdateFunction · 0.90
useMarkProjectUnreadFunction · 0.90
useDeleteCallPinFunction · 0.90
useCreatePostPinFunction · 0.90
useUpdateCommentFunction · 0.90
useCreatePollVoteFunction · 0.90
useMarkThreadUnreadFunction · 0.90

Calls 4

pluckKeyValuesFunction · 0.90
getNormalizedDataFunction · 0.85
normalizedDataFunction · 0.85
updateFunction · 0.50

Tested by

no test coverage detected