// eslint-disable-next-line @typescript-eslint/no-var-requires const plotly = require("plotly.js-dist"); class Trace { private static index = 1; readonly index: number; readonly name: string; private data: any[][] = [] private timeAxis: number[] = [] constructor(name?: string) { this.index = Trace.index; this.name = name ? name : this.index.toString(); Trace.index++; } add(time: number, data: any[]) { while (this.timeAxis.includes(time)) { time += 0.001; } this.timeAxis.push(time); this.data.push(data); } toJSON() { return { x: this.timeAxis, y: this.data.map(_ => this.index), text: this.data.map(d => d.join(" ")), mode: "markers", type: "scatter", textposition: "top right", }; } } /** * Create a logging object which can be dropped in to track events */ export class Logger { private tracking: Map = new Map() readonly element = document.createElement("div"); private data: Trace[] = [] private _timeout = -1 constructor() { plotly.newPlot(this.element, this.data); } clear() { this.data = []; plotly.update(this.element, this.data.map(datum => datum.toJSON())); } log(...args: any[]) { if (args[0] && typeof args[0] === "object") { const obj = args[0]; const time = Reflect.has(obj, "context") ? obj.context.currentTime : performance.now(); if (!this.tracking.has(args[0])) { const trace = new Trace(obj.name); this.data.push(trace); this.tracking.set(args[0], trace); } // get the logging index of the class const trace = this.tracking.get(args[0]) as Trace; const name = Reflect.has(obj, "name") ? obj.name : trace.index; trace.add(time, args.slice(1)); this._debounceRender(); } } private _debounceRender() { clearTimeout(this._timeout); this._timeout = setTimeout(() => { plotly.react(this.element, this.data.map(datum => datum.toJSON()), { hovermode: "closest", showlegend: false, xaxis: { title: { text: "Time", }, showgrid: false, }, yaxis: { autorange: true, showgrid: false, tickvals: Array.from(this.tracking).map(([_, trace]) => trace.index), ticktext: Array.from(this.tracking).map(([_, trace]) => trace.name), }, }); }, 10) as unknown as number; } warn() { } }