11cb0ef41Sopenharmony_ci// Copyright 2021 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ciimport {formatDurationMicros} from '../helper.mjs';
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciimport {LogEntry} from './log.mjs';
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciexport class TimerLogEntry extends LogEntry {
91cb0ef41Sopenharmony_ci  constructor(type, startTime, endTime = -1) {
101cb0ef41Sopenharmony_ci    super(type, startTime);
111cb0ef41Sopenharmony_ci    this._endTime = endTime;
121cb0ef41Sopenharmony_ci    this.depth = 0;
131cb0ef41Sopenharmony_ci  }
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci  end(time) {
161cb0ef41Sopenharmony_ci    if (this.isInitialized) throw new Error('Invalid timer change');
171cb0ef41Sopenharmony_ci    this._endTime = time;
181cb0ef41Sopenharmony_ci  }
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  get isInitialized() {
211cb0ef41Sopenharmony_ci    return this._endTime !== -1;
221cb0ef41Sopenharmony_ci  }
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  get startTime() {
251cb0ef41Sopenharmony_ci    return this._time;
261cb0ef41Sopenharmony_ci  }
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  get endTime() {
291cb0ef41Sopenharmony_ci    return this._endTime;
301cb0ef41Sopenharmony_ci  }
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  get duration() {
331cb0ef41Sopenharmony_ci    return Math.max(0, this._endTime - this._time);
341cb0ef41Sopenharmony_ci  }
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  covers(time) {
371cb0ef41Sopenharmony_ci    return this._time <= time && time <= this._endTime;
381cb0ef41Sopenharmony_ci  }
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  get toolTipDict() {
411cb0ef41Sopenharmony_ci    const dict = super.toolTipDict;
421cb0ef41Sopenharmony_ci    dict.startTime = formatDurationMicros(dict.startTime);
431cb0ef41Sopenharmony_ci    dict.endTime = formatDurationMicros(dict.endTime);
441cb0ef41Sopenharmony_ci    dict.duration = formatDurationMicros(dict.duration);
451cb0ef41Sopenharmony_ci    return dict;
461cb0ef41Sopenharmony_ci  }
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  static get propertyNames() {
491cb0ef41Sopenharmony_ci    return [
501cb0ef41Sopenharmony_ci      'type',
511cb0ef41Sopenharmony_ci      'startTime',
521cb0ef41Sopenharmony_ci      'endTime',
531cb0ef41Sopenharmony_ci      'duration',
541cb0ef41Sopenharmony_ci    ];
551cb0ef41Sopenharmony_ci  }
561cb0ef41Sopenharmony_ci}
57