@@ -428,9 +428,6 @@ function addStoreToDevtools(app: DevtoolsApp, store: StoreGeneric) {
428
428
groupId : activeAction ,
429
429
}
430
430
431
- // reset for the next mutation
432
- activeAction = undefined
433
-
434
431
if ( type === MutationType . patchFunction ) {
435
432
eventData . subtitle = '⤵️'
436
433
} else if ( type === MutationType . patchObject ) {
@@ -510,7 +507,11 @@ let activeAction: number | undefined
510
507
* @param store - store to patch
511
508
* @param actionNames - list of actionst to patch
512
509
*/
513
- function patchActionForGrouping ( store : StoreGeneric , actionNames : string [ ] ) {
510
+ function patchActionForGrouping (
511
+ store : StoreGeneric ,
512
+ actionNames : string [ ] ,
513
+ wrapWithProxy : boolean
514
+ ) {
514
515
// original actions of the store as they are given by pinia. We are going to override them
515
516
const actions = actionNames . reduce ( ( storeActions , actionName ) => {
516
517
// use toRaw to avoid tracking #541
@@ -520,23 +521,30 @@ function patchActionForGrouping(store: StoreGeneric, actionNames: string[]) {
520
521
521
522
for ( const actionName in actions ) {
522
523
store [ actionName ] = function ( ) {
523
- // setActivePinia(store._p)
524
524
// the running action id is incremented in a before action hook
525
525
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 (
537
542
trackedStore ,
538
543
arguments as unknown as any [ ]
539
544
)
545
+ // this is safer as async actions in Setup Stores would associate mutations done outside of the action
546
+ activeAction = undefined
547
+ return retValue
540
548
}
541
549
}
542
550
}
@@ -556,29 +564,23 @@ export function devtoolsPlugin<
556
564
}
557
565
558
566
// 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
+ )
562
574
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 )
566
579
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
570
583
)
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
- }
582
584
}
583
585
584
586
addStoreToDevtools (
0 commit comments