From 21f917f057522b7e2ff70fa8517c5941c644a577 Mon Sep 17 00:00:00 2001 From: Marshall Thompson Date: Mon, 3 May 2021 02:21:19 -0600 Subject: [PATCH] feat(devtools): work with stores added before app.use (#444) * feat: Stores add to devtools before app.use(pinia) * fix(view): Remove watcher from getClientApp * fix(view): must resolve the original promise Co-authored-by: Eduardo San Martin Morote Co-authored-by: Eduardo San Martin Morote --- src/rootStore.ts | 11 +++++++---- src/store.ts | 18 +----------------- 2 files changed, 8 insertions(+), 21 deletions(-) diff --git a/src/rootStore.ts b/src/rootStore.ts index c3b601f2..7838cf84 100644 --- a/src/rootStore.ts +++ b/src/rootStore.ts @@ -61,11 +61,14 @@ export const storesMap = new WeakMap< >() /** - * Client-side application instance used for devtools + * Expose the client-side application instance used for devtools */ -export let clientApp: App | undefined /*#__PURE__*/ -export const setClientApp = (app: App) => (clientApp = app) -export const getClientApp = () => clientApp +let clientAppPromise: Promise | undefined +let resolveApp: ((app: App) => void) | undefined +export const setClientApp = (app: App) => resolveApp && resolveApp(app) +export const getClientApp = () => + clientAppPromise || + (clientAppPromise = new Promise((resolve) => (resolveApp = resolve))) /** * Context argument passed to Pinia plugins. diff --git a/src/store.ts b/src/store.ts index cf806947..64bff32b 100644 --- a/src/store.ts +++ b/src/store.ts @@ -274,9 +274,6 @@ function buildStoreToUse< return store } -// only warn the dev once -let isDevWarned: boolean | undefined - /** * Creates a `useStore` function that retrieves the store instance * @param options - options to define the store @@ -334,20 +331,7 @@ export function defineStore< __BROWSER__ && __DEV__ /*|| __FEATURE_PROD_DEVTOOLS__*/ ) { - const app = getClientApp() - /* istanbul ignore else */ - if (app) { - addDevtools(app, store) - } else if (!isDevWarned && !__TEST__) { - isDevWarned = true - console.warn( - `[🍍]: store was instantiated before calling\n` + - `app.use(pinia)\n` + - `Make sure to install pinia's plugin by using createPinia:\n` + - `https://github.com/posva/pinia/tree/v2#install-the-plugin\n` + - `It will enable devtools and overall a better developer experience.` - ) - } + getClientApp().then((app) => addDevtools(app, store)) } return store -- 2.47.2