import { ObjectCreate, TypeError, WeakMap, WeakMapPrototypeSet as set, } from "ext:core/primordials"; import { onRuntimeStart } from "ext:core/bootstrap"; import { op_timer_now as timerNow, op_timer_time_origin as timerTimeOrigin, } from "ext:core/ops"; import { EventTarget } from "./events.js"; import { state } from "./idl.js"; const secret = ObjectCreate(null) as { readonly performanceToken: unique symbol; }; const performances = new WeakMap(); const floor = Math.floor; class Performance extends EventTarget { constructor(key: typeof secret); constructor() { if (arguments[0] !== secret) throw new TypeError("Illegal constructor"); super(); } now(): number { state(performances, this); return floor(timerNow(null) * 10) / 10; } get timeOrigin(): number { return state(performances, this); } toJSON(): { timeOrigin: number } { return { timeOrigin: state(performances, this) }; } } const performance = new Performance(secret); onRuntimeStart(() => { set(performances, performance, floor(timerTimeOrigin(null) * 10) / 10); }); export { Performance, performance };