Skip to content

Commit

Permalink
feat(devtools): expose selected store as global variable (#2692)
Browse files Browse the repository at this point in the history
* feat: expose selected store as

* fix: Expose  and  to window

* refactor: add to globalThis

---------

Co-authored-by: Eduardo San Martin Morote <[email protected]>
  • Loading branch information
Azurewarth0920 and posva authored Jun 26, 2024
1 parent 2c01fc0 commit e0a7351
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/pinia/src/devtools/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ export function registerPiniaDevtools(app: DevtoolsApp, pinia: Pinia) {
}
})

// Expose pinia instance as $pinia to window
globalThis.$pinia = pinia

api.on.getInspectorState((payload) => {
if (payload.app === app && payload.inspectorId === INSPECTOR_ID) {
const inspectedStore =
Expand All @@ -230,6 +233,9 @@ export function registerPiniaDevtools(app: DevtoolsApp, pinia: Pinia) {
}

if (inspectedStore) {
// Expose selected store as $store to window
if (payload.nodeId !== PINIA_ROOT_ID)
globalThis.$store = toRaw(inspectedStore as StoreGeneric)
payload.state = formatStoreForInspectorState(inspectedStore)
}
}
Expand Down Expand Up @@ -592,3 +598,14 @@ export function devtoolsPlugin<
store as StoreGeneric
)
}

declare global {
/**
* Exposes the `pinia` instance when Devtools are opened.
*/
var $pinia: Pinia | undefined
/**
* Exposes the current store when Devtools are opened.
*/
var $store: StoreGeneric | undefined
}

0 comments on commit e0a7351

Please sign in to comment.