Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: install devtools plugin only after app is created and installed (#1063) #1392

Draft
wants to merge 2 commits into
base: v2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions packages/pinia/src/createPinia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export function createPinia(): Pinia {
let _p: Pinia['_p'] = []
// plugins added before calling app.use(pinia)
let toBeInstalled: PiniaPlugin[] = []
let onInstallCallbacks: ((app: App) => void)[] = []
let _a: App | null = null

const pinia: Pinia = markRaw({
install(app: App) {
Expand Down Expand Up @@ -47,9 +49,27 @@ export function createPinia(): Pinia {
},

_p,
// it's actually undefined here
// @ts-expect-error
_a: null,

get _a() {
return _a!
},
set _a(app) {
_a = app

if (app) {
onInstallCallbacks.forEach((cb) => cb(app))
onInstallCallbacks = []
}
},

onInstall(cb: (app: App) => void) {
if (_a) {
cb(_a)
} else {
onInstallCallbacks.push(cb)
}
},

_e: scope,
_s: new Map<string, StoreGeneric>(),
state,
Expand Down
12 changes: 7 additions & 5 deletions packages/pinia/src/devtools/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ export function devtoolsPlugin<
S extends StateTree = StateTree,
G /* extends GettersTree<S> */ = _GettersTree<S>,
A /* extends ActionsTree */ = _ActionsTree
>({ app, store, options }: PiniaPluginContext<Id, S, G, A>) {
>({ store, options, pinia }: PiniaPluginContext<Id, S, G, A>) {
// HMR module
if (store.$id.startsWith('__hot:')) {
return
Expand Down Expand Up @@ -553,9 +553,11 @@ export function devtoolsPlugin<
}
}

addStoreToDevtools(
app,
// FIXME: is there a way to allow the assignment from Store<Id, S, G, A> to StoreGeneric?
store as StoreGeneric
pinia.onInstall((app) =>
addStoreToDevtools(
app,
// FIXME: is there a way to allow the assignment from Store<Id, S, G, A> to StoreGeneric?
store as StoreGeneric
)
)
}
8 changes: 8 additions & 0 deletions packages/pinia/src/rootStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ export interface Pinia {
*/
use(plugin: PiniaPlugin): Pinia

/**
* Executes callback after Pinia is installed into Vue app
*
* @internal
* @param cb - callback to execute
*/
onInstall(cb: (app: App) => void): void

/**
* Installed store plugins
*
Expand Down