Skip to content

Commit

Permalink
fix(devtools): group setup store sync actions mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Jun 14, 2023
1 parent 8225440 commit 683efe1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 36 deletions.
6 changes: 6 additions & 0 deletions packages/pinia/src/devtools/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import { Pinia } from '../rootStore'
import { saveAs } from './file-saver'
import { toastMessage } from './utils'

/**
* This file contain devtools actions, they are not Pinia actions.
*/

// ---

export function checkClipboardAccess() {
if (!('clipboard' in navigator)) {
toastMessage(`Your browser doesn't support the Clipboard API`, 'error')
Expand Down
74 changes: 38 additions & 36 deletions packages/pinia/src/devtools/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,9 +428,6 @@ function addStoreToDevtools(app: DevtoolsApp, store: StoreGeneric) {
groupId: activeAction,
}

// reset for the next mutation
activeAction = undefined

if (type === MutationType.patchFunction) {
eventData.subtitle = '猡碉笍'
} else if (type === MutationType.patchObject) {
Expand Down Expand Up @@ -510,7 +507,11 @@ let activeAction: number | undefined
* @param store - store to patch
* @param actionNames - list of actionst to patch
*/
function patchActionForGrouping(store: StoreGeneric, actionNames: string[]) {
function patchActionForGrouping(
store: StoreGeneric,
actionNames: string[],
wrapWithProxy: boolean
) {
// original actions of the store as they are given by pinia. We are going to override them
const actions = actionNames.reduce((storeActions, actionName) => {
// use toRaw to avoid tracking #541
Expand All @@ -520,23 +521,30 @@ function patchActionForGrouping(store: StoreGeneric, actionNames: string[]) {

for (const actionName in actions) {
store[actionName] = function () {
// setActivePinia(store._p)
// the running action id is incremented in a before action hook
const _actionId = runningActionId
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(
const trackedStore = wrapWithProxy
? new Proxy(store, {
get(...args) {
activeAction = _actionId
return Reflect.get(...args)
},
set(...args) {
activeAction = _actionId
return Reflect.set(...args)
},
})
: store

// For Setup Stores we need https://github.com/tc39/proposal-async-context
activeAction = _actionId
const retValue = actions[actionName].apply(
trackedStore,
arguments as unknown as any[]
)
// this is safer as async actions in Setup Stores would associate mutations done outside of the action
activeAction = undefined
return retValue
}
}
}
Expand All @@ -556,29 +564,23 @@ export function devtoolsPlugin<
}

// detect option api vs setup api
if (options.state) {
store._isOptionsAPI = true
}
store._isOptionsAPI = !!options.state

patchActionForGrouping(
store as StoreGeneric,
Object.keys(options.actions),
store._isOptionsAPI
)

// only wrap actions in option-defined stores as this technique relies on
// wrapping the context of the action with a proxy
if (typeof options.state === 'function') {
// Upgrade the HMR to also update the new actions
const originalHotUpdate = store._hotUpdate
toRaw(store)._hotUpdate = function (newStore) {
originalHotUpdate.apply(this, arguments as any)
patchActionForGrouping(
// @ts-expect-error: can cast the store...
store,
Object.keys(options.actions)
store as StoreGeneric,
Object.keys(newStore._hmrPayload.actions),
!!store._isOptionsAPI
)

const originalHotUpdate = store._hotUpdate

// Upgrade the HMR to also update the new actions
toRaw(store)._hotUpdate = function (newStore) {
originalHotUpdate.apply(this, arguments as any)
patchActionForGrouping(
store as StoreGeneric,
Object.keys(newStore._hmrPayload.actions)
)
}
}

addStoreToDevtools(
Expand Down
1 change: 1 addition & 0 deletions packages/pinia/src/devtools/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export function toastMessage(
const piniaMessage = '馃崓 ' + message

if (typeof __VUE_DEVTOOLS_TOAST__ === 'function') {
// No longer available :(
__VUE_DEVTOOLS_TOAST__(piniaMessage, type)
} else if (type === 'error') {
console.error(piniaMessage)
Expand Down

0 comments on commit 683efe1

Please sign in to comment.