From 8954953eca441f237e80b53b3f625c7d250d1e7c Mon Sep 17 00:00:00 2001 From: psychedelicious <4822129+psychedelicious@users.noreply.github.com> Date: Thu, 21 Sep 2023 09:41:19 +1000 Subject: [PATCH] fix(ui): no duplicate network requests on app startup --- .../listeners/socketio/socketConnected.ts | 10 ++++++++-- .../listeners/socketio/socketDisconnected.ts | 3 --- .../src/features/system/store/systemPersistDenylist.ts | 1 + .../web/src/features/system/store/systemSlice.ts | 5 +++++ .../frontend/web/src/features/system/store/types.ts | 1 + 5 files changed, 15 insertions(+), 5 deletions(-) diff --git a/invokeai/frontend/web/src/app/store/middleware/listenerMiddleware/listeners/socketio/socketConnected.ts b/invokeai/frontend/web/src/app/store/middleware/listenerMiddleware/listeners/socketio/socketConnected.ts index b81aa36455..9957b7f117 100644 --- a/invokeai/frontend/web/src/app/store/middleware/listenerMiddleware/listeners/socketio/socketConnected.ts +++ b/invokeai/frontend/web/src/app/store/middleware/listenerMiddleware/listeners/socketio/socketConnected.ts @@ -4,6 +4,7 @@ import { api } from 'services/api'; import { receivedOpenAPISchema } from 'services/api/thunks/schema'; import { appSocketConnected, socketConnected } from 'services/events/actions'; import { startAppListening } from '../..'; +import { isInitializedChanged } from 'features/system/store/systemSlice'; export const addSocketConnectedEventListener = () => { startAppListening({ @@ -13,7 +14,7 @@ export const addSocketConnectedEventListener = () => { log.debug('Connected'); - const { nodes, config } = getState(); + const { nodes, config, system } = getState(); const { disabledTabs } = config; @@ -21,7 +22,12 @@ export const addSocketConnectedEventListener = () => { dispatch(receivedOpenAPISchema()); } - dispatch(api.util.resetApiState()); + if (system.isInitialized) { + // only reset the query caches if this connect event is a *reconnect* event + dispatch(api.util.resetApiState()); + } else { + dispatch(isInitializedChanged(true)); + } // pass along the socket event as an application action dispatch(appSocketConnected(action.payload)); diff --git a/invokeai/frontend/web/src/app/store/middleware/listenerMiddleware/listeners/socketio/socketDisconnected.ts b/invokeai/frontend/web/src/app/store/middleware/listenerMiddleware/listeners/socketio/socketDisconnected.ts index 05ff8cd0e6..80e6fb0813 100644 --- a/invokeai/frontend/web/src/app/store/middleware/listenerMiddleware/listeners/socketio/socketDisconnected.ts +++ b/invokeai/frontend/web/src/app/store/middleware/listenerMiddleware/listeners/socketio/socketDisconnected.ts @@ -1,5 +1,4 @@ import { logger } from 'app/logging/logger'; -import { api } from 'services/api'; import { appSocketDisconnected, socketDisconnected, @@ -13,8 +12,6 @@ export const addSocketDisconnectedEventListener = () => { const log = logger('socketio'); log.debug('Disconnected'); - dispatch(api.util.resetApiState()); - // pass along the socket event as an application action dispatch(appSocketDisconnected(action.payload)); }, diff --git a/invokeai/frontend/web/src/features/system/store/systemPersistDenylist.ts b/invokeai/frontend/web/src/features/system/store/systemPersistDenylist.ts index df0d349d00..b1d952ed83 100644 --- a/invokeai/frontend/web/src/features/system/store/systemPersistDenylist.ts +++ b/invokeai/frontend/web/src/features/system/store/systemPersistDenylist.ts @@ -1,6 +1,7 @@ import { SystemState } from './types'; export const systemPersistDenylist: (keyof SystemState)[] = [ + 'isInitialized', 'isConnected', 'denoiseProgress', 'status', diff --git a/invokeai/frontend/web/src/features/system/store/systemSlice.ts b/invokeai/frontend/web/src/features/system/store/systemSlice.ts index 1fb50562c4..7d31838afd 100644 --- a/invokeai/frontend/web/src/features/system/store/systemSlice.ts +++ b/invokeai/frontend/web/src/features/system/store/systemSlice.ts @@ -24,6 +24,7 @@ import { SystemState, LANGUAGES } from './types'; import { zPydanticValidationError } from './zodSchemas'; export const initialSystemState: SystemState = { + isInitialized: false, isConnected: false, shouldConfirmOnDelete: true, enableImageDebugging: false, @@ -82,6 +83,9 @@ export const systemSlice = createSlice({ ) { state.shouldEnableInformationalPopovers = action.payload; }, + isInitializedChanged(state, action: PayloadAction) { + state.isInitialized = action.payload; + }, }, extraReducers(builder) { /** @@ -242,6 +246,7 @@ export const { shouldUseNSFWCheckerChanged, shouldUseWatermarkerChanged, setShouldEnableInformationalPopovers, + isInitializedChanged, } = systemSlice.actions; export default systemSlice.reducer; diff --git a/invokeai/frontend/web/src/features/system/store/types.ts b/invokeai/frontend/web/src/features/system/store/types.ts index 5b83e35d22..95c63a1415 100644 --- a/invokeai/frontend/web/src/features/system/store/types.ts +++ b/invokeai/frontend/web/src/features/system/store/types.ts @@ -21,6 +21,7 @@ export type DenoiseProgress = { }; export interface SystemState { + isInitialized: boolean; isConnected: boolean; shouldConfirmOnDelete: boolean; enableImageDebugging: boolean;