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(ui): stop image map status polls from churning the map

Josh Corbett1ffcfe11f090
diff --git a/invokeai/frontend/webv2/src/workbench/image-map/imageMap.test.ts b/invokeai/frontend/webv2/src/workbench/image-map/imageMap.test.ts
index 90ea648eb8..56d3b25b1a 100644
--- a/invokeai/frontend/webv2/src/workbench/image-map/imageMap.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/image-map/imageMap.test.ts
@@ -941,6 +941,7 @@ describe('image index progress', () => {
it('polls only for the mounted view lifetime, without overlapping requests or surviving account changes', async () => {
vi.useFakeTimers();
+ vi.stubGlobal('document', Object.assign(new EventTarget(), { visibilityState: 'visible' }));
try {
let resolveStatus: (value: unknown) => void = () => {};
mocks.apiFetchJson.mockImplementation(
@@ -966,6 +967,32 @@ describe('image index progress', () => {
expect(mocks.apiFetchJson).toHaveBeenCalledTimes(1);
detachNext();
} finally {
+ vi.unstubAllGlobals();
+ vi.useRealTimers();
+ }
+ });
+
+ it('pauses polling while the page is hidden and checks at once when it returns', async () => {
+ vi.useFakeTimers();
+ const page = Object.assign(new EventTarget(), { visibilityState: 'hidden' });
+ vi.stubGlobal('document', page);
+ try {
+ mocks.apiFetchJson.mockResolvedValue({ enabled: true, index: null, projection: { state: 'ready' } });
+ const detach = attachImageMapStatusPolling();
+ await vi.advanceTimersByTimeAsync(15_000);
+ expect(mocks.apiFetchJson).not.toHaveBeenCalled();
+
+ page.visibilityState = 'visible';
+ page.dispatchEvent(new Event('visibilitychange'));
+ await vi.advanceTimersByTimeAsync(0);
+ expect(mocks.apiFetchJson).toHaveBeenCalledTimes(1);
+
+ detach();
+ page.dispatchEvent(new Event('visibilitychange'));
+ await vi.advanceTimersByTimeAsync(5_000);
+ expect(mocks.apiFetchJson).toHaveBeenCalledTimes(1);
+ } finally {
+ vi.unstubAllGlobals();
vi.useRealTimers();
}
});
@@ -1017,6 +1044,43 @@ describe('image index progress', () => {
expect(imageMapStore.getSnapshot().clusterLabels).toBeNull();
});
+ it('lets Check again land while polls repeat the same missing encoder', async () => {
+ imageMapStore.patchSnapshot({
+ data: { ...LOADED_POINTS, modelName: 'encoder', state: 'model_missing' },
+ loadState: 'loaded',
+ });
+ const diagnosis = imageMapStore.getSnapshot().data;
+ let resolvePoints: (value: unknown) => void = () => {};
+ mocks.apiFetchJson.mockImplementation((url: string) => {
+ if (url.startsWith('/api/v1/image_map/status')) {
+ return Promise.resolve({
+ enabled: false,
+ index: null,
+ model_name: 'encoder',
+ projection: { state: 'model_missing' },
+ });
+ }
+ if (url.startsWith('/api/v1/image_map/cluster_labels')) {
+ return Promise.resolve(FOREIGN_LABELS_RESPONSE);
+ }
+ return new Promise((resolve) => {
+ resolvePoints = resolve;
+ });
+ });
+
+ refreshImageIndexStatus();
+ await drainMacrotask();
+ expect(imageMapStore.getSnapshot().data).toBe(diagnosis);
+
+ const checkAgain = refreshImageMapPoints();
+ refreshImageIndexStatus();
+ await drainMacrotask();
+ resolvePoints(BACKEND_RESPONSE);
+ await checkAgain;
+
+ expect(imageMapStore.getSnapshot().data).toMatchObject({ pointCount: 3, state: 'ready' });
+ });
+
it('reloads points when status discovers the encoder reinstalled', async () => {
imageMapStore.patchSnapshot({
data: { ...LOADED_POINTS, modelName: 'encoder', state: 'model_missing' },
diff --git a/invokeai/frontend/webv2/src/workbench/image-map/imageMapStore.ts b/invokeai/frontend/webv2/src/workbench/image-map/imageMapStore.ts
index a6dd3dc385..974b28a039 100644
--- a/invokeai/frontend/webv2/src/workbench/image-map/imageMapStore.ts
+++ b/invokeai/frontend/webv2/src/workbench/image-map/imageMapStore.ts
@@ -365,10 +365,12 @@ export const refreshImageIndexStatus = (): void => {
if (revision === mapRevision) {
const { data } = imageMapStore.getSnapshot();
const missing = status.state === 'model_missing';
+ // A repeated diagnosis must not bump the revision: that would discard an in-flight Check again.
+ const unchanged = missing && data?.state === 'model_missing' && data.modelName === status.modelName;
const replaced = data !== null && status.modelId !== null && data.modelId !== status.modelId;
- const recovered = data?.state === 'model_missing' && status.state !== 'disabled';
+ const recovered = data?.state === 'model_missing' && !missing && status.state !== 'disabled';
- if (missing || replaced || recovered) {
+ if ((missing && !unchanged) || replaced || recovered) {
mapRevision += 1;
labelsSequence += 1;
imageMapStore.patchSnapshot({
@@ -433,15 +435,26 @@ export const ensureImageMapLoaded = (): void => {
refreshImageIndexStatus();
};
-/** Poll only while a map view is mounted; missing encoders produce no index events. */
+/**
+ * Poll only while a map view is mounted and its page is visible; missing encoders produce no index events. A page
+ * returning to view checks at once instead of waiting out the interval.
+ */
export const attachImageMapStatusPolling = (): (() => void) => {
const owner = captureAccountScope();
- const timer = setInterval(refreshImageIndexStatus, 5_000);
+ const poll = () => {
+ if (document.visibilityState === 'visible') {
+ refreshImageIndexStatus();
+ }
+ };
+ const timer = setInterval(poll, 5_000);
const dispose = () => {
clearInterval(timer);
+ document.removeEventListener('visibilitychange', poll);
owner.signal.removeEventListener('abort', dispose);
};
+ document.addEventListener('visibilitychange', poll);
+
owner.signal.addEventListener('abort', dispose, { once: true });
return dispose;