diff --git a/src/createRemoteStateCachingContext.ts b/src/createRemoteStateCachingContext.ts index 29cb278..6ac6989 100644 --- a/src/createRemoteStateCachingContext.ts +++ b/src/createRemoteStateCachingContext.ts @@ -271,6 +271,24 @@ export const createRemoteStateCachingContext = < return { ...logicExtendedWithCaching, addTrigger, name: registration.name }; }; + /** + * define a method which is able to kick off all registered query invalidations on invocation of a mutation + * + * note + * - it's important to invalidate _before_ a mutation begins, because if the runtime is terminated part way through the mutation, onOutput will never be called + */ + const onMutationInput = async
  • any>({ + mutationName, + mutationInput, + mutationOutput, + mutationStatus, + }: { + mutationName: string; + mutationInput: Parameters; + mutationOutput: null; + mutationStatus: MutationExecutionStatus; + }) => onMutationOutput({ mutationName, mutationInput, mutationOutput, mutationStatus }); // note: for now, its just an alias for readability. we may find additional requirements in the future + /** * define a method which is able to kick off all registered query invalidations and query updates, on the execution of a mutation */ @@ -378,10 +396,19 @@ export const createRemoteStateCachingContext = < // define the execute function, with triggers onMutationOutput const execute: L = (async (...args: Parameters): Promise> => { try { + // invalidate accessible cache entries before the mutation runs, in case the runtime is ungracefully terminated during logic + await onMutationInput({ mutationName, mutationInput: args, mutationOutput: null, mutationStatus: MutationExecutionStatus.RESOLVED }); + + // execute the logic const result = (await logic(...args)) as ReturnType; + + // invalidate accessible cache entries now that the mutation has completed with success await onMutationOutput({ mutationName, mutationInput: args, mutationOutput: result, mutationStatus: MutationExecutionStatus.RESOLVED }); + + // return the original result return result; } catch (error) { + // invalidate accessible cache entries now that the mutation has completed with rejection await onMutationOutput({ mutationName, mutationInput: args, mutationOutput: null, mutationStatus: MutationExecutionStatus.REJECTED }); throw error; }