From: Eduardo San Martin Morote Date: Wed, 28 Jul 2021 10:04:48 +0000 (+0200) Subject: fix(hmr): correctly handle updates with id parameter and options X-Git-Tag: v2.0.0-rc.0~12 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=76865e882af7c0fb04ad38bc663209550ab3ed78;p=thirdparty%2Fvuejs%2Fpinia.git fix(hmr): correctly handle updates with id parameter and options --- diff --git a/__tests__/store.spec.ts b/__tests__/store.spec.ts index 0b090072..394d981a 100644 --- a/__tests__/store.spec.ts +++ b/__tests__/store.spec.ts @@ -26,6 +26,22 @@ describe('Store', () => { expect(useStore()).toBe(useStore()) }) + it('works with id as first argument', () => { + setActivePinia(createPinia()) + const useStore = defineStore('main', { + state: () => ({ + a: true, + nested: { + foo: 'foo', + a: { b: 'string' }, + }, + }), + }) + expect(useStore()).toBe(useStore()) + const useStoreEmpty = defineStore('main', {}) + expect(useStoreEmpty()).toBe(useStoreEmpty()) + }) + it('sets the initial state', () => { const store = useStore() expect(store.$state).toEqual({ diff --git a/src/store.ts b/src/store.ts index 3672c9bd..dfe3ff88 100644 --- a/src/store.ts +++ b/src/store.ts @@ -85,11 +85,12 @@ function createOptionsStore< G extends GettersTree, A extends ActionsTree >( + id: Id, options: DefineStoreOptions, pinia: Pinia, hot?: boolean ): Store { - const { id, state, actions, getters } = options + const { state, actions, getters } = options function $reset() { pinia.state.value[id] = state ? state() : {} } @@ -689,7 +690,8 @@ export function defineStore( const isSetupStore = typeof setup === 'function' if (typeof idOrOptions === 'string') { id = idOrOptions - options = setupOptions + // the option store setup will contain the actual options in this case + options = isSetupStore ? setupOptions : setup } else { options = idOrOptions id = idOrOptions.id @@ -711,7 +713,7 @@ export function defineStore( id, isSetupStore ? createSetupStore(id, setup, options, pinia) - : createOptionsStore(options as any, pinia) + : createOptionsStore(id, options as any, pinia) ) if (__DEV__) { @@ -726,11 +728,7 @@ export function defineStore( const hotId = '__hot:' + id const newStore = isSetupStore ? createSetupStore(hotId, setup, options, pinia, true) - : createOptionsStore( - assign({}, options, { id: hotId }) as any, - pinia, - true - ) + : createOptionsStore(hotId, assign({}, options) as any, pinia, true) hot._hotUpdate(newStore)