]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
fix(store): keep original refs with $reset
authorEduardo San Martin Morote <posva13@gmail.com>
Fri, 30 Jul 2021 14:12:59 +0000 (16:12 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Fri, 30 Jul 2021 14:12:59 +0000 (16:12 +0200)
Fix #593

__tests__/store.spec.ts
src/store.ts

index 394d981a8676cad20237e34a982ef291d5094c45..07d1e4da60a7fbbb6cb17798c4d03ee58cd34136 100644 (file)
@@ -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', () => {
index 491250d22ea163718ee1d333aef349ae0f832154..c2cf11cbecad672cf8487d7284b25e76af64da62 100644 (file)
@@ -95,9 +95,6 @@ function createOptionsStore<
   hot?: boolean
 ): Store<Id, S, G, A> {
   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
 }