Skip to content

Commit ce8f1e5

Browse files
committed
feat(devtools): display all getters in pinia root
1 parent e1ecb18 commit ce8f1e5

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

packages/pinia/src/devtools/formatting.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,39 +22,44 @@ export const PINIA_ROOT_ID = '_root'
2222
export function formatStoreForInspectorTree(
2323
store: StoreGeneric | Pinia
2424
): CustomInspectorNode {
25-
return '$id' in store
25+
return isPinia(store)
2626
? {
27-
id: store.$id,
28-
label: store.$id,
29-
}
30-
: {
3127
id: PINIA_ROOT_ID,
3228
label: PINIA_ROOT_LABEL,
3329
}
30+
: {
31+
id: store.$id,
32+
label: store.$id,
33+
}
3434
}
3535

3636
export function formatStoreForInspectorState(
3737
store: StoreGeneric | Pinia
3838
): CustomInspectorState {
3939
if (isPinia(store)) {
40+
const storeNames = Array.from(store._s.keys())
41+
const storeMap = store._s
4042
const state: CustomInspectorState = {
41-
state: Object.keys(store.state.value).map((storeId) => ({
43+
state: storeNames.map((storeId) => ({
4244
editable: true,
4345
key: storeId,
4446
value: store.state.value[storeId],
4547
})),
48+
getters: storeNames
49+
.filter((id) => storeMap.get(id)!._getters)
50+
.map((id) => {
51+
const store = storeMap.get(id)!
52+
53+
return {
54+
editable: false,
55+
key: id,
56+
value: store._getters!.reduce((getters, key) => {
57+
getters[key] = store[key]
58+
return getters
59+
}, {} as Record<string, any>),
60+
}
61+
}),
4662
}
47-
// TODO: use this version when possible
48-
// Object.keys(store.state.value).forEach((storeId) => {
49-
// const currentState = store.state.value[storeId]
50-
// state[storeId] = Object.keys(currentState).map((key) => ({
51-
// // is not possible to made editable because no way to get the storeId in
52-
// // edit inspector state callback
53-
// editable: false,
54-
// key,
55-
// value: currentState[key],
56-
// }))
57-
// })
5863

5964
return state
6065
}

0 commit comments

Comments
 (0)