Skip to content

Commit 2bd34dc

Browse files
author
colinmcneil
committed
Decouple improve optimistic mutates
1 parent b75c958 commit 2bd34dc

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

src/extension/ui/src/queries/useCatalog.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,28 @@ export function useCatalogOperations(client: v1.DockerDesktopClient) {
253253
return { success: true, newRegistry };
254254
} catch (error) {
255255
client.desktopUI.toast.error('Failed to register catalog item: ' + error);
256+
// Treat YAML file write failures as fatal, no rollback
256257
throw error;
257258
}
258259
},
260+
onMutate: async ({ item }) => {
261+
// Optimistically update the registry data
262+
const currentRegistry = queryClient.getQueryData(['registry']) as { [key: string]: { ref: string; config?: any } } || {};
263+
const newRegistry: { [key: string]: { ref: string; config?: any } } = {
264+
...currentRegistry,
265+
[item.name]: { ref: item.ref }
266+
};
267+
268+
// If there's config, add it
269+
if (item.config && config && config[item.name]) {
270+
newRegistry[item.name] = {
271+
...newRegistry[item.name],
272+
config: config[item.name]
273+
};
274+
}
275+
276+
queryClient.setQueryData(['registry'], newRegistry);
277+
},
259278
onSuccess: async (data) => {
260279
// Update the registry data after successful registration
261280
queryClient.setQueryData(['registry'], data.newRegistry);
@@ -285,9 +304,21 @@ export function useCatalogOperations(client: v1.DockerDesktopClient) {
285304
return { success: true, newRegistry: currentRegistry };
286305
} catch (error) {
287306
client.desktopUI.toast.error('Failed to unregister catalog item: ' + error);
307+
// Treat YAML file write failures as fatal, no rollback
288308
throw error;
289309
}
290310
},
311+
onMutate: async (item) => {
312+
// Optimistically update the registry data
313+
const currentRegistry = { ...(queryClient.getQueryData(['registry']) as { [key: string]: { ref: string; config?: any } } || {}) };
314+
315+
// Remove the item
316+
if (currentRegistry[item.name]) {
317+
delete currentRegistry[item.name];
318+
}
319+
320+
queryClient.setQueryData(['registry'], currentRegistry);
321+
},
291322
onSuccess: async (data) => {
292323
// Update the registry data after successful unregistration
293324
queryClient.setQueryData(['registry'], data.newRegistry);

src/extension/ui/src/queries/useConfig.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,31 +67,21 @@ export function useConfig(client: v1.DockerDesktopClient) {
6767
return { itemName, updatedConfig: updatedConfigRef };
6868
} catch (error) {
6969
client.desktopUI.toast.error('Failed to update config: ' + error);
70+
// Treat YAML file write failures as fatal, no rollback
7071
throw error;
7172
}
7273
},
7374
onMutate: async ({ itemName, newConfig }) => {
7475
// Cancel any outgoing refetches
7576
await queryClient.cancelQueries({ queryKey: ['config'] });
7677

77-
// Snapshot the previous value
78-
const previousConfig = queryClient.getQueryData(['config']);
79-
8078
// Optimistically update to the new value
8179
const updatedConfig = {
82-
...(previousConfig as Record<string, any> || {}),
80+
...(queryClient.getQueryData(['config']) as Record<string, any> || {}),
8381
[itemName]: newConfig
8482
};
8583

8684
queryClient.setQueryData(['config'], updatedConfig);
87-
88-
return { previousConfig };
89-
},
90-
onError: (err, variables, context) => {
91-
// If the mutation fails, use the context to roll back
92-
if (context?.previousConfig) {
93-
queryClient.setQueryData(['config'], context.previousConfig);
94-
}
9585
},
9686
onSuccess: (data) => {
9787
client.desktopUI.toast.success('Config saved successfully.');

0 commit comments

Comments
 (0)