Skip to content

Commit 11b92fd

Browse files
committed
fix: correctly detect option stores
Fix #1272
1 parent 01604e9 commit 11b92fd

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

packages/pinia/__tests__/ssr.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,13 @@ describe('SSR', () => {
144144
`)
145145
})
146146

147+
it('accepts a store with no state', () => {
148+
const pinia = createPinia()
149+
pinia.state.value.a = { start: 'start' }
150+
const store = defineStore('a', {})(pinia)
151+
expect(store.$state).toEqual({ start: 'start' })
152+
})
153+
147154
describe('Setup Store', () => {
148155
const useStore = defineStore('main', () => {
149156
const count = ref(0)

packages/pinia/src/store.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ function createOptionsStore<
163163
)
164164
}
165165

166-
store = createSetupStore(id, setup, options, pinia, hot)
166+
store = createSetupStore(id, setup, options, pinia, hot, true)
167167

168168
store.$reset = function $reset() {
169169
const newState = state ? state() : {}
@@ -189,10 +189,10 @@ function createSetupStore<
189189
| DefineSetupStoreOptions<Id, S, G, A>
190190
| DefineStoreOptions<Id, S, G, A> = {},
191191
pinia: Pinia,
192-
hot?: boolean
192+
hot?: boolean,
193+
isOptionsStore?: boolean
193194
): Store<Id, S, G, A> {
194195
let scope!: EffectScope
195-
const buildState = (options as DefineStoreOptions<Id, S, G, A>).state
196196

197197
const optionsForPlugin: DefineStoreOptionsInPlugin<Id, S, G, A> = assign(
198198
{ actions: {} as A },
@@ -241,7 +241,7 @@ function createSetupStore<
241241

242242
// avoid setting the state for option stores are it is set
243243
// by the setup
244-
if (!buildState && !initialState && (!__DEV__ || !hot)) {
244+
if (!isOptionsStore && !initialState && (!__DEV__ || !hot)) {
245245
/* istanbul ignore if */
246246
if (isVue2) {
247247
set(pinia.state.value, $id, {})
@@ -459,7 +459,7 @@ function createSetupStore<
459459
set(hotState.value, key, toRef(setupStore as any, key))
460460
// createOptionStore directly sets the state in pinia.state.value so we
461461
// can just skip that
462-
} else if (!buildState) {
462+
} else if (!isOptionsStore) {
463463
// in setup stores we must hydrate the state and sync pinia state tree with the refs the user just created
464464
if (initialState && shouldHydrate(prop)) {
465465
if (isRef(prop)) {
@@ -507,7 +507,7 @@ function createSetupStore<
507507
} else if (__DEV__) {
508508
// add getters for devtools
509509
if (isComputed(prop)) {
510-
_hmrPayload.getters[key] = buildState
510+
_hmrPayload.getters[key] = isOptionsStore
511511
? // @ts-expect-error
512512
options.getters[key]
513513
: prop
@@ -605,7 +605,7 @@ function createSetupStore<
605605
// TODO: does this work in both setup and option store?
606606
for (const getterName in newStore._hmrPayload.getters) {
607607
const getter: _Method = newStore._hmrPayload.getters[getterName]
608-
const getterValue = buildState
608+
const getterValue = isOptionsStore
609609
? // special handling of options api
610610
computed(() => {
611611
setActivePinia(pinia)
@@ -710,7 +710,7 @@ function createSetupStore<
710710
// only apply hydrate to option stores with an initial state in pinia
711711
if (
712712
initialState &&
713-
buildState &&
713+
isOptionsStore &&
714714
(options as DefineStoreOptions<Id, S, G, A>).hydrate
715715
) {
716716
;(options as DefineStoreOptions<Id, S, G, A>).hydrate!(
@@ -724,11 +724,6 @@ function createSetupStore<
724724
return store
725725
}
726726

727-
// export function disposeStore(store: StoreGeneric) {
728-
// store._e
729-
730-
// }
731-
732727
/**
733728
* Extract the actions of a store type. Works with both a Setup Store or an
734729
* Options Store.

0 commit comments

Comments
 (0)