char/avril

(wip) why'd you have to go and make things so complicaaaaated?

git clone https://git.t4t.associates/char/avril

Charlotte Somport web extension to typescript44fe2c6

main
1.1 KiB43 linesraw
1import {
2  ObjectCreate,
3  TypeError,
4  WeakMap,
5  WeakMapPrototypeSet as set,
6} from "ext:core/primordials";
7import { onRuntimeStart } from "ext:core/bootstrap";
8import {
9  op_timer_now as timerNow,
10  op_timer_time_origin as timerTimeOrigin,
11} from "ext:core/ops";
12import { EventTarget } from "./events.js";
13import { state } from "./idl.js";
14
15const secret = ObjectCreate(null) as {
16  readonly performanceToken: unique symbol;
17};
18const performances = new WeakMap<object, number>();
19const floor = Math.floor;
20
21class Performance extends EventTarget {
22  constructor(key: typeof secret);
23  constructor() {
24    if (arguments[0] !== secret) throw new TypeError("Illegal constructor");
25    super();
26  }
27  now(): number {
28    state(performances, this);
29    return floor(timerNow(null) * 10) / 10;
30  }
31  get timeOrigin(): number {
32    return state(performances, this);
33  }
34  toJSON(): { timeOrigin: number } {
35    return { timeOrigin: state(performances, this) };
36  }
37}
38
39const performance = new Performance(secret);
40onRuntimeStart(() => {
41  set(performances, performance, floor(timerTimeOrigin(null) * 10) / 10);
42});
43export { Performance, performance };