Skip to content

Commit 81b77fa

Browse files
committed
feat(api): on.timelineCleared
1 parent abcf352 commit 81b77fa

7 files changed

Lines changed: 29 additions & 5 deletions

File tree

docs/plugin/api-reference.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,16 @@ api.on.inspectTimelineEvent(payload => {
583583
})
584584
```
585585

586+
### on.timelineCleared
587+
588+
This hook is called when the timeline is cleared by the user. Note that clearing the timeline affects all apps and layers simultaneously.
589+
590+
```js
591+
api.on.timelineCleared(() => {
592+
console.log('timeline is cleared!')
593+
})
594+
```
595+
586596
## Utilities
587597

588598
### getComponentInstances

packages/api/src/api/hooks.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const enum Hooks {
2020
GET_COMPONENT_DEVTOOLS_OPTIONS = 'getAppDevtoolsOptions',
2121
GET_COMPONENT_RENDER_CODE = 'getComponentRenderCode',
2222
INSPECT_TIMELINE_EVENT = 'inspectTimelineEvent',
23+
TIMELINE_CLEARED = 'timelineCleared',
2324
GET_INSPECTOR_TREE = 'getInspectorTree',
2425
GET_INSPECTOR_STATE = 'getInspectorState',
2526
EDIT_INSPECTOR_STATE = 'editInspectorState'
@@ -113,6 +114,7 @@ export type HookPayloads = {
113114
all?: boolean
114115
data: any
115116
}
117+
[Hooks.TIMELINE_CLEARED]: Record<string, never>
116118
[Hooks.GET_INSPECTOR_TREE]: {
117119
app: App
118120
inspectorId: string
@@ -166,6 +168,7 @@ export interface Hookable<TContext> {
166168
getComponentDevtoolsOptions (handler: HookHandler<HookPayloads[Hooks.GET_COMPONENT_DEVTOOLS_OPTIONS], TContext>)
167169
getComponentRenderCode (handler: HookHandler<HookPayloads[Hooks.GET_COMPONENT_RENDER_CODE], TContext>)
168170
inspectTimelineEvent (handler: HookHandler<HookPayloads[Hooks.INSPECT_TIMELINE_EVENT], TContext>)
171+
timelineCleared (handler: HookHandler<HookPayloads[Hooks.TIMELINE_CLEARED], TContext>)
169172
getInspectorTree (handler: HookHandler<HookPayloads[Hooks.GET_INSPECTOR_TREE], TContext>)
170173
getInspectorState (handler: HookHandler<HookPayloads[Hooks.GET_INSPECTOR_STATE], TContext>)
171174
editInspectorState (handler: HookHandler<HookPayloads[Hooks.EDIT_INSPECTOR_STATE], TContext>)

packages/app-backend-api/src/api.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ export class DevtoolsApi {
198198
return payload.data
199199
}
200200

201+
async clearTimeline () {
202+
await this.callHook(Hooks.TIMELINE_CLEARED, {})
203+
}
204+
201205
async getInspectorTree (inspectorId: string, app: App, filter: string) {
202206
const payload = await this.callHook(Hooks.GET_INSPECTOR_TREE, {
203207
inspectorId,

packages/app-backend-api/src/hooks.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ export class DevtoolsHookable implements Hookable<BackendContext> {
128128
this.hook(Hooks.INSPECT_TIMELINE_EVENT, handler, PluginPermission.TIMELINE)
129129
}
130130

131+
timelineCleared (handler: Handler<HookPayloads[Hooks.TIMELINE_CLEARED]>) {
132+
this.hook(Hooks.TIMELINE_CLEARED, handler, PluginPermission.TIMELINE)
133+
}
134+
131135
getInspectorTree (handler: Handler<HookPayloads[Hooks.GET_INSPECTOR_TREE]>) {
132136
this.hook(Hooks.GET_INSPECTOR_TREE, handler, PluginPermission.CUSTOM_INSPECTOR)
133137
}

packages/app-backend-core/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,8 @@ function connectBridge () {
447447
showScreenshot(screenshot, ctx)
448448
})
449449

450-
ctx.bridge.on(BridgeEvents.TO_BACK_TIMELINE_CLEAR, () => {
451-
clearTimeline(ctx)
450+
ctx.bridge.on(BridgeEvents.TO_BACK_TIMELINE_CLEAR, async () => {
451+
await clearTimeline(ctx)
452452
})
453453

454454
ctx.bridge.on(BridgeEvents.TO_BACK_TIMELINE_EVENT_DATA, async ({ id }) => {

packages/app-backend-core/src/timeline.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,12 @@ function mapTimelineEvent (eventData: TimelineEventOptions & WithId) {
163163
}
164164
}
165165

166-
export function clearTimeline (ctx: BackendContext) {
166+
export async function clearTimeline (ctx: BackendContext) {
167167
ctx.timelineEventMap.clear()
168168
for (const layer of ctx.timelineLayers) {
169169
layer.events = []
170170
}
171-
console.log('timeline cleared')
171+
await ctx.api.clearTimeline()
172172
}
173173

174174
export async function sendTimelineEventData (id: ID, ctx: BackendContext) {
@@ -187,7 +187,6 @@ export async function sendTimelineEventData (id: ID, ctx: BackendContext) {
187187
}
188188

189189
export function removeLayersForApp (app: App, ctx: BackendContext) {
190-
console.log('removeLayersForApp', app)
191190
const layers = ctx.timelineLayers.filter(l => l.app === app)
192191
for (const layer of layers) {
193192
const index = ctx.timelineLayers.indexOf(layer)

packages/shell-dev-vue3/src/devtools-plugin/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ export default {
130130
}
131131
})
132132

133+
api.on.timelineCleared(() => {
134+
console.log('timeline is cleared!')
135+
})
136+
133137
api.addInspector({
134138
id: 'test-inspector',
135139
label: 'Test inspector',

0 commit comments

Comments
 (0)