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): coalesce library refreshes from install completions

joshistoast39e9d9dd6f01
diff --git a/invokeai/frontend/webv2/src/features/models/data/installsStore.test.ts b/invokeai/frontend/webv2/src/features/models/data/installsStore.test.ts
index 16bd4e3b96..02a6e08025 100644
--- a/invokeai/frontend/webv2/src/features/models/data/installsStore.test.ts
+++ b/invokeai/frontend/webv2/src/features/models/data/installsStore.test.ts
@@ -75,11 +75,26 @@ describe('model install event interpretation', () => {
modelName: 'Installed Model',
source: 'org/model',
});
+ // Catalog refreshes ride the same coalescing window as the jobs list.
+ expect(dependencies.refreshModels).not.toHaveBeenCalled();
+
+ await vi.advanceTimersByTimeAsync(250);
expect(dependencies.refreshModels).toHaveBeenCalledTimes(1);
expect(dependencies.refreshStartersIfLoaded).toHaveBeenCalledTimes(1);
+ expect(dependencies.listModelInstalls).toHaveBeenCalledTimes(1);
+ });
+
+ it('coalesces a burst of completions into one library refetch', async () => {
+ const store = await import('./installsStore');
+
+ for (const id of [1, 2, 3, 4]) {
+ store.handleModelInstallSocketEvent('model_install_complete', { config: {}, id, source: 'org/model' });
+ }
await vi.advanceTimersByTimeAsync(250);
- expect(dependencies.listModelInstalls).toHaveBeenCalledTimes(1);
+
+ expect(dependencies.refreshModels).toHaveBeenCalledTimes(1);
+ expect(dependencies.refreshStartersIfLoaded).toHaveBeenCalledTimes(1);
});
it('drops a stale refresh without releasing the replacement account refresh', async () => {
diff --git a/invokeai/frontend/webv2/src/features/models/data/installsStore.ts b/invokeai/frontend/webv2/src/features/models/data/installsStore.ts
index 3e272d9053..9baad119aa 100644
--- a/invokeai/frontend/webv2/src/features/models/data/installsStore.ts
+++ b/invokeai/frontend/webv2/src/features/models/data/installsStore.ts
@@ -85,6 +85,7 @@ const progressByJobId = createKeyedTransientStore<number, InstallDownloadProgres
const refreshFlight = createTrailingSingleFlight();
let refreshTimer: ReturnType<typeof setTimeout> | null = null;
+let catalogRefreshTimer: ReturnType<typeof setTimeout> | null = null;
registerAccountOwnedResource({
clear: () => {
@@ -93,6 +94,11 @@ registerAccountOwnedResource({
refreshTimer = null;
}
+ if (catalogRefreshTimer !== null) {
+ clearTimeout(catalogRefreshTimer);
+ catalogRefreshTimer = null;
+ }
+
refreshFlight.reset();
nextOutcomeId = 1;
progressByJobId.clear();
@@ -154,6 +160,23 @@ const scheduleRefresh = (): void => {
}, REFRESH_COALESCE_MS);
};
+/**
+ * Revalidate the library + starter flags after installs land. Coalesced like
+ * `scheduleRefresh`: a bundle whose jobs complete in a burst triggers one
+ * full-library refetch, not one per completion event.
+ */
+const scheduleCatalogRefresh = (): void => {
+ if (catalogRefreshTimer !== null) {
+ return;
+ }
+
+ catalogRefreshTimer = setTimeout(() => {
+ catalogRefreshTimer = null;
+ void refreshModels();
+ refreshStartersIfLoaded();
+ }, REFRESH_COALESCE_MS);
+};
+
/** Optimistically replace one job (e.g. after pause/resume API calls). */
export const replaceInstallJob = (job: ModelInstallJob): void => {
store.patchSnapshot({
@@ -248,8 +271,7 @@ export const handleModelInstallSocketEvent = (
modelName: data.config?.name ?? null,
source: getInstallSourceLabel(data.source),
});
- void refreshModels();
- refreshStartersIfLoaded();
+ scheduleCatalogRefresh();
} else if (event === 'model_install_error') {
recordOutcome({
error: data.error ?? data.error_type ?? 'Unknown install error.',