From 236314c254583f2b6a595d8eef1ae3c59dd62dda Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Tue, 11 May 2021 22:35:48 +0200 Subject: [PATCH] refactor(devtools): simplify with proxy --- src/devtools.ts | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/src/devtools.ts b/src/devtools.ts index 7a14eaf6..8b62bb12 100644 --- a/src/devtools.ts +++ b/src/devtools.ts @@ -4,7 +4,7 @@ import { setupDevtoolsPlugin, TimelineEvent, } from '@vue/devtools-api' -import { App, computed, DebuggerEvent, reactive, toRefs } from 'vue' +import { App, DebuggerEvent } from 'vue' import { PiniaPluginContext, setActivePinia } from './rootStore' import { GenericStore, @@ -344,7 +344,7 @@ function formatMutationType(type: MutationType): string { } let runningActionId = 0 -let activeAction: number | undefined = -1 +let activeAction: number | undefined /** * pinia.use(devtoolsPlugin) @@ -370,30 +370,20 @@ export function devtoolsPlugin< // @ts-expect-error wrappedActions[actionName] = function () { setActivePinia(pinia) - const keys = Object.keys(options.state?.() || {}) // the running action id is incremented in a before action hook const _actionId = runningActionId - const patchedStore = reactive({ - ...toRefs(store), - ...keys.reduce((stateProperties, stateName) => { - // @ts-expect-error - stateProperties[stateName] = computed({ - get() { - activeAction = _actionId - return store[stateName] - }, - set(value) { - activeAction = _actionId - // @ts-expect-error - return (store[stateName] = value) - }, - }) - return stateProperties - }, {}), - _actionId, + const trackedStore = new Proxy(store, { + get(...args) { + activeAction = _actionId + return Reflect.get(...args) + }, + set(...args) { + activeAction = _actionId + return Reflect.set(...args) + }, }) return actions[actionName].apply( - patchedStore, + trackedStore, (arguments as unknown) as any[] ) } -- 2.47.3