Skip to content

Commit

Permalink
fix: install devtools plugin after app is set, but revert change for …
Browse files Browse the repository at this point in the history
…other plugins
  • Loading branch information
popov-a-e committed Jun 25, 2022
1 parent 4c45ec0 commit 9ea96a4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 36 deletions.
10 changes: 5 additions & 5 deletions packages/pinia/src/createPinia.ts
Expand Up @@ -18,7 +18,7 @@ export function createPinia(): Pinia {
let _p: Pinia['_p'] = []
// plugins added before calling app.use(pinia)
let toBeInstalled: PiniaPlugin[] = []
let afterInitializeCallbacks: ((app: App) => void)[] = []
let onInstallCallbacks: ((app: App) => void)[] = []
let _a: App | null = null

const pinia: Pinia = markRaw({
Expand Down Expand Up @@ -57,16 +57,16 @@ export function createPinia(): Pinia {
_a = app

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

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

Expand Down
12 changes: 7 additions & 5 deletions packages/pinia/src/devtools/plugin.ts
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
)
)
}
4 changes: 2 additions & 2 deletions packages/pinia/src/rootStore.ts
Expand Up @@ -58,12 +58,12 @@ export interface Pinia {
use(plugin: PiniaPlugin): Pinia

/**
* Executes callback after pinia._a is set, or immediately, if it is set already
* Executes callback after Pinia is installed into Vue app
*
* @internal
* @param cb - callback to execute
*/
afterAppInit(cb: (app: App) => void): void
onInstall(cb: (app: App) => void): void

/**
* Installed store plugins
Expand Down
46 changes: 22 additions & 24 deletions packages/pinia/src/store.ts
Expand Up @@ -676,36 +676,34 @@ function createSetupStore<
}

// apply all plugins
pinia.afterAppInit((app) => {
pinia._p.forEach((extender) => {
/* istanbul ignore else */
if (__DEV__ && IS_CLIENT) {
const extensions = scope.run(() =>
pinia._p.forEach((extender) => {
/* istanbul ignore else */
if (__DEV__ && IS_CLIENT) {
const extensions = scope.run(() =>
extender({
store,
app: pinia._a,
pinia,
options: optionsForPlugin,
})
)!
Object.keys(extensions || {}).forEach((key) =>
store._customProperties.add(key)
)
assign(store, extensions)
} else {
assign(
store,
scope.run(() =>
extender({
store,
app,
app: pinia._a,
pinia,
options: optionsForPlugin,
})
)!
Object.keys(extensions || {}).forEach((key) =>
store._customProperties.add(key)
)
assign(store, extensions)
} else {
assign(
store,
scope.run(() =>
extender({
store,
app,
pinia,
options: optionsForPlugin,
})
)!
)
}
})
)
}
})

if (
Expand Down

0 comments on commit 9ea96a4

Please sign in to comment.