Skip to content

Commit 44fa896

Browse files
committed
feat(devtools): allow resetting directly from devtools
1 parent ce8f1e5 commit 44fa896

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

packages/pinia/src/devtools/plugin.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,20 @@ export function registerPiniaDevtools(app: DevtoolsApp, pinia: Pinia) {
125125
type: getStoreType(store.$id),
126126
key: 'state',
127127
editable: true,
128-
value: store.$state,
128+
value: store._isOptionsAPI
129+
? {
130+
_custom: {
131+
value: store.$state,
132+
actions: [
133+
{
134+
icon: 'restore',
135+
tooltip: 'Reset the state of this store',
136+
action: () => store.$reset(),
137+
},
138+
],
139+
},
140+
}
141+
: store.$state,
129142
})
130143

131144
if (store._getters && store._getters.length) {
@@ -434,7 +447,9 @@ let runningActionId = 0
434447
let activeAction: number | undefined
435448

436449
/**
437-
* Patches a store to enable action grouping in devtools by wrapping the store with a Proxy that is passed as the context of all actions, allowing us to set `runningAction` on each access and effectively associating any state mutation to the action.
450+
* Patches a store to enable action grouping in devtools by wrapping the store with a Proxy that is passed as the
451+
* context of all actions, allowing us to set `runningAction` on each access and effectively associating any state
452+
* mutation to the action.
438453
*
439454
* @param store - store to patch
440455
* @param actionNames - list of actionst to patch
@@ -484,6 +499,11 @@ export function devtoolsPlugin<
484499
return
485500
}
486501

502+
// detect option api vs setup api
503+
if (options.state) {
504+
store._isOptionsAPI = true
505+
}
506+
487507
// only wrap actions in option-defined stores as this technique relies on
488508
// wrapping the context of the action with a proxy
489509
if (typeof options.state === 'function') {

packages/pinia/src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,13 @@ export interface StoreProperties<Id extends string> {
265265
*/
266266
_getters?: string[]
267267

268+
/**
269+
* Used (and added) by devtools plugin to detect Setup vs Options API usage.
270+
*
271+
* @internal
272+
*/
273+
_isOptionsAPI?: boolean
274+
268275
/**
269276
* Used by devtools plugin to retrieve properties added with plugins. Removed
270277
* in production. Can be used by the user to add property keys of the store

0 commit comments

Comments
 (0)