Skip to content

Commit

Permalink
feat(devtools): work with stores added before app.use (#444)
Browse files Browse the repository at this point in the history
* 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 <posva@users.noreply.github.com>

Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com>
  • Loading branch information
marshallswain and posva committed May 3, 2021
1 parent 3751c0c commit 21f917f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 21 deletions.
11 changes: 7 additions & 4 deletions src/rootStore.ts
Expand Up @@ -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<App> | 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.
Expand Down
18 changes: 1 addition & 17 deletions src/store.ts
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 21f917f

Please sign in to comment.