@@ -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 (
0 commit comments