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
import type { GalleryMediaFilter } from '@features/gallery/contracts';
import type { ImageIndexStatusEvent, ImageMapProjectionReadyEvent } from '@workbench/image-map/events';
import { getGalleryMediaFilter, toGalleryMediaKind } from '@features/gallery/contracts';
import { getAuthSession } from '@features/identity';
import { useMountEffect } from '@platform/react/useMountEffect';
import { socketHub } from '@platform/transport/socketHub';
import {
imageMapStore,
recordImageIndexStatus,
refreshImageIndexStatus,
refreshImageMapPoints,
setImageMapMediaKind,
} from '@workbench/image-map/imageMapStore';
import { getProjectWidgetValues } from '@workbench/widgetState';
import { useWorkbenchQueries, useWorkbenchSubscription } from '@workbench/WorkbenchContext';
/** Admins get every user's events, which would drive UMAP fits on each admin; unknown users are not foreign. */
const isForAnotherUser = (userId: unknown): boolean => {
const sessionUserId = getAuthSession().user?.user_id;
return typeof userId === 'string' && typeof sessionUserId === 'string' && userId !== sessionUserId;
};
/** The active project's Gallery media filter, which the map shares, and a subscription to its changes. */
export interface ImageMapMediaFilterSource {
getMediaFilter: () => GalleryMediaFilter;
subscribe: (listener: () => void) => () => void;
}
/** Refreshes loaded maps on projection events and reconnects; footer counts never fetch. */
export const attachImageMapDataRuntime = (mediaFilterSource: ImageMapMediaFilterSource): (() => void) => {
// The store ignores an unchanged kind, so every Workbench change may push; only an actual change refetches.
const syncMediaKind = () => setImageMapMediaKind(toGalleryMediaKind(mediaFilterSource.getMediaFilter()));
syncMediaKind();
const detachMediaFilter = mediaFilterSource.subscribe(syncMediaKind);
// The widget host is mounted whenever the image-map widget is *available*, not when it is open, so anything
// unconditional here bills a user who never looks at the map.
const hasLoadedOnce = () => {
const { loadState } = imageMapStore.getSnapshot();
return loadState === 'loaded' || loadState === 'error';
};
const refreshIfLoaded = () => {
const { loadState, renderError } = imageMapStore.getSnapshot();
// Ignore ordinary events during first load; projection-ready is the exception because it may supersede the
// in-flight response.
if (loadState !== 'loaded' && loadState !== 'error') {
return;
}
// A failed canvas is not something fresh data can repair, and every successful refresh clears `renderError` — which
// remounts the plot, fails again, and flickers once per event for the length of a backfill. Clearing it stays what
// it was designed to be: the user's deliberate retry.
if (renderError) {
return;
}
void refreshImageMapPoints();
};
// Allow projection-ready during loading: recompute can overtake the fetch and invalidate labels.
const refreshProjectionReady = () => {
const { loadState, renderError } = imageMapStore.getSnapshot();
if (renderError || (loadState !== 'loaded' && loadState !== 'error' && loadState !== 'loading')) {
return;
}
void refreshImageMapPoints();
};
const detachers = [
// The backend routes this to the requesting user's room plus admins, so receipt alone does not mean "my map
// changed".
socketHub.on('image_map_projection_ready', (payload: never) => {
if (!isForAnotherUser((payload as unknown as ImageMapProjectionReadyEvent | undefined)?.user_id)) {
refreshProjectionReady();
}
}),
// Counts-free per-user poke: the owner's images just reached the index.
socketHub.on('image_index_updated', refreshIfLoaded),
socketHub.on('image_index_status', (payload: never) => {
const event = payload as unknown as ImageIndexStatusEvent;
recordImageIndexStatus({
embedded: event.embedded,
failed: event.failed ?? 0,
pending: event.pending,
total: event.total,
});
// At indexing quiescence, refresh to trigger scope-hash comparison and needed recomputation.
if (event.pending === 0) {
refreshIfLoaded();
}
}),
];
// Use connection replay only as baseline; refresh on actual disconnected-to-connected transitions.
let previousStatus: string | null = null;
const detachConnection = socketHub.onConnectionChange((status) => {
const isReconnect = previousStatus !== null && previousStatus !== 'connected' && status === 'connected';
previousStatus = status;
if (isReconnect) {
refreshIfLoaded();
// Reread counts after reconnect because status events are not replayed; retain the map-open guard.
if (hasLoadedOnce()) {
refreshImageIndexStatus();
}
}
});
return () => {
for (const detach of detachers) {
detach();
}
detachConnection();
detachMediaFilter();
};
};
/** React is only the idempotent lifecycle adapter for the non-React runtime. */
export const ImageMapDataRuntime = () => {
const { getSnapshot } = useWorkbenchQueries();
const subscribe = useWorkbenchSubscription();
// Both are the store's stable read-model ports, so the mount-time closure stays current.
useMountEffect(() =>
attachImageMapDataRuntime({
getMediaFilter: () => getGalleryMediaFilter(getProjectWidgetValues(getSnapshot().activeProject, 'gallery')),
subscribe,
})
);
return null;
};