Skip to content

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.

fix(webv2): scope gallery semantic query memoization

joshistoasteefcbea3309a
diff --git a/invokeai/frontend/webv2/src/features/gallery/ui/GalleryWidgetView.tsx b/invokeai/frontend/webv2/src/features/gallery/ui/GalleryWidgetView.tsx
index 063c776a2b..a7606e266d 100644
--- a/invokeai/frontend/webv2/src/features/gallery/ui/GalleryWidgetView.tsx
+++ b/invokeai/frontend/webv2/src/features/gallery/ui/GalleryWidgetView.tsx
@@ -56,6 +56,10 @@ export const GalleryStatusChip = ({ count }: { count: number }) => {
);
};
+/** Keeps query identity local to the gallery consumer that owns it. */
+export const useGallerySemanticImageQuery = (value: unknown) =>
+ useMemo(() => getGallerySemanticImageQuery({ semanticImageQuery: value }), [value]);
+
export const GalleryWidgetView = ({ presentation, region, runtime }: GalleryWidgetProps) => {
const {
gallery: galleryCommands,
@@ -76,7 +80,7 @@ export const GalleryWidgetView = ({ presentation, region, runtime }: GalleryWidg
const knownTotalImages = getGalleryTotalImages(galleryValues);
const settings = getGallerySettings(galleryValues);
const queryClient = useQueryClient();
- const semanticQuery = getGallerySemanticImageQuery(galleryValues);
+ const semanticQuery = useGallerySemanticImageQuery(galleryValues.semanticImageQuery);
const data = useGalleryData({
galleryView,
page,
diff --git a/invokeai/frontend/webv2/src/features/gallery/ui/galleryStateView.test.ts b/invokeai/frontend/webv2/src/features/gallery/ui/galleryStateView.test.ts
index b5181dbc67..9c7c8c0db6 100644
--- a/invokeai/frontend/webv2/src/features/gallery/ui/galleryStateView.test.ts
+++ b/invokeai/frontend/webv2/src/features/gallery/ui/galleryStateView.test.ts
@@ -263,16 +263,17 @@ describe('gallery state view', () => {
expect(gallery.compareImageKey).toBe('image:shared');
});
- it('returns an identity-stable semantic reference while the underlying value is unchanged', () => {
- // An unstable identity here restarts in-flight searches on every
- // unrelated gallery interaction (it feeds memo/effect dependencies).
+ it('parses a persisted semantic reference for the gallery view', () => {
const values = { semanticImageQuery: { kind: 'url', url: 'https://x.test/i.png' } };
const first = getGallerySemanticImageQuery(values);
const second = getGallerySemanticImageQuery({ ...values });
expect(first).toEqual({ kind: 'url', url: 'https://x.test/i.png' });
- expect(second).toBe(first);
- expect(getGallerySemanticImageQuery({ semanticImageQuery: 'other.png' })).not.toBe(first);
+ expect(second).toEqual(first);
+ expect(getGallerySemanticImageQuery({ semanticImageQuery: 'other.png' })).toEqual({
+ imageName: 'other.png',
+ kind: 'image',
+ });
});
it('threads the parsed semantic reference into the view and hides pending placeholders while ranked', () => {
diff --git a/invokeai/frontend/webv2/src/features/gallery/ui/galleryStateView.ts b/invokeai/frontend/webv2/src/features/gallery/ui/galleryStateView.ts
index 67fc71e382..84a0e62315 100644
--- a/invokeai/frontend/webv2/src/features/gallery/ui/galleryStateView.ts
+++ b/invokeai/frontend/webv2/src/features/gallery/ui/galleryStateView.ts
@@ -14,7 +14,6 @@ import {
getSelectedGalleryItemFromValues,
} from '@features/gallery/core/selection';
import {
- gallerySemanticReferenceKey,
parseGallerySemanticReference,
type GallerySemanticReference,
} from '@features/gallery/core/semanticImageQuery';
@@ -231,22 +230,8 @@ export const getGalleryView = (values: Record<string, unknown>): GalleryView =>
export const getGallerySearchTerm = (values: Record<string, unknown>): string =>
typeof values.searchTerm === 'string' ? values.searchTerm : '';
-// The parser builds a fresh object per call, but selectors and memo
-// dependencies need identity stability: reuse the previous object while its
-// key is unchanged. (The gallery widget is single-instance, so one slot is
-// enough; a second consumer with a different value would still be correct,
-// merely unmemoized.)
-let lastSemanticReference: GallerySemanticReference | null = null;
-
-export const getGallerySemanticImageQuery = (values: Record<string, unknown>): GallerySemanticReference | null => {
- const parsed = parseGallerySemanticReference(values.semanticImageQuery);
-
- if (gallerySemanticReferenceKey(parsed) !== gallerySemanticReferenceKey(lastSemanticReference)) {
- lastSemanticReference = parsed;
- }
-
- return lastSemanticReference;
-};
+export const getGallerySemanticImageQuery = (values: Record<string, unknown>): GallerySemanticReference | null =>
+ parseGallerySemanticReference(values.semanticImageQuery);
/**
* Where new results land, resolved against the boards this install actually has.
diff --git a/invokeai/frontend/webv2/src/features/gallery/ui/useGallerySemanticImageQuery.browser.test.tsx b/invokeai/frontend/webv2/src/features/gallery/ui/useGallerySemanticImageQuery.browser.test.tsx
new file mode 100644
index 0000000000..24f52b0bcd
--- /dev/null
+++ b/invokeai/frontend/webv2/src/features/gallery/ui/useGallerySemanticImageQuery.browser.test.tsx
@@ -0,0 +1,59 @@
+import type { GallerySemanticReference } from '@features/gallery/core/semanticImageQuery';
+
+import { act } from 'react';
+import { createRoot } from 'react-dom/client';
+import { expect, it } from 'vitest';
+
+import { useGallerySemanticImageQuery } from './GalleryWidgetView';
+
+(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
+const FIRST_A = { kind: 'url', url: 'https://a.test/image.png' } as const;
+const SECOND_A = { kind: 'url', url: 'https://a.test/other.png' } as const;
+const ONLY_B = { imageName: 'b.png', kind: 'image' } as const;
+
+it('keeps semantic query identity stable per gallery consumer', async () => {
+ const references: Record<'a' | 'b', (GallerySemanticReference | null)[]> = { a: [], b: [] };
+ const host = document.createElement('div');
+ const root = createRoot(host);
+
+ document.body.append(host);
+
+ const Probe = ({ id, value }: { id: 'a' | 'b'; value: unknown }) => {
+ references[id].push(useGallerySemanticImageQuery(value));
+
+ return null;
+ };
+
+ await act(() =>
+ root.render(
+ <>
+ <Probe id="a" value={FIRST_A} />
+ <Probe id="b" value={ONLY_B} />
+ </>
+ )
+ );
+ await act(() =>
+ root.render(
+ <>
+ <Probe id="a" value={FIRST_A} />
+ <Probe id="b" value={ONLY_B} />
+ </>
+ )
+ );
+ await act(() =>
+ root.render(
+ <>
+ <Probe id="a" value={SECOND_A} />
+ <Probe id="b" value={ONLY_B} />
+ </>
+ )
+ );
+
+ expect(references.a[1]).toBe(references.a[0]);
+ expect(references.b[1]).toBe(references.b[0]);
+ expect(references.a[2]).not.toBe(references.a[1]);
+ expect(references.b[2]).toBe(references.b[1]);
+
+ await act(() => root.unmount());
+ host.remove();
+});