]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
refactor(devtools): simplify with proxy
authorEduardo San Martin Morote <posva13@gmail.com>
Tue, 11 May 2021 20:35:48 +0000 (22:35 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Tue, 11 May 2021 20:58:17 +0000 (22:58 +0200)
src/devtools.ts

index 7a14eaf632e3f21b3f5a18e9f183fdd52c227a7e..8b62bb12872892b41e1f7244062c66d53200e832 100644 (file)
@@ -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[]
       )
     }