Skip to content

Commit d3ddb1b

Browse files
authored
feat: add timeline layer (#4)
1 parent 165088d commit d3ddb1b

3 files changed

Lines changed: 51 additions & 34 deletions

File tree

src/debug.ts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { getCurrentInstance, watch } from 'vue'
1+
import {
2+
effectScope,
3+
getCurrentInstance,
4+
getCurrentScope,
5+
onScopeDispose,
6+
watch,
7+
} from 'vue'
28
import { remove, upsert } from './registry'
39
import type { VuebuggerEntry } from './types'
410

@@ -16,28 +22,33 @@ export function debug<T extends Record<string, any>>(
1622
'No component'
1723
const uid = `${componentName}/${groupId}-${Math.random().toString(36).slice(2, 9)}`
1824

19-
watch(
20-
() => state,
21-
(value, _, onCleanup) => {
22-
upsert({
25+
const scope = getCurrentScope() ?? effectScope()
26+
scope.run(() => {
27+
onScopeDispose(() =>
28+
remove({
2329
groupId,
2430
uid,
2531
componentName,
2632
componentInstance: instance,
27-
debugState: value,
28-
})
29-
onCleanup(() => {
30-
remove({
33+
debugState: state,
34+
}),
35+
)
36+
watch(
37+
() => state,
38+
(value, _) => {
39+
upsert({
3140
groupId,
3241
uid,
3342
componentName,
3443
componentInstance: instance,
3544
debugState: value,
3645
})
37-
})
38-
},
39-
{ deep: true },
40-
)
41-
46+
},
47+
{
48+
immediate: true,
49+
deep: true,
50+
},
51+
)
52+
})
4253
return state
4354
}

src/devtools.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import type {
88
VuebuggerEntry,
99
} from './types'
1010

11-
const INSPECTOR_ID = 'composables'
11+
const INSPECTOR_ID = 'vuebugger-inspector'
12+
const TIMELINE_ID = 'vuebugger-timeline'
1213

1314
export const handleGetInspectorTree: DevtoolsApiHandler<
1415
'getInspectorTree'
@@ -123,16 +124,26 @@ export function setupComposableDevtools<T>(app: App<T>) {
123124
treeFilterPlaceholder:
124125
'Search by composable or component name...',
125126
})
127+
api.addTimelineLayer({
128+
id: TIMELINE_ID,
129+
label: 'Vuebugger',
130+
color: 155,
131+
})
126132

127-
onUpdate(
128-
(
129-
componentInstance: VuebuggerEntry['componentInstance'],
130-
) => {
131-
api.sendInspectorTree(INSPECTOR_ID)
132-
api.sendInspectorState(INSPECTOR_ID)
133-
api.notifyComponentUpdate(componentInstance)
134-
},
135-
)
133+
onUpdate((entry: VuebuggerEntry) => {
134+
api.sendInspectorTree(INSPECTOR_ID)
135+
api.sendInspectorState(INSPECTOR_ID)
136+
api.notifyComponentUpdate(entry.componentInstance)
137+
api.addTimelineEvent({
138+
layerId: TIMELINE_ID,
139+
event: {
140+
time: api.now(),
141+
data: entry,
142+
title: `${entry.uid} state change`,
143+
groupId: entry.uid,
144+
},
145+
})
146+
})
136147

137148
api.on.getInspectorTree(handleGetInspectorTree)
138149
api.on.getInspectorState(handleGetInspectorState(api))

src/registry.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,14 @@ export const byGroupId = new Map<
1010
Set<VuebuggerEntry['uid']>
1111
>()
1212

13-
const callbacks: ((
14-
componentInstance: VuebuggerEntry['componentInstance'],
15-
) => void)[] = []
16-
const runCallbacks = (
17-
componentInstance: VuebuggerEntry['componentInstance'],
18-
) => callbacks.forEach((cb) => cb(componentInstance))
13+
const callbacks: ((entry: VuebuggerEntry) => void)[] = []
14+
const runCallbacks = (entry: VuebuggerEntry) =>
15+
callbacks.forEach((cb) => cb(entry))
1916
const withCallbacks =
2017
(fn: (entry: VuebuggerEntry) => void) =>
2118
(entry: VuebuggerEntry) => {
2219
fn(entry)
23-
runCallbacks(entry.componentInstance)
20+
runCallbacks(entry)
2421
}
2522

2623
export const upsert = withCallbacks(
@@ -44,9 +41,7 @@ export const remove = withCallbacks(
4441
)
4542

4643
export const onUpdate = (
47-
fn: (
48-
componentInstance: VuebuggerEntry['componentInstance'],
49-
) => void,
44+
fn: (entry: VuebuggerEntry) => void,
5045
) => {
5146
callbacks.push(fn)
5247
}

0 commit comments

Comments
 (0)