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

feat(devtools): expose selected store as global variable #2692

Merged
merged 3 commits into from
Jun 26, 2024
Merged
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
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 @@ -587,3 +593,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
}
Loading