]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
fix(hmr): correctly handle updates with id parameter and options
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 28 Jul 2021 10:04:48 +0000 (12:04 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 28 Jul 2021 10:04:48 +0000 (12:04 +0200)
__tests__/store.spec.ts
src/store.ts

index 0b09007217403d58954d29e89cab61b44b89ab0a..394d981a8676cad20237e34a982ef291d5094c45 100644 (file)
@@ -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({
index 3672c9bd83dc1962520136cb473aa2b543ea7205..dfe3ff8811679e1540002f78d41388c307b5381f 100644 (file)
@@ -85,11 +85,12 @@ function createOptionsStore<
   G extends GettersTree<S>,
   A extends ActionsTree
 >(
+  id: Id,
   options: DefineStoreOptions<Id, S, G, A>,
   pinia: Pinia,
   hot?: boolean
 ): Store<Id, S, G, A> {
-  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)