From 11b92fd9e4b1d2402df79de9fc47c32c1b9ce726 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Thu, 5 May 2022 13:18:47 +0200 Subject: [PATCH] fix: correctly detect option stores Fix #1272 --- packages/pinia/__tests__/ssr.spec.ts | 7 +++++++ packages/pinia/src/store.ts | 21 ++++++++------------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/packages/pinia/__tests__/ssr.spec.ts b/packages/pinia/__tests__/ssr.spec.ts index f179d280..18b478d5 100644 --- a/packages/pinia/__tests__/ssr.spec.ts +++ b/packages/pinia/__tests__/ssr.spec.ts @@ -144,6 +144,13 @@ describe('SSR', () => { `) }) + it('accepts a store with no state', () => { + const pinia = createPinia() + pinia.state.value.a = { start: 'start' } + const store = defineStore('a', {})(pinia) + expect(store.$state).toEqual({ start: 'start' }) + }) + describe('Setup Store', () => { const useStore = defineStore('main', () => { const count = ref(0) diff --git a/packages/pinia/src/store.ts b/packages/pinia/src/store.ts index 65bd466a..d8631f46 100644 --- a/packages/pinia/src/store.ts +++ b/packages/pinia/src/store.ts @@ -163,7 +163,7 @@ function createOptionsStore< ) } - store = createSetupStore(id, setup, options, pinia, hot) + store = createSetupStore(id, setup, options, pinia, hot, true) store.$reset = function $reset() { const newState = state ? state() : {} @@ -189,10 +189,10 @@ function createSetupStore< | DefineSetupStoreOptions | DefineStoreOptions = {}, pinia: Pinia, - hot?: boolean + hot?: boolean, + isOptionsStore?: boolean ): Store { let scope!: EffectScope - const buildState = (options as DefineStoreOptions).state const optionsForPlugin: DefineStoreOptionsInPlugin = assign( { actions: {} as A }, @@ -241,7 +241,7 @@ function createSetupStore< // avoid setting the state for option stores are it is set // by the setup - if (!buildState && !initialState && (!__DEV__ || !hot)) { + if (!isOptionsStore && !initialState && (!__DEV__ || !hot)) { /* istanbul ignore if */ if (isVue2) { set(pinia.state.value, $id, {}) @@ -459,7 +459,7 @@ function createSetupStore< set(hotState.value, key, toRef(setupStore as any, key)) // createOptionStore directly sets the state in pinia.state.value so we // can just skip that - } else if (!buildState) { + } else if (!isOptionsStore) { // in setup stores we must hydrate the state and sync pinia state tree with the refs the user just created if (initialState && shouldHydrate(prop)) { if (isRef(prop)) { @@ -507,7 +507,7 @@ function createSetupStore< } else if (__DEV__) { // add getters for devtools if (isComputed(prop)) { - _hmrPayload.getters[key] = buildState + _hmrPayload.getters[key] = isOptionsStore ? // @ts-expect-error options.getters[key] : prop @@ -605,7 +605,7 @@ function createSetupStore< // TODO: does this work in both setup and option store? for (const getterName in newStore._hmrPayload.getters) { const getter: _Method = newStore._hmrPayload.getters[getterName] - const getterValue = buildState + const getterValue = isOptionsStore ? // special handling of options api computed(() => { setActivePinia(pinia) @@ -710,7 +710,7 @@ function createSetupStore< // only apply hydrate to option stores with an initial state in pinia if ( initialState && - buildState && + isOptionsStore && (options as DefineStoreOptions).hydrate ) { ;(options as DefineStoreOptions).hydrate!( @@ -724,11 +724,6 @@ function createSetupStore< return store } -// export function disposeStore(store: StoreGeneric) { -// store._e - -// } - /** * Extract the actions of a store type. Works with both a Setup Store or an * Options Store. -- 2.47.2