Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataSync: Cancel all previous mutations upon new mutation of same key #41472

Open
wants to merge 12 commits into
base: trunk
Choose a base branch
from
11 changes: 8 additions & 3 deletions projects/js-packages/react-data-sync-client/src/DataSyncHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,13 @@ export function useDataSync<
// AbortController is used to track rapid value mutations
// and will cancel in-flight requests and prevent
// the optimistic value from being reverted.
const getAbortController = () =>
queryClient.getMutationDefaults( queryKey )?.meta?.abortController as AbortController;
const getAbortController = () => {
const defaults = queryClient.getMutationDefaults( queryKey );
return defaults?.meta?.abortController instanceof AbortController
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per discussion with @dilirity, this avoids unnecessary type casts. Thanks for pushing this!

? defaults.meta.abortController
: undefined;
};

const setAbortController = ( abortController: AbortController ) => {
queryClient.setMutationDefaults( queryKey, {
meta: {
Expand All @@ -146,7 +151,7 @@ export function useDataSync<
*/
const mutationConfigDefaults = {
meta: {
abortController: null as AbortController | null,
abortController: null,
},
// Mutation function that's called when the mutation is triggered
mutationFn: value => datasync.SET( value, params, getAbortController()?.signal ),
Expand Down
Loading