From 20e2a576c8cae35613600c8bf0cd2871a8dbff7e Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Wed, 12 May 2021 16:33:31 +0200 Subject: [PATCH] refactor(types): use Store with defaults, deprecate GenericStore --- __tests__/onAction.spec.ts | 10 +++++++++- src/devtools/formatting.ts | 8 +++----- src/devtools/plugin.ts | 6 +++--- src/mapHelpers.ts | 3 +-- src/rootStore.ts | 5 ++--- src/store.ts | 3 +-- src/types.ts | 18 ++---------------- 7 files changed, 21 insertions(+), 32 deletions(-) diff --git a/__tests__/onAction.spec.ts b/__tests__/onAction.spec.ts index f4faa6b8..dd3d3cc6 100644 --- a/__tests__/onAction.spec.ts +++ b/__tests__/onAction.spec.ts @@ -68,7 +68,15 @@ describe('Subscriptions', () => { it('calls after with the returned value', async () => { const spy = jest.fn() - store.$onAction(({ after }) => { + store.$onAction(({ after, name, store }) => { + name + if (name === 'upperName') { + after((ret) => { + // @ts-expect-error + ret * 2 + ret.toUpperCase() + }) + } after(spy) }) expect(store.upperName()).toBe('EDUARDO') diff --git a/src/devtools/formatting.ts b/src/devtools/formatting.ts index 0b3aaaf9..54bba476 100644 --- a/src/devtools/formatting.ts +++ b/src/devtools/formatting.ts @@ -1,5 +1,5 @@ import { CustomInspectorNode, CustomInspectorState } from '@vue/devtools-api' -import { GenericStore, GettersTree, MutationType, StateTree } from '../types' +import { Store, GettersTree, MutationType, StateTree } from '../types' import { DebuggerEvent } from 'vue' export function formatDisplay(display: string) { @@ -10,9 +10,7 @@ export function formatDisplay(display: string) { } } -export function formatStoreForInspectorTree( - store: GenericStore -): CustomInspectorNode { +export function formatStoreForInspectorTree(store: Store): CustomInspectorNode { return { id: store.$id, label: store.$id, @@ -21,7 +19,7 @@ export function formatStoreForInspectorTree( } export function formatStoreForInspectorState( - store: GenericStore + store: Store ): CustomInspectorState[string] { const fields: CustomInspectorState[string] = [ { editable: false, key: 'id', value: formatDisplay(store.$id) }, diff --git a/src/devtools/plugin.ts b/src/devtools/plugin.ts index fcba6f69..90cfcd95 100644 --- a/src/devtools/plugin.ts +++ b/src/devtools/plugin.ts @@ -2,7 +2,7 @@ import { setupDevtoolsPlugin, TimelineEvent } from '@vue/devtools-api' import { App } from 'vue' import { PiniaPluginContext, setActivePinia } from '../rootStore' import { - GenericStore, + Store, GettersTree, MutationType, StateTree, @@ -18,7 +18,7 @@ import { /** * Registered stores used for devtools. */ -const registeredStores = /*#__PURE__*/ new Set() +const registeredStores = /*#__PURE__*/ new Set() let isAlreadyInstalled: boolean | undefined // timeline can be paused when directly changing the state @@ -28,7 +28,7 @@ const componentStateTypes: string[] = [] const MUTATIONS_LAYER_ID = 'pinia:mutations' const INSPECTOR_ID = 'pinia' -export function addDevtools(app: App, store: GenericStore) { +export function addDevtools(app: App, store: Store) { registeredStores.add(store) componentStateTypes.push('🍍 ' + store.$id) setupDevtoolsPlugin( diff --git a/src/mapHelpers.ts b/src/mapHelpers.ts index eac63bd0..47c5c9b0 100644 --- a/src/mapHelpers.ts +++ b/src/mapHelpers.ts @@ -1,6 +1,5 @@ import { ComponentPublicInstance } from 'vue' import { - GenericStore, GettersTree, _Method, StateTree, @@ -149,7 +148,7 @@ export type _MapStateObjectReturn< keyof S | keyof G | ((store: Store) => any) > > = { - [key in keyof T]: () => T[key] extends (store: GenericStore) => infer R + [key in keyof T]: () => T[key] extends (store: Store) => infer R ? R : T[key] extends keyof S | keyof G ? Store[T[key]] diff --git a/src/rootStore.ts b/src/rootStore.ts index ec293a34..b1d85d9d 100644 --- a/src/rootStore.ts +++ b/src/rootStore.ts @@ -3,7 +3,6 @@ import { StateTree, StoreWithState, StateDescriptor, - GenericStore, PiniaCustomProperties, _Method, DefineStoreOptions, @@ -54,7 +53,7 @@ export const storesMap = new WeakMap< [ StoreWithState, StateDescriptor, - InjectionKey + InjectionKey ] > >() @@ -105,7 +104,7 @@ declare module '@vue/runtime-core' { * * @internal */ - _pStores?: Record + _pStores?: Record } } diff --git a/src/store.ts b/src/store.ts index ffd31de1..6ed81b73 100644 --- a/src/store.ts +++ b/src/store.ts @@ -25,7 +25,6 @@ import { StateDescriptor, DefineStoreOptions, StoreDefinition, - GenericStore, GettersTree, MutationType, StoreOnActionListener, @@ -100,7 +99,7 @@ function initStore( ): [ StoreWithState, { get: () => S; set: (newValue: S) => void }, - InjectionKey + InjectionKey ] { const pinia = getActivePinia() pinia.state.value[$id] = initialState || buildState() diff --git a/src/types.ts b/src/types.ts index cd48cf30..1f75c0d8 100644 --- a/src/types.ts +++ b/src/types.ts @@ -313,23 +313,9 @@ export interface StoreDefinition< /** * Generic version of Store. + * @deprecated Use Store instead */ -export type GenericStore = Store< - string, - StateTree, - GettersTree, - Record -> - -/** - * Generic version of `StoreDefinition`. - */ -export type GenericStoreDefinition = StoreDefinition< - string, - StateTree, - GettersTree, - Record -> +export type GenericStore = Store /** * Properties that are added to every store by `pinia.use()` -- 2.47.2