Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 36 additions & 16 deletions libs/storefront-data/src/cart/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import {
createCacheConfig,
getPrefetchCacheOptions,
} from "../shared/cache-config"
import { invalidateCartCaches, syncCartCaches } from "../shared/cart-cache-sync"
import {
cancelCartCaches,
invalidateCartCaches,
syncCartCaches,
} from "../shared/cart-cache-sync"
import { toErrorMessage } from "../shared/error-utils"
import type {
MutationOptions,
Expand Down Expand Up @@ -530,12 +534,18 @@ export function createCartHooks<
invalidateCartCaches(queryClient, resolvedQueryKeys, cartId)
}

const syncMutationCart = (
const syncMutationCart = async (
queryClient: ReturnType<typeof useQueryClient>,
cart: TCart
) => {
const cancellation = cancelCartCaches(
queryClient,
resolvedQueryKeys,
cart.id
)
syncCartCaches(queryClient, resolvedQueryKeys, cart)
invalidateCart(queryClient, cart)
await cancellation
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

type LoadCartOptions = {
Expand Down Expand Up @@ -621,6 +631,16 @@ export function createCartHooks<
return
}

const sourceKey = resolvedQueryKeys.active({
cartId: previousCartId,
regionId: previousRegionId,
})
// A restored inactive observer can resume with data older than its cache.
// Only the raw value still owned by this query may fan out to cart aliases.
if (queryClient.getQueryData(sourceKey) !== cart) {
return
}

syncCartCaches(queryClient, resolvedQueryKeys, cart)

if (previousCartId !== cart.id) {
Expand Down Expand Up @@ -744,9 +764,9 @@ export function createCartHooks<
mutationFn: (input: TCreateInput) =>
service.createCart(buildCreate(input)),
onMutate: options?.onMutate,
onSuccess: (cart, variables, context) => {
onSuccess: async (cart, variables, context) => {
persistCartId(cart.id)
syncMutationCart(queryClient, cart)
await syncMutationCart(queryClient, cart)
options?.onSuccess?.(cart, variables, context)
},
onError: (error, variables, context) => {
Expand All @@ -772,8 +792,8 @@ export function createCartHooks<
return service.updateCart(cartId, buildUpdate(input))
},
onMutate: options?.onMutate,
onSuccess: (cart, variables, context) => {
syncMutationCart(queryClient, cart)
onSuccess: async (cart, variables, context) => {
await syncMutationCart(queryClient, cart)
options?.onSuccess?.(cart, variables, context)
},
onError: (error, variables, context) => {
Expand Down Expand Up @@ -806,8 +826,8 @@ export function createCartHooks<
return callUpdateCart(cartId, buildUpdate(updateInput))
},
onMutate: options?.onMutate,
onSuccess: (cart, variables, context) => {
syncMutationCart(queryClient, cart)
onSuccess: async (cart, variables, context) => {
await syncMutationCart(queryClient, cart)
options?.onSuccess?.(cart, variables, context)
},
onError: (error, variables, context) => {
Expand Down Expand Up @@ -854,8 +874,8 @@ export function createCartHooks<
return updated
},
onMutate: options?.onMutate,
onSuccess: (cart, variables, context) => {
syncMutationCart(queryClient, cart)
onSuccess: async (cart, variables, context) => {
await syncMutationCart(queryClient, cart)
options?.onSuccess?.(cart, variables, context)
},
onError: (error, variables, context) => {
Expand Down Expand Up @@ -887,8 +907,8 @@ export function createCartHooks<
)
},
onMutate: options?.onMutate,
onSuccess: (cart, variables, context) => {
syncMutationCart(queryClient, cart)
onSuccess: async (cart, variables, context) => {
await syncMutationCart(queryClient, cart)
options?.onSuccess?.(cart, variables, context)
},
onError: (error, variables, context) => {
Expand Down Expand Up @@ -916,8 +936,8 @@ export function createCartHooks<
return service.removeLineItem(cartId, input.lineItemId)
},
onMutate: options?.onMutate,
onSuccess: (cart, variables, context) => {
syncMutationCart(queryClient, cart)
onSuccess: async (cart, variables, context) => {
await syncMutationCart(queryClient, cart)
options?.onSuccess?.(cart, variables, context)
},
onError: (error, variables, context) => {
Expand Down Expand Up @@ -945,8 +965,8 @@ export function createCartHooks<
return service.transferCart(cartId)
},
onMutate: options?.onMutate,
onSuccess: (cart, variables, context) => {
syncMutationCart(queryClient, cart)
onSuccess: async (cart, variables, context) => {
await syncMutationCart(queryClient, cart)
options?.onSuccess?.(cart, variables, context)
},
onError: (error, variables, context) => {
Expand Down
22 changes: 22 additions & 0 deletions libs/storefront-data/src/shared/cart-cache-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,28 @@ export function invalidateCartCaches(
queryClient.invalidateQueries({ queryKey: queryKeys.detail(cartId) })
}

export async function cancelCartCaches(
queryClient: QueryClient,
queryKeys: CartQueryKeys,
cartId: string,
options?: CartCacheSyncOptions
): Promise<void> {
const isActiveCartQueryKey = resolveActiveCartQueryMatcher(queryKeys, options)

await Promise.all([
queryClient.cancelQueries(
{
predicate: (query) => isActiveCartQueryKey(query.queryKey, cartId),
},
{ silent: true }
),
queryClient.cancelQueries(
{ queryKey: queryKeys.detail(cartId) },
{ silent: true }
),
])
}

export type PatchCartCachesParams<TCart extends CartLike> = {
patch: CartUpdater<TCart>
options?: CartCacheSyncOptions
Expand Down
Loading
Loading