From fca27ca4fc6779cc9abea8a58c41ea9a7557e69f Mon Sep 17 00:00:00 2001 From: arlo Date: Sun, 4 Feb 2024 18:17:32 +0800 Subject: [PATCH] fix(kit): use factory function to init/reset state --- packages/devtools-kit/src/state/context.ts | 31 ++++++++++++---------- packages/devtools-kit/src/state/state.ts | 29 +++++++++++--------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/packages/devtools-kit/src/state/context.ts b/packages/devtools-kit/src/state/context.ts index e0ed2d3a..b8404bb8 100644 --- a/packages/devtools-kit/src/state/context.ts +++ b/packages/devtools-kit/src/state/context.ts @@ -1,23 +1,26 @@ -import { deepClone, target as global } from '@vue/devtools-shared' +import { target as global } from '@vue/devtools-shared' import type { DevToolsContext } from '../types' import { ROUTER_KEY } from './router' const CONTEXT_KEY = '__VUE_DEVTOOLS_CONTEXT__' -const INITIAL_CONTEXT = { - appRecord: null, - api: null, - inspector: [], - timelineLayer: [], - routerInfo: {}, - router: null, - activeInspectorTreeId: '', - componentPluginHookBuffer: [], -} as unknown as DevToolsContext -global[CONTEXT_KEY] ??= deepClone(INITIAL_CONTEXT) +function initContextFactory() { + return { + appRecord: null, + api: null, + inspector: [], + timelineLayer: [], + routerInfo: {}, + router: null, + activeInspectorTreeId: '', + componentPluginHookBuffer: [], + } as unknown as DevToolsContext +} + +global[CONTEXT_KEY] ??= initContextFactory() -function resetDevToolsContext() { - global[CONTEXT_KEY] = deepClone(INITIAL_CONTEXT) +export function resetDevToolsContext() { + global[CONTEXT_KEY] = initContextFactory() } export const devtoolsContext = new Proxy(global[CONTEXT_KEY], { diff --git a/packages/devtools-kit/src/state/state.ts b/packages/devtools-kit/src/state/state.ts index fe0480a4..135ffa96 100644 --- a/packages/devtools-kit/src/state/state.ts +++ b/packages/devtools-kit/src/state/state.ts @@ -6,23 +6,26 @@ import { DevToolsEvents, apiHooks } from '../api' export type { DevToolsState } from '../types' const STATE_KEY = '__VUE_DEVTOOLS_GLOBAL_STATE__' -const INITIAL_STATE = { - connected: false, - clientConnected: false, - appRecords: [], - activeAppRecord: null, - selectedComponentId: null, - pluginBuffer: [], - tabs: [], - commands: [], - vitePluginDetected: false, - activeAppRecordId: null, + +function initStateFactory() { + return { + connected: false, + clientConnected: false, + appRecords: [], + activeAppRecord: null, + selectedComponentId: null, + pluginBuffer: [], + tabs: [], + commands: [], + vitePluginDetected: false, + activeAppRecordId: null, + } } -global[STATE_KEY] ??= INITIAL_STATE +global[STATE_KEY] ??= initStateFactory() export function resetDevToolsState() { - global[STATE_KEY] = INITIAL_STATE + global[STATE_KEY] = initStateFactory() } export const callStateUpdatedHook = debounce((state: DevToolsState, oldState: DevToolsState) => {