Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
feat(webv2): star gallery items optimistically with failure rollback
joshistoast31a606c7bc84
diff --git a/invokeai/frontend/webv2/src/workbench/image-actions/useImageActions.browser.test.tsx b/invokeai/frontend/webv2/src/workbench/image-actions/useImageActions.browser.test.tsxindex 4c47607710..0b90466535 100644--- a/invokeai/frontend/webv2/src/workbench/image-actions/useImageActions.browser.test.tsx+++ b/invokeai/frontend/webv2/src/workbench/image-actions/useImageActions.browser.test.tsx@@ -305,7 +305,7 @@ describe('partial image mutation outcomes', () => {expect(mocks.galleryPatchItems).toHaveBeenCalledWith(['image:moved.png'], { boardId: 'board-1' });});- it('stars and patches only images confirmed by the backend', async () => {+ it('stars optimistically before the request and reverts only backend-rejected images', async () => {mocks.itemSetStarred.mockResolvedValue({affectedBoardIds: [],failed: [{ kind: 'image', name: 'locked.png' }],@@ -316,18 +316,32 @@ describe('partial image mutation outcomes', () => {await actionsRef.current?.setImagesStarred(['starred.png', 'locked.png'], true);});- const result = {- affectedBoardIds: [],- failed: [{ kind: 'image', name: 'locked.png' }],- succeeded: [{ kind: 'image', name: 'starred.png' }],- };-- expect(mocks.patchGalleryItemCaches).toHaveBeenCalledWith(expect.anything(), {+ // The whole selection paints before the transport is even asked.+ expect(mocks.patchGalleryItemCaches).toHaveBeenNthCalledWith(1, expect.anything(), {kind: 'star',- result,+ result: {+ failed: [],+ succeeded: [+ { kind: 'image', name: 'starred.png' },+ { kind: 'image', name: 'locked.png' },+ ],+ },+ starred: true,+ });+ expect(mocks.patchGalleryItemCaches.mock.invocationCallOrder[0]).toBeLessThan(+ mocks.itemSetStarred.mock.invocationCallOrder[0] ?? 0+ );+ expect(mocks.galleryPatchItems).toHaveBeenNthCalledWith(1, ['image:starred.png', 'image:locked.png'], {starred: true,});- expect(mocks.galleryPatchItems).toHaveBeenCalledWith(['image:starred.png'], { starred: true });++ // Only the rejected ref flips back once the backend answers.+ expect(mocks.patchGalleryItemCaches).toHaveBeenNthCalledWith(2, expect.anything(), {+ kind: 'star',+ result: { failed: [], succeeded: [{ kind: 'image', name: 'locked.png' }] },+ starred: false,+ });+ expect(mocks.galleryPatchItems).toHaveBeenNthCalledWith(2, ['image:locked.png'], { starred: false });});});@@ -429,28 +443,28 @@ describe('mixed item mutation outcomes', () => {expect(mocks.notificationsAdd.mock.calls.length + mocks.reportError.mock.calls.length).toBe(1);});- it('stars same-name media independently and preserves a failed selected item', async () => {+ it('stars same-name media independently and reverts only the failed qualified key', async () => {const refs = [{ kind: 'image' as const, name: 'shared' },{ kind: 'video' as const, name: 'shared' },];- const result = {+ mocks.itemSetStarred.mockResolvedValue({affectedBoardIds: [],failed: [refs[0]],succeeded: [refs[1]],- };- mocks.itemSetStarred.mockResolvedValue(result);+ });await act(async () => {await getItemActions().setItemsStarred(refs, true);});- expect(mocks.patchGalleryItemCaches).toHaveBeenCalledWith(expect.anything(), {+ expect(mocks.galleryPatchItems).toHaveBeenNthCalledWith(1, ['image:shared', 'video:shared'], { starred: true });+ expect(mocks.patchGalleryItemCaches).toHaveBeenNthCalledWith(2, expect.anything(), {kind: 'star',- result,- starred: true,+ result: { failed: [], succeeded: [refs[0]] },+ starred: false,});- expect(mocks.galleryPatchItems).toHaveBeenCalledWith(['video:shared'], { starred: true });+ expect(mocks.galleryPatchItems).toHaveBeenNthCalledWith(2, ['image:shared'], { starred: false });expect(mocks.galleryRemoveItems).not.toHaveBeenCalled();expect(mocks.invalidateGallery).toHaveBeenCalledOnce();expect(mocks.notificationsAdd.mock.calls.length + mocks.reportError.mock.calls.length).toBe(1);diff --git a/invokeai/frontend/webv2/src/workbench/image-actions/useImageActions.ts b/invokeai/frontend/webv2/src/workbench/image-actions/useImageActions.tsindex d82326348b..c0b06f1359 100644--- a/invokeai/frontend/webv2/src/workbench/image-actions/useImageActions.ts+++ b/invokeai/frontend/webv2/src/workbench/image-actions/useImageActions.ts@@ -365,20 +365,28 @@ export const useImageActions = ({mutate: (signal) => galleryItemOrganization.moveToBoard(items, boardId, signal),requested: items,});- const setItemsStarred = (items: GalleryItemRef[], starred: boolean): Promise<void> =>- runItemMutation({- action: starred ? 'star' : 'unstar',- applyConfirmed: (result) => {- if (result.succeeded.length === 0) {- return;- }+ const patchItemsStarred = (refs: GalleryItemRef[], starred: boolean): void => {+ if (refs.length === 0) {+ return;+ }- patchGalleryItemCaches(queryClient, { kind: 'star', result, starred });- gallery.patchItems(result.succeeded.map(toGalleryItemKey), { starred });- },+ patchGalleryItemCaches(queryClient, { kind: 'star', result: { failed: [], succeeded: refs }, starred });+ gallery.patchItems(refs.map(toGalleryItemKey), { starred });+ };+ const setItemsStarred = (items: GalleryItemRef[], starred: boolean): Promise<void> => {+ // Optimistic: a star toggle's outcome is the request itself except for+ // the rare rejected ref, so paint the whole selection immediately and+ // flip back only what the backend refuses. The trailing gallery+ // invalidation reconciles either path with the server.+ patchItemsStarred(items, starred);++ return runItemMutation({+ action: starred ? 'star' : 'unstar',+ applyConfirmed: (result) => patchItemsStarred(result.failed, !starred),mutate: (signal) => galleryItemOrganization.setStarred(items, starred, signal),requested: items,});+ };const fetchItemBlob = async (item: GalleryItem, signal: AbortSignal): Promise<Blob> => {const response = await fetch(item.fullUrl, { signal });