From: Eduardo San Martin Morote Date: Sun, 2 May 2021 13:20:28 +0000 (+0200) Subject: feat(devtools): add getters to devtools X-Git-Tag: v2.0.0-alpha.14~17 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=c4bf761e95b79a0831061e490ba1d69802bc9d95;p=thirdparty%2Fvuejs%2Fpinia.git feat(devtools): add getters to devtools --- diff --git a/src/devtools.ts b/src/devtools.ts index 2e4dc603..68da5c5a 100644 --- a/src/devtools.ts +++ b/src/devtools.ts @@ -4,7 +4,7 @@ import { setupDevtoolsPlugin, } from '@vue/devtools-api' import { App } from 'vue' -import { GenericStore } from './types' +import { GenericStore, GettersTree, StateTree } from './types' function formatDisplay(display: string) { return { @@ -167,6 +167,14 @@ function formatStoreForInspectorState( const fields: CustomInspectorState[string] = [ { editable: false, key: 'id', value: formatDisplay(store.$id) }, { editable: true, key: 'state', value: store.$state }, + { + editable: false, + key: 'getters', + value: (store._getters || []).reduce((getters, key) => { + getters[key] = store[key] + return getters + }, {} as GettersTree), + }, ] return fields diff --git a/src/store.ts b/src/store.ts index 196f03c4..73060a8a 100644 --- a/src/store.ts +++ b/src/store.ts @@ -267,6 +267,10 @@ function buildStoreToUse< // created. Object.defineProperty(store, '$state', descriptor) + if (IS_CLIENT && __BROWSER__ && __DEV__) { + store._getters = Object.keys(getters) + } + // apply all plugins pinia._p.forEach((extender) => { assign(store, extender({ store, app: pinia._a, pinia, options })) diff --git a/src/types.ts b/src/types.ts index 6163b00e..e8818036 100644 --- a/src/types.ts +++ b/src/types.ts @@ -64,6 +64,13 @@ export interface StoreWithState { */ _p: Pinia + /** + * Used by devtools plugin to retrieve getters. Removed in production + * + * @internal + */ + _getters?: string[] + /** * Applies a state patch to current state. Allows passing nested values *