]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
fix(vue2): delay getters until stores are ready when cross using them
authorEduardo San Martin Morote <posva13@gmail.com>
Sun, 12 Sep 2021 10:15:36 +0000 (12:15 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Sun, 12 Sep 2021 10:15:36 +0000 (12:15 +0200)
packages/pinia/src/store.ts
packages/pinia/src/types.ts

index e695d85e70ca2c99785db7d6fca01412e5d18810..06a1aad51e985d8930a536ae2ed4a1d2197d0d79 100644 (file)
@@ -129,6 +129,10 @@ function createOptionsStore<
             setActivePinia(pinia)
             // it was created just before
             const store = pinia._s.get(id)!
+
+            // allow cross using stores
+            if (isVue2 && store._r) return
+
             // @ts-expect-error
             // return getters![name].call(context, context)
             // TODO: avoid reading the getter while assigning with a global variable
@@ -404,6 +408,11 @@ function createSetupStore<
     $dispose,
   } as StoreWithState<Id, S, G, A>
 
+  if (isVue2) {
+    // start as non ready
+    partialStore._r = false
+  }
+
   const store: Store<Id, S, G, A> = reactive(
     assign(
       __DEV__ && IS_CLIENT
@@ -624,6 +633,11 @@ function createSetupStore<
     }
   }
 
+  if (isVue2) {
+    // mark the store as ready before pluginsn
+    store._r = true
+  }
+
   // apply all plugins
   pinia._p.forEach((extender) => {
     /* istanbul ignore else */
index 04d2b3cfb327293ce5cd3a252eb79ef81387f02d..893eb9239f19b9915236876177adc763783d5a08 100644 (file)
@@ -421,6 +421,14 @@ export interface StoreWithState<
    * e.g. devtools plugin stops displaying disposed stores from devtools.
    */
   $dispose(): void
+
+  /**
+   * Vue 2 only. Is the store ready. Used for store cross usage. Getters automatically compute when they are added to
+   * the store, before the store is actually ready, this allows to avoid calling the computed function yet.
+   *
+   * @internal
+   */
+  _r?: boolean
 }
 
 /**