From ed48b0093c2a58caf8bb4548cceb13eeaf5f1378 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Sun, 12 Sep 2021 12:15:36 +0200 Subject: [PATCH] fix(vue2): delay getters until stores are ready when cross using them --- packages/pinia/src/store.ts | 14 ++++++++++++++ packages/pinia/src/types.ts | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/packages/pinia/src/store.ts b/packages/pinia/src/store.ts index e695d85e..06a1aad5 100644 --- a/packages/pinia/src/store.ts +++ b/packages/pinia/src/store.ts @@ -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 + if (isVue2) { + // start as non ready + partialStore._r = false + } + const store: Store = 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 */ diff --git a/packages/pinia/src/types.ts b/packages/pinia/src/types.ts index 04d2b3cf..893eb923 100644 --- a/packages/pinia/src/types.ts +++ b/packages/pinia/src/types.ts @@ -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 } /** -- 2.47.3