Skip to content

Commit

Permalink
fix(devtools): avoid infinite loop when cross using stores
Browse files Browse the repository at this point in the history
Fix #541
  • Loading branch information
posva committed Jun 24, 2021
1 parent 8c89a48 commit 55c651d
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/devtools/plugin.ts
@@ -1,5 +1,5 @@
import { setupDevtoolsPlugin, TimelineEvent } from '@vue/devtools-api'
import { App, ComponentPublicInstance } from 'vue'
import { App, ComponentPublicInstance, toRaw } from 'vue'
import { Pinia, PiniaPluginContext, setActivePinia } from '../rootStore'
import {
Store,
Expand Down Expand Up @@ -368,21 +368,20 @@ export function devtoolsPlugin<
G extends GettersTree<S> = GettersTree<S>,
A /* extends ActionsTree */ = ActionsTree
>({ app, store, options, pinia }: PiniaPluginContext<Id, S, G, A>) {
const wrappedActions = {} as A

// original actions of the store as they are given by pinia. We are going to override them
const actions = Object.keys(options.actions || ({} as A)).reduce(
(storeActions, actionName) => {
// @ts-expect-error
storeActions[actionName] = store[actionName]
// use toRaw to avoid tracking #541
storeActions[actionName] = toRaw(store)[actionName]
return storeActions
},
{} as ActionsTree
)

for (const actionName in actions) {
// @ts-expect-error
wrappedActions[actionName] = function () {
store[actionName] = function () {
setActivePinia(pinia)
// the running action id is incremented in a before action hook
const _actionId = runningActionId
Expand All @@ -408,7 +407,4 @@ export function devtoolsPlugin<
// @ts-expect-error: FIXME: if possible...
store
)

// avoid returning to not display them in devtools
Object.assign(store, wrappedActions)
}

0 comments on commit 55c651d

Please sign in to comment.