Skip to content

Commit 90285de

Browse files
fix(client, bridge): update pinia status when module loaded (#244)
1 parent 2abfb2f commit 90285de

File tree

7 files changed

+41
-10
lines changed

7 files changed

+41
-10
lines changed

packages/client/src/pages/pinia.vue

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { useDevToolsBridgeRpc } from '@vue/devtools-core'
55
import { type InspectorNodeTag, type InspectorState } from '@vue/devtools-kit'
66
import { Pane, Splitpanes } from 'splitpanes'
77
8+
const inspectorId = 'pinia'
89
const bridgeRpc = useDevToolsBridgeRpc()
910
1011
const selected = ref('')
@@ -16,7 +17,7 @@ const state = ref<{
1617
}>({})
1718
1819
function getPiniaState(nodeId: string) {
19-
bridgeRpc.getInspectorState({ inspectorId: 'pinia', nodeId }).then(({ data }) => {
20+
bridgeRpc.getInspectorState({ inspectorId, nodeId }).then(({ data }) => {
2021
state.value = data
2122
})
2223
}
@@ -33,12 +34,22 @@ watch(selected, () => {
3334
createCollapseContext('inspector-state')
3435
3536
onDevToolsClientConnected(() => {
36-
bridgeRpc.getInspectorTree({ inspectorId: 'pinia', filter: '' }).then(({ data }) => {
37-
tree.value = data
38-
if (!selected.value && data.length) {
39-
selected.value = data[0].id
37+
const getPiniaInspectorTree = () => {
38+
bridgeRpc.getInspectorTree({ inspectorId, filter: '' }).then(({ data }) => {
39+
tree.value = data
40+
if (!selected.value && data.length)
41+
selected.value = data[0].id
4042
getPiniaState(data[0].id)
41-
}
43+
})
44+
}
45+
getPiniaInspectorTree()
46+
47+
bridgeRpc.on.componentUpdated((id) => {
48+
if (id !== inspectorId)
49+
return
50+
getPiniaInspectorTree()
51+
}, {
52+
inspectorId,
4253
})
4354
4455
bridgeRpc.on.inspectorTreeUpdated((data) => {
@@ -50,7 +61,7 @@ onDevToolsClientConnected(() => {
5061
getPiniaState(data.data[0].id)
5162
}
5263
}, {
53-
inspectorId: 'pinia',
64+
inspectorId,
5465
})
5566
5667
bridgeRpc.on.inspectorStateUpdated((data) => {
@@ -59,7 +70,7 @@ onDevToolsClientConnected(() => {
5970
6071
state.value = data
6172
}, {
62-
inspectorId: 'pinia',
73+
inspectorId,
6374
})
6475
})
6576
</script>
@@ -77,7 +88,7 @@ onDevToolsClientConnected(() => {
7788
<InspectorState
7889
v-for="(item, key) in state" :id="key"
7990
:key="key"
80-
inspector-id="pinia"
91+
:inspector-id="inspectorId"
8192
:node-id="selected" :data="item" :name="`${key}`"
8293
/>
8394
</div>

packages/core/src/bridge/devtools.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ export class BridgeRpc {
100100
} as T)
101101
})
102102
},
103+
componentUpdated(cb: (id?: string) => void, options: { inspectorId: string }) {
104+
devtoolsBridge.value.on(BridgeEvents.COMPONENT_UPDATED, () => {
105+
cb(options?.inspectorId)
106+
})
107+
},
103108
devtoolsStateUpdated(cb) {
104109
devtoolsBridge.value.on(BridgeEvents.DEVTOOLS_STATE_UPDATED, (payload) => {
105110
cb(parse(payload))

packages/core/src/bridge/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export enum BridgeEvents {
1616
SEND_INSPECTOR_TREE = 'inspector-tree:send',
1717
// inspector state
1818
SEND_INSPECTOR_STATE = 'inspector-state:send',
19+
// component updated
20+
COMPONENT_UPDATED = 'component:updated',
1921

2022
// edit related things (components/pinia/route, etc...) they are all fired the same event
2123
// so that we can decouple the specific logic to the `devtools-kit` package.

packages/core/src/bridge/user-app.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ export function registerBridgeRpc(bridge: BridgeInstanceType) {
175175
bridge.emit(BridgeEvents.SEND_INSPECTOR_TREE, payload)
176176
})
177177

178+
// component updated
179+
devtools.api.on.componentUpdated(() => {
180+
bridge.emit(BridgeEvents.COMPONENT_UPDATED)
181+
})
182+
178183
// inspector state updated
179184
devtools.api.on.sendInspectorState((payload) => {
180185
bridge.emit(BridgeEvents.SEND_INSPECTOR_STATE, payload)

packages/devtools-kit/src/api/api.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,10 @@ export class DevToolsPluginApi {
156156
apiHooks.callHook(DevToolsEvents.VISIT_COMPONENT_TREE, ...params)
157157
}
158158

159-
notifyComponentUpdate() {}
159+
notifyComponentUpdate() {
160+
apiHooks.callHook(DevToolsEvents.COMPONENT_UPDATED)
161+
}
162+
160163
now() {
161164
return nowFn()
162165
}

packages/devtools-kit/src/api/hook.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export enum DevToolsEvents {
1313
DEVTOOLS_CONNECTED_UPDATED = 'devtools:connected-updated',
1414
ROUTER_INFO_UPDATED = 'router-info:updated',
1515
COMPONENT_STATE_INSPECT = 'component-state:inspect',
16+
COMPONENT_UPDATED = 'component:updated',
1617
TOGGLE_COMPONENT_HIGHLIGHTER = 'component-highlighter:toggle',
1718
GET_COMPONENT_BOUNDING_RECT = 'component-bounding-rect:get',
1819
SCROLL_TO_COMPONENT = 'scroll-to-component',
@@ -60,6 +61,7 @@ export interface DevToolsEvent {
6061
[DevToolsEvents.CUSTOM_TABS_UPDATED]: (payload: CustomTab[]) => void
6162
// custom command
6263
[DevToolsEvents.CUSTOM_COMMANDS_UPDATED]: (payload: CustomCommand[]) => void
64+
[DevToolsEvents.COMPONENT_UPDATED]: () => void
6365
}
6466

6567
export type DevToolsEventParams<T extends keyof DevToolsEvent> = Parameters<DevToolsEvent[T]>

packages/devtools-kit/src/api/on.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ export const on = {
2828
apiHooks.hook(DevToolsEvents.EDIT_INSPECTOR_STATE, fn)
2929
},
3030
editComponentState() {},
31+
componentUpdated(fn: DevToolsEvent[DevToolsEvents.COMPONENT_UPDATED]) {
32+
apiHooks.hook(DevToolsEvents.COMPONENT_UPDATED, fn)
33+
},
3134
// #endregion compatible with old devtools
3235

3336
// router

0 commit comments

Comments
 (0)