Skip to content

Commit

Permalink
fix(kit): use factory function to init/reset state
Browse files Browse the repository at this point in the history
  • Loading branch information
webfansplz committed Feb 4, 2024
1 parent 54e8493 commit fca27ca
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
31 changes: 17 additions & 14 deletions packages/devtools-kit/src/state/context.ts
Original file line number Diff line number Diff line change
@@ -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], {
Expand Down
29 changes: 16 additions & 13 deletions packages/devtools-kit/src/state/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit fca27ca

Please sign in to comment.