char/avril

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

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

Charlotte Somport timers and structured clone extensions to typescriptc42f8c8

main
2.1 KiB70 linesraw
1import {
2  ObjectDefineProperty as define,
3  ReflectApply as apply,
4  SymbolIterator,
5  TypeError,
6} from "ext:core/primordials";
7import { clone } from "ext:core/structured_clone";
8import { array, dictionary, isObject, required } from "ext:web/idl.js";
9import {
10  classify,
11  setStructuredClone,
12  transferCallbacks,
13} from "ext:web/serialization.js";
14import { dataCloneError } from "ext:web/events.js";
15const structuredClone = {
16  structuredClone(value: unknown, options: unknown = undefined): unknown {
17    required(arguments.length, 1);
18    const raw = dictionary(options).transfer, transfer = array<object>();
19    if (raw !== undefined) {
20      if (!isObject(raw)) {
21        throw new TypeError("Transfer list must be a sequence");
22      }
23      const method = raw[SymbolIterator];
24      if (typeof method !== "function") {
25        throw new TypeError("Transfer list must be iterable");
26      }
27      const iterator = apply(method, raw, []);
28      if (!isObject(iterator)) throw new TypeError("Invalid transfer iterator");
29      const next = iterator.next;
30      if (typeof next !== "function") {
31        throw new TypeError("Invalid transfer iterator next method");
32      }
33      while (true) {
34        const step = apply(next, iterator, []);
35        if (!isObject(step)) {
36          throw new TypeError("Invalid transfer iterator result");
37        }
38        if (step.done) break;
39        const item = step.value;
40        if (!isObject(item)) {
41          throw new TypeError("Transfer entries must be objects");
42        }
43        transfer[transfer.length] = item;
44      }
45    }
46    const callbacks = transferCallbacks(transfer, dataCloneError);
47    try {
48      return clone(
49        value,
50        transfer,
51        classify,
52        callbacks.cloneHost,
53        dataCloneError,
54        callbacks.prepare,
55        callbacks.validate,
56        callbacks.commit,
57      );
58    } finally {
59      callbacks.clear();
60    }
61  },
62}.structuredClone;
63define(globalThis, "structuredClone", {
64  value: structuredClone,
65  writable: true,
66  configurable: true,
67  enumerable: true,
68});
69setStructuredClone(structuredClone);
70export { structuredClone };