Skip to content

Commit 683efe1

Browse files
committed
fix(devtools): group setup store sync actions mutations
1 parent 8225440 commit 683efe1

File tree

3 files changed

+45
-36
lines changed

3 files changed

+45
-36
lines changed

packages/pinia/src/devtools/actions.ts

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import { Pinia } from '../rootStore'
22
import { saveAs } from './file-saver'
33
import { toastMessage } from './utils'
44

5+
/**
6+
* This file contain devtools actions, they are not Pinia actions.
7+
*/
8+
9+
// ---
10+
511
export function checkClipboardAccess() {
612
if (!('clipboard' in navigator)) {
713
toastMessage(`Your browser doesn't support the Clipboard API`, 'error')

packages/pinia/src/devtools/plugin.ts

+38-36
Original file line numberDiff line numberDiff line change
@@ -428,9 +428,6 @@ function addStoreToDevtools(app: DevtoolsApp, store: StoreGeneric) {
428428
groupId: activeAction,
429429
}
430430

431-
// reset for the next mutation
432-
activeAction = undefined
433-
434431
if (type === MutationType.patchFunction) {
435432
eventData.subtitle = '⤵️'
436433
} else if (type === MutationType.patchObject) {
@@ -510,7 +507,11 @@ let activeAction: number | undefined
510507
* @param store - store to patch
511508
* @param actionNames - list of actionst to patch
512509
*/
513-
function patchActionForGrouping(store: StoreGeneric, actionNames: string[]) {
510+
function patchActionForGrouping(
511+
store: StoreGeneric,
512+
actionNames: string[],
513+
wrapWithProxy: boolean
514+
) {
514515
// original actions of the store as they are given by pinia. We are going to override them
515516
const actions = actionNames.reduce((storeActions, actionName) => {
516517
// use toRaw to avoid tracking #541
@@ -520,23 +521,30 @@ function patchActionForGrouping(store: StoreGeneric, actionNames: string[]) {
520521

521522
for (const actionName in actions) {
522523
store[actionName] = function () {
523-
// setActivePinia(store._p)
524524
// the running action id is incremented in a before action hook
525525
const _actionId = runningActionId
526-
const trackedStore = new Proxy(store, {
527-
get(...args) {
528-
activeAction = _actionId
529-
return Reflect.get(...args)
530-
},
531-
set(...args) {
532-
activeAction = _actionId
533-
return Reflect.set(...args)
534-
},
535-
})
536-
return actions[actionName].apply(
526+
const trackedStore = wrapWithProxy
527+
? new Proxy(store, {
528+
get(...args) {
529+
activeAction = _actionId
530+
return Reflect.get(...args)
531+
},
532+
set(...args) {
533+
activeAction = _actionId
534+
return Reflect.set(...args)
535+
},
536+
})
537+
: store
538+
539+
// For Setup Stores we need https://github.com/tc39/proposal-async-context
540+
activeAction = _actionId
541+
const retValue = actions[actionName].apply(
537542
trackedStore,
538543
arguments as unknown as any[]
539544
)
545+
// this is safer as async actions in Setup Stores would associate mutations done outside of the action
546+
activeAction = undefined
547+
return retValue
540548
}
541549
}
542550
}
@@ -556,29 +564,23 @@ export function devtoolsPlugin<
556564
}
557565

558566
// detect option api vs setup api
559-
if (options.state) {
560-
store._isOptionsAPI = true
561-
}
567+
store._isOptionsAPI = !!options.state
568+
569+
patchActionForGrouping(
570+
store as StoreGeneric,
571+
Object.keys(options.actions),
572+
store._isOptionsAPI
573+
)
562574

563-
// only wrap actions in option-defined stores as this technique relies on
564-
// wrapping the context of the action with a proxy
565-
if (typeof options.state === 'function') {
575+
// Upgrade the HMR to also update the new actions
576+
const originalHotUpdate = store._hotUpdate
577+
toRaw(store)._hotUpdate = function (newStore) {
578+
originalHotUpdate.apply(this, arguments as any)
566579
patchActionForGrouping(
567-
// @ts-expect-error: can cast the store...
568-
store,
569-
Object.keys(options.actions)
580+
store as StoreGeneric,
581+
Object.keys(newStore._hmrPayload.actions),
582+
!!store._isOptionsAPI
570583
)
571-
572-
const originalHotUpdate = store._hotUpdate
573-
574-
// Upgrade the HMR to also update the new actions
575-
toRaw(store)._hotUpdate = function (newStore) {
576-
originalHotUpdate.apply(this, arguments as any)
577-
patchActionForGrouping(
578-
store as StoreGeneric,
579-
Object.keys(newStore._hmrPayload.actions)
580-
)
581-
}
582584
}
583585

584586
addStoreToDevtools(

packages/pinia/src/devtools/utils.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export function toastMessage(
1313
const piniaMessage = '🍍 ' + message
1414

1515
if (typeof __VUE_DEVTOOLS_TOAST__ === 'function') {
16+
// No longer available :(
1617
__VUE_DEVTOOLS_TOAST__(piniaMessage, type)
1718
} else if (type === 'error') {
1819
console.error(piniaMessage)

0 commit comments

Comments
 (0)