import { unstable_useCacheRefresh as useCacheRefresh, useCallback, useTransition, } from "react"; import { InternalCache } from "../cache/createCache"; import { STATUS_PENDING, STATUS_REJECTED, STATUS_RESOLVED } from "../constants"; import { Cache, Record } from "../types"; import { createDeferred } from "../utils/createDeferred"; import { createResolvedRecord, updateRecordToResolved } from "../utils/Record"; import { assert } from "../utils/assert"; type MutationCallback = () => Promise; export type MutateAsync, Value> = ( params: Params, callback: MutationCallback ) => Promise; export type MutateSync, Value> = ( params: Params, newValue: Value ) => void; export type MutationApi, Value> = { isPending: boolean; mutateAsync: MutateAsync; mutateSync: MutateSync; }; export function useCacheMutation, Value>( cache: Cache ): MutationApi { const { __createPendingMutationRecordMap: createPendingMutationRecordMap, __getKey: getKey, __isImmutable: isImmutable, __mutationAbortControllerMap: mutationAbortControllerMap, __notifySubscribers: notifySubscribers, __recordMap: recordMap, } = cache as InternalCache; assert(!isImmutable(), "Cannot mutate an immutable cache"); const [isPending, startTransition] = useTransition(); const refresh = useCacheRefresh(); const mutateSync = useCallback>( (params: Params, newValue: Value) => { const cacheKey = getKey(params); if (mutationAbortControllerMap.has(cacheKey)) { const abortController = mutationAbortControllerMap.get(cacheKey)!; abortController.abort(); mutationAbortControllerMap.delete(cacheKey); } const record: Record = createResolvedRecord(newValue); recordMap.set(cacheKey, record); const pendingMutationRecordMap = createPendingMutationRecordMap(); pendingMutationRecordMap.set(cacheKey, record); startTransition(() => { refresh(createPendingMutationRecordMap, pendingMutationRecordMap); }); notifySubscribers(params); }, [refresh, startTransition] ); const mutateAsync = useCallback>( async (params: Params, callback: MutationCallback) => { const cacheKey = getKey(params); if (mutationAbortControllerMap.has(cacheKey)) { const abortController = mutationAbortControllerMap.get(cacheKey)!; abortController.abort(); mutationAbortControllerMap.delete(cacheKey); } const abortController = new AbortController(); const deferred = createDeferred(); let record: Record = { data: { abortController, deferred, status: STATUS_PENDING, }, }; // Don't mutate the module-level cache yet; // this might cause other components to suspend (and fallback) // if they happened to re-render before the mutation finished const pendingMutationRecordMap = createPendingMutationRecordMap(); pendingMutationRecordMap.set(cacheKey, record); mutationAbortControllerMap.set(cacheKey, abortController); startTransition(() => { refresh(createPendingMutationRecordMap, pendingMutationRecordMap); }); notifySubscribers(params, { status: STATUS_PENDING }); try { // Wait until the mutation finishes or is aborted const newValue = await Promise.race([ callback(), new Promise((resolve) => { abortController.signal.onabort = () => resolve(); }), ]); if (abortController.signal.aborted) { // The mutation was aborted; // if we can restore the previous record, do it const backupRecord = recordMap.get(cacheKey); if (backupRecord) { pendingMutationRecordMap.set(cacheKey, backupRecord); } else { pendingMutationRecordMap.delete(cacheKey); } } else { // This method determines whether to store the value in a WeakRef updateRecordToResolved(record, newValue as Value); deferred.resolve(newValue as Value); recordMap.set(cacheKey, record); } startTransition(() => { refresh(createPendingMutationRecordMap, pendingMutationRecordMap); }); notifySubscribers(params, { status: STATUS_RESOLVED, value: newValue as Value, }); } catch (error) { (record as Record).data = { error, status: STATUS_REJECTED, }; try { deferred.reject(error); await deferred.promise; } catch (error) { // Don't trigger an unhandled rejection } recordMap.set(cacheKey, record); startTransition(() => { refresh(createPendingMutationRecordMap, pendingMutationRecordMap); }); notifySubscribers(params, { error, status: STATUS_REJECTED, }); } finally { // Cleanup after mutation by deleting the abort controller // If this mutation has already been preempted by a newer mutation // don't delete the newer controller if (abortController === mutationAbortControllerMap.get(cacheKey)) { mutationAbortControllerMap.delete(cacheKey); } } }, [refresh, startTransition] ); return { isPending, mutateAsync, mutateSync, }; } useCacheMutation.displayName = "useCacheMutation";