Skip to content

Commit e459259

Browse files
committed
fix(timeline): correct time diff between Date.now & performance.now
1 parent e9e826e commit e459259

File tree

5 files changed

+22
-11
lines changed

5 files changed

+22
-11
lines changed

packages/api/src/time.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
let supported: boolean
22
let perf: Performance
33

4-
function isSupported () {
4+
export function isPerformanceSupported () {
55
if (supported !== undefined) {
66
return supported
77
}
@@ -15,5 +15,5 @@ function isSupported () {
1515
}
1616

1717
export function now () {
18-
return isSupported() ? perf.now() : Date.now()
18+
return isPerformanceSupported() ? perf.now() : Date.now()
1919
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { BackendContext, TimelineMarker } from '@vue-devtools/app-backend-api'
22
import { BridgeEvents } from '@vue-devtools/shared-utils'
3-
import { TimelineMarkerOptions } from '@vue/devtools-api'
3+
import { isPerformanceSupported, TimelineMarkerOptions } from '@vue/devtools-api'
4+
import { dateThreshold, perfTimeDiff } from './timeline'
45

56
export async function addTimelineMarker (options: TimelineMarkerOptions, ctx: BackendContext) {
67
if (!ctx.currentAppRecord) {
@@ -31,11 +32,15 @@ export async function sendTimelineMarkers (ctx: BackendContext) {
3132
}
3233

3334
async function serializeMarker (marker: TimelineMarker) {
35+
let time = marker.time
36+
if (isPerformanceSupported() && time < dateThreshold) {
37+
time += perfTimeDiff
38+
}
3439
return {
3540
id: marker.id,
3641
appId: marker.appRecord?.id,
3742
all: marker.all,
38-
time: Math.round(marker.time * 1000),
43+
time: Math.round(time * 1000),
3944
label: marker.label,
4045
color: marker.color,
4146
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BackendContext, AppRecord } from '@vue-devtools/app-backend-api'
22
import { BridgeEvents, HookEvents, stringify, SharedData } from '@vue-devtools/shared-utils'
3-
import { App, ID, TimelineEventOptions, WithId, now } from '@vue/devtools-api'
3+
import { App, ID, TimelineEventOptions, WithId, now, isPerformanceSupported } from '@vue/devtools-api'
44
import { hook } from './global-hook'
55
import { getAppRecord, getAppRecordId } from './app'
66
import { builtinLayers } from './timeline-builtins'
@@ -156,10 +156,18 @@ export async function addTimelineEvent (options: TimelineEventOptions, app: App,
156156
}
157157
}
158158

159+
const initialTime = Date.now()
160+
export const dateThreshold = initialTime - 1_000_000
161+
export const perfTimeDiff = initialTime - now()
162+
159163
function mapTimelineEvent (eventData: TimelineEventOptions & WithId) {
164+
let time = eventData.event.time
165+
if (isPerformanceSupported() && time < dateThreshold) {
166+
time += perfTimeDiff
167+
}
160168
return {
161169
id: eventData.id,
162-
time: Math.round(eventData.event.time * 1000),
170+
time: Math.round(time * 1000),
163171
logType: eventData.event.logType,
164172
groupId: eventData.event.groupId,
165173
title: eventData.event.title,

packages/app-frontend/src/features/timeline/composable/events.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export function addEvent (appId: string, eventOptions: TimelineEvent, layer: Lay
8383
}
8484

8585
// Min time
86-
if (minTime.value > event.time) {
86+
if (minTime.value === -1_000_000 || minTime.value > event.time) {
8787
const stick = minTime.value === startTime.value
8888
minTime.value = event.time - 100_000
8989
if (stick) {

packages/app-frontend/src/features/timeline/composable/reset.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,8 @@ export function resetTimeline (sync = true) {
5353

5454
export function resetTime () {
5555
const now = 0
56-
startTime.value = now - 1_000_000
57-
endTime.value = now
58-
minTime.value = now - 1_000_000
59-
maxTime.value = now
56+
minTime.value = startTime.value = now - 1_000_000
57+
maxTime.value = endTime.value = now
6058
}
6159

6260
export function onTimelineReset (cb: ResetCb) {

0 commit comments

Comments
 (0)