From ea5d598c9f90a26603c4e22eb7e2c5d3ff5ef5d6 Mon Sep 17 00:00:00 2001 From: Birk Johansson Date: Wed, 7 Sep 2022 02:17:52 +0200 Subject: [PATCH] fix: deduplicate identical mutations --- src/app/query-client/use-query-client.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/app/query-client/use-query-client.js b/src/app/query-client/use-query-client.js index 99950b352..2b622b51d 100644 --- a/src/app/query-client/use-query-client.js +++ b/src/app/query-client/use-query-client.js @@ -25,6 +25,25 @@ const useQueryClient = () => { }, }) + const mutationCache = queryClient.getMutationCache() + // prevent duplicate mutations from being stored in cache + mutationCache.subscribe((event) => { + if (event.type !== 'updated') { + return + } + const { mutation } = event + const duplicateMutation = mutationCache.find({ + mutationKey: mutation.options.mutationKey, + // ensure previous mutation was fired before this + // (mutationId is an incremental integer) + predicate: (currMutation) => + currMutation.mutationId < mutation.mutationId, + }) + if (duplicateMutation) { + mutationCache.remove(duplicateMutation) + } + }) + return queryClient }