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(gallery): prevent repeated total update dispatches

joshistoastbfefc0d1bba5
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/gallery/GalleryWidgetView.test.ts b/invokeai/frontend/webv2/src/workbench/widgets/gallery/GalleryWidgetView.test.ts
new file mode 100644
index 0000000000..bce1cf1c02
--- /dev/null
+++ b/invokeai/frontend/webv2/src/workbench/widgets/gallery/GalleryWidgetView.test.ts
@@ -0,0 +1,22 @@
+import { describe, expect, it } from 'vitest';
+
+import { shouldPublishGalleryTotal } from './GalleryWidgetView';
+
+describe('shouldPublishGalleryTotal', () => {
+ it('publishes finite totals that differ from known state and have not already been published', () => {
+ expect(shouldPublishGalleryTotal({ knownTotalImages: null, lastPublishedTotal: null, total: 12 })).toBe(true);
+ expect(shouldPublishGalleryTotal({ knownTotalImages: 8, lastPublishedTotal: null, total: 12 })).toBe(true);
+ });
+
+ it('does not republish the same in-flight total while known state catches up', () => {
+ expect(shouldPublishGalleryTotal({ knownTotalImages: null, lastPublishedTotal: 12, total: 12 })).toBe(false);
+ });
+
+ it('does not publish non-finite or already-known totals', () => {
+ expect(shouldPublishGalleryTotal({ knownTotalImages: 12, lastPublishedTotal: null, total: 12 })).toBe(false);
+ expect(shouldPublishGalleryTotal({ knownTotalImages: null, lastPublishedTotal: null, total: null })).toBe(false);
+ expect(shouldPublishGalleryTotal({ knownTotalImages: null, lastPublishedTotal: null, total: Number.NaN })).toBe(
+ false
+ );
+ });
+});
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/gallery/GalleryWidgetView.tsx b/invokeai/frontend/webv2/src/workbench/widgets/gallery/GalleryWidgetView.tsx
index 9fc372697d..9f42531bba 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/gallery/GalleryWidgetView.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/gallery/GalleryWidgetView.tsx
@@ -105,6 +105,17 @@ const selectGalleryWidgetValues = createStableSelector(
const selectGenerateRecallValues = createGenerateFormValuesSelector();
+export const shouldPublishGalleryTotal = ({
+ knownTotalImages,
+ lastPublishedTotal,
+ total,
+}: {
+ knownTotalImages: number | null;
+ lastPublishedTotal: number | null;
+ total: number | null;
+}): boolean =>
+ typeof total === 'number' && Number.isFinite(total) && total !== knownTotalImages && total !== lastPublishedTotal;
+
export const GalleryWidgetView = ({ presentation, region, runtime }: WidgetViewProps) => {
const dispatch = useWorkbenchDispatch();
const projectId = useActiveProjectId();
@@ -149,6 +160,7 @@ export const GalleryWidgetView = ({ presentation, region, runtime }: WidgetViewP
const galleryImagesRef = useRef(gallery.images);
const selectedImageNameRef = useRef(gallery.selectedImageName);
+ const lastPublishedTotalRef = useRef<number | null>(null);
galleryImagesRef.current = gallery.images;
selectedImageNameRef.current = gallery.selectedImageName;
@@ -213,7 +225,23 @@ export const GalleryWidgetView = ({ presentation, region, runtime }: WidgetViewP
// render page navigation without its own fetch, and clamp the page when the
// query shrinks (e.g. after deletions).
useEffect(() => {
- if (typeof total === 'number' && Number.isFinite(total) && total !== knownTotalImages) {
+ if (total === knownTotalImages) {
+ lastPublishedTotalRef.current = null;
+ return;
+ }
+
+ if (typeof total !== 'number' || !Number.isFinite(total)) {
+ return;
+ }
+
+ if (
+ shouldPublishGalleryTotal({
+ knownTotalImages,
+ lastPublishedTotal: lastPublishedTotalRef.current,
+ total,
+ })
+ ) {
+ lastPublishedTotalRef.current = total;
dispatch({ totalImages: total, type: 'setGalleryPageInfo' });
}
}, [dispatch, knownTotalImages, total]);