]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
feat(devtools): allow resetting directly from devtools
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 15 Nov 2021 13:31:31 +0000 (14:31 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 15 Nov 2021 13:31:31 +0000 (14:31 +0100)
packages/pinia/src/devtools/plugin.ts
packages/pinia/src/types.ts

index c62e660d0bddc105b2fdc4587ce9c5f78fb6ed7d..383ee2b3a3a5a093b19d90624974de4a39c97cf7 100644 (file)
@@ -125,7 +125,20 @@ export function registerPiniaDevtools(app: DevtoolsApp, pinia: Pinia) {
               type: getStoreType(store.$id),
               key: 'state',
               editable: true,
-              value: store.$state,
+              value: store._isOptionsAPI
+                ? {
+                    _custom: {
+                      value: store.$state,
+                      actions: [
+                        {
+                          icon: 'restore',
+                          tooltip: 'Reset the state of this store',
+                          action: () => store.$reset(),
+                        },
+                      ],
+                    },
+                  }
+                : store.$state,
             })
 
             if (store._getters && store._getters.length) {
@@ -434,7 +447,9 @@ let runningActionId = 0
 let activeAction: number | undefined
 
 /**
- * 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.
+ * 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.
  *
  * @param store - store to patch
  * @param actionNames - list of actionst to patch
@@ -484,6 +499,11 @@ export function devtoolsPlugin<
     return
   }
 
+  // detect option api vs setup api
+  if (options.state) {
+    store._isOptionsAPI = true
+  }
+
   // 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') {
index 1f0c25f3aa2441aee0b3466c8778b4487d2ebabf..356c1f34fdb45e268c67b2536a59fdf446b0f93c 100644 (file)
@@ -265,6 +265,13 @@ export interface StoreProperties<Id extends string> {
    */
   _getters?: string[]
 
+  /**
+   * Used (and added) by devtools plugin to detect Setup vs Options API usage.
+   *
+   * @internal
+   */
+  _isOptionsAPI?: boolean
+
   /**
    * Used by devtools plugin to retrieve properties added with plugins. Removed
    * in production. Can be used by the user to add property keys of the store