From: Eduardo San Martin Morote Date: Fri, 30 Jul 2021 14:12:59 +0000 (+0200) Subject: fix(store): keep original refs with $reset X-Git-Tag: v2.0.0-rc.1~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a7dadfff8aae4abb83696a47904b030295408a09;p=thirdparty%2Fvuejs%2Fpinia.git fix(store): keep original refs with $reset Fix #593 --- diff --git a/__tests__/store.spec.ts b/__tests__/store.spec.ts index 394d981a..07d1e4da 100644 --- a/__tests__/store.spec.ts +++ b/__tests__/store.spec.ts @@ -93,6 +93,9 @@ describe('Store', () => { a: { b: 'string' }, }, }) + + // https://github.com/posva/pinia/issues/593 + expect(store.nested.foo).toBe('bar') }) it('can create an empty state if no state option is provided', () => { diff --git a/src/store.ts b/src/store.ts index 491250d2..c2cf11cb 100644 --- a/src/store.ts +++ b/src/store.ts @@ -95,9 +95,6 @@ function createOptionsStore< hot?: boolean ): Store { const { state, actions, getters } = options - function $reset() { - pinia.state.value[id] = state ? state() : {} - } const initialState: StateTree | undefined = pinia.state.value[id] @@ -105,9 +102,8 @@ function createOptionsStore< function setup() { if (!initialState && (!__DEV__ || !hot)) { - $reset() + pinia.state.value[id] = state ? state() : {} } - // pinia.state.value[id] = state ? state() : {} // avoid creating a state in pinia.state.value const localState = @@ -135,7 +131,13 @@ function createOptionsStore< store = createSetupStore(id, setup, options, pinia, hot) - store.$reset = $reset + store.$reset = function $reset() { + const newState = state ? state() : {} + // we use a patch to group all changes into one single subscription + this.$patch(($state) => { + assign($state, newState) + }) + } return store as any }