@isoloop/runtime 0.1.0-next.8 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # @isoloop/runtime
2
+
3
+ The [Isoloop](https://isoloop.dev) runtime inside each worker: write a part of your app as an isolate, with a UI or headless, in plain TypeScript.
4
+
5
+ ## What you get
6
+
7
+ - **One way to write an isolate:** `defineIsolate` with a `start(ctx)`, for a widget, a feed, an engine or a request handler.
8
+ - **Plain DOM in a worker:** create elements, listen to input and style your surface, and the host draws it.
9
+ - **The bus:** typed publish and subscribe, straight to the bus worker, with replay after a restart.
10
+ - **The page when you need it:** async calls to measure, focus, scroll, navigate and get a canvas to draw on.
11
+ - **A lifecycle:** shutdown handlers, error reporting, and a hold against suspension.
12
+ - **Unit tests without a worker:** `createTestContext` from `@isoloop/runtime/testing`.
13
+
14
+ ## Install
15
+
16
+ ```sh
17
+ npm install @isoloop/runtime
18
+ ```
19
+
20
+ ## Example
21
+
22
+ ```ts
23
+ import { defineIsolate } from '@isoloop/runtime';
24
+ import { count } from './topics'; // a topic from your contracts
25
+
26
+ export default defineIsolate<HTMLElement>({
27
+ start(ctx) {
28
+ const label = document.createElement('p');
29
+ label.textContent = 'Waiting for a count';
30
+ ctx.root?.append(label);
31
+
32
+ ctx.bus.subscribe(
33
+ count,
34
+ (record) => {
35
+ label.textContent = `Count: ${record.value}`;
36
+ },
37
+ { from: 'earliest' },
38
+ );
39
+ },
40
+ });
41
+ ```
42
+
43
+ ## Learn more
44
+
45
+ - [Writing isolates](https://isoloop.dev/docs/isolates/)
46
+ - [Testing isolates and contracts](https://isoloop.dev/docs/testing/)
47
+ - [What Isoloop can't hide](https://isoloop.dev/docs/limits/)
48
+ - [API reference](https://isoloop.dev/docs/api/runtime/)
package/dist/bootstrap.js CHANGED
@@ -240,8 +240,11 @@ var init_dist = __esmMin((() => {
240
240
  seq
241
241
  };
242
242
  BusError = class extends Error {
243
+ /** What went wrong, to branch on: `invalid`, `forbidden`, `timeout`, `bus_unavailable`, … */
243
244
  code;
245
+ /** For `invalid`: where the value failed its schema, and why. */
244
246
  issues;
247
+ /** @param error The broker's error: its code, message and issues. */
245
248
  constructor(error) {
246
249
  super(error.message);
247
250
  this.name = "BusError";
@@ -293,7 +296,7 @@ var init_dist = __esmMin((() => {
293
296
  };
294
297
  isInspectToIsolate = envelopeGuard(INSPECT_TO_ISOLATE);
295
298
  }));
296
- // src/busy.ts
299
+ // src/pools/busy.ts
297
300
  function createBusy(now = () => performance.now()) {
298
301
  let frame = null;
299
302
  const spent = /* @__PURE__ */ new Map();
@@ -606,7 +609,7 @@ var init_busy = __esmMin((() => {
606
609
  busy = createBusy();
607
610
  BUSY = Symbol.for("isoloop.busy");
608
611
  }));
609
- // src/freeze.ts
612
+ // src/pools/freeze.ts
610
613
  init_dist();
611
614
  init_busy();
612
615
  /** Constructors whose `prototype` and statics are frozen, if this engine has them. */
@@ -769,7 +772,7 @@ function freezeBuiltins() {
769
772
  Object.freeze(JSON);
770
773
  Object.freeze(Reflect);
771
774
  }
772
- // src/inspector.ts
775
+ // src/isolate/inspector.ts
773
776
  function createInspectorClient() {
774
777
  let port = null;
775
778
  let ended = false;
@@ -950,7 +953,7 @@ var init_inspector = __esmMin((() => {
950
953
  failure = (r) => r.ok ? void 0 : new BusError(r.error);
951
954
  endedError = () => /* @__PURE__ */ new Error("inspector: its port has ended; controls no longer act");
952
955
  }));
953
- // src/bus-client.ts
956
+ // src/bus/bus-client.ts
954
957
  function createBusClient(first, o = {}) {
955
958
  const limit = o.inboxLimit ?? 1e3;
956
959
  const now = o.now ?? (() => performance.now());
@@ -1241,7 +1244,7 @@ var init_bus_client = __esmMin((() => {
1241
1244
  };
1242
1245
  REQUEST_TIMEOUT_MS = 3e4;
1243
1246
  }));
1244
- // src/host-calls.ts
1247
+ // src/isolate/host-calls.ts
1245
1248
  function createHostCalls(opts) {
1246
1249
  const timeoutMs = opts.timeoutMs ?? 5e3;
1247
1250
  const pending = /* @__PURE__ */ new Map();
@@ -1315,7 +1318,12 @@ function createHostCalls(opts) {
1315
1318
  var HostCallError, isRect;
1316
1319
  var init_host_calls = __esmMin((() => {
1317
1320
  HostCallError = class extends Error {
1321
+ /** What went wrong: `not_found`, `no_surface`, `forbidden`, `invalid`, `timeout`, … */
1318
1322
  code;
1323
+ /**
1324
+ * @param code What went wrong.
1325
+ * @param message A sentence for people.
1326
+ */
1319
1327
  constructor(code, message) {
1320
1328
  super(message);
1321
1329
  this.name = "HostCallError";
@@ -1329,7 +1337,7 @@ var init_host_calls = __esmMin((() => {
1329
1337
  "height"
1330
1338
  ].every((k) => typeof Reflect.get(x, k) === "number");
1331
1339
  }));
1332
- // src/keep-awake.ts
1340
+ // src/isolate/keep-awake.ts
1333
1341
  function createKeepAwake(now = () => performance.now()) {
1334
1342
  const holds = /* @__PURE__ */ new Map();
1335
1343
  let next = 0;
@@ -1354,12 +1362,12 @@ function createKeepAwake(now = () => performance.now()) {
1354
1362
  };
1355
1363
  }
1356
1364
  var init_keep_awake = __esmMin((() => {}));
1357
- // ../dom/dist/records-wVZrPpqr.js
1365
+ // ../dom/dist/records-qo23ZEsQ.js
1358
1366
  var ISOLOOP_DOM_FMT;
1359
- var init_records_wVZrPpqr = __esmMin((() => {
1367
+ var init_records_qo23ZEsQ = __esmMin((() => {
1360
1368
  ISOLOOP_DOM_FMT = "isoloop-dom@1";
1361
1369
  }));
1362
- // ../dom/dist/dom-BHxfO_GX.js
1370
+ // ../dom/dist/dom-Bd22osRG.js
1363
1371
  function createStyle(el) {
1364
1372
  const decls = /* @__PURE__ */ new Map();
1365
1373
  const write = () => {
@@ -1411,7 +1419,7 @@ function createStyle(el) {
1411
1419
  });
1412
1420
  }
1413
1421
  /**
1414
- * `element.dataset` (DOM group 1, ADR 0010): camelCase keys over the
1422
+ * `element.dataset`: camelCase keys over the
1415
1423
  * `data-*` attributes, so every write travels as an `attr` record.
1416
1424
  */
1417
1425
  function createDataset(el) {
@@ -1573,6 +1581,7 @@ function recorder(root, onBatch, schedule) {
1573
1581
  }
1574
1582
  };
1575
1583
  }
1584
+ /** @internal */
1576
1585
  function createDom(opts) {
1577
1586
  const schedule = opts.schedule ?? ((fn) => queueMicrotask(fn));
1578
1587
  const core = {
@@ -1651,7 +1660,7 @@ function toEvent(e) {
1651
1660
  return ev;
1652
1661
  }
1653
1662
  var HTML_NS, SVG_NS, str, domError, emit, WEventTarget, WNode, WCharacterData, WText, WComment, kebab, WDOMTokenList, dataAttr, dataKey, WParentNode, WDocumentFragment, WElement, HANDLER_EVENTS, MEASURE, ANIMATE, LAYOUT_METHODS, LAYOUT_PROPERTIES, layoutError, WHTMLElement, WSVGElement, WHTMLIFrameElement, WHTMLInputElement, WHTMLTextAreaElement, WHTMLSelectElement, WHTMLOptionElement, HTML_CLASSES, WDocument, isControl;
1654
- var init_dom_BHxfO_GX = __esmMin((() => {
1663
+ var init_dom_Bd22osRG = __esmMin((() => {
1655
1664
  HTML_NS = "http://www.w3.org/1999/xhtml";
1656
1665
  SVG_NS = "http://www.w3.org/2000/svg";
1657
1666
  str = (v) => typeof v === "string" ? v : typeof v === "number" || typeof v === "boolean" || typeof v === "bigint" ? String(v) : "";
@@ -1808,7 +1817,7 @@ var init_dom_BHxfO_GX = __esmMin((() => {
1808
1817
  /**
1809
1818
  * @internal The root this node was rendered under, once it has been. Kept
1810
1819
  * after the node leaves the tree, so it can't move to another member's root
1811
- * in a pool (ADR 0015, decision 3).
1820
+ * in a pool.
1812
1821
  */
1813
1822
  isoOwner = null;
1814
1823
  constructor(doc, id) {
@@ -2099,7 +2108,7 @@ var init_dom_BHxfO_GX = __esmMin((() => {
2099
2108
  /** @internal */ isoStyle = null;
2100
2109
  /** @internal */ isoClassList = null;
2101
2110
  /** @internal */ isoDataset = null;
2102
- /** @internal The offsets the last `scroll` event brought (design §4.6). */
2111
+ /** @internal The offsets the last `scroll` event brought. */
2103
2112
  isoScroll = {
2104
2113
  top: 0,
2105
2114
  left: 0
@@ -2428,7 +2437,7 @@ var init_dom_BHxfO_GX = __esmMin((() => {
2428
2437
  body;
2429
2438
  /**
2430
2439
  * A detached head: React 19 hoists `<title>`, `<meta>` and `<link>` into
2431
- * it. The bridge doesn't render it (M3a task 3).
2440
+ * it. The bridge doesn't render it.
2432
2441
  */
2433
2442
  head;
2434
2443
  constructor(core) {
@@ -2458,7 +2467,7 @@ var init_dom_BHxfO_GX = __esmMin((() => {
2458
2467
  return new WDocumentFragment(this, -3);
2459
2468
  }
2460
2469
  /**
2461
- * The first element in tree order with this `id` (DOM group 1, ADR 0010):
2470
+ * The first element in tree order with this `id`:
2462
2471
  * under the render root, then the detached head. Nodes not yet inserted
2463
2472
  * aren't in the document, so they aren't found.
2464
2473
  */
@@ -2479,8 +2488,7 @@ var init_dom_BHxfO_GX = __esmMin((() => {
2479
2488
  }
2480
2489
  /**
2481
2490
  * Type selectors only, alone or as a `head > title` child chain: what React
2482
- * 19 asks when it hoists a `<title>`. Queries in general are DOM group 2
2483
- * (ADR 0010), built when a library needs them; anything else throws.
2491
+ * 19 asks when it hoists a `<title>`. Anything else throws.
2484
2492
  */
2485
2493
  querySelector(selector) {
2486
2494
  const steps = selector.split(">").map((t) => t.trim().toLowerCase());
@@ -2512,11 +2520,13 @@ var init_dom_BHxfO_GX = __esmMin((() => {
2512
2520
  defineHandlers(WDocument.prototype);
2513
2521
  isControl = (node) => node instanceof WHTMLInputElement || node instanceof WHTMLTextAreaElement || node instanceof WHTMLSelectElement;
2514
2522
  }));
2515
- // ../dom/dist/install-MjRUtHp6.js
2523
+ // ../dom/dist/install-0dSawI50.js
2516
2524
  /**
2517
2525
  * Exposes `dom` as the worker's `document`, `window` and DOM classes, so
2518
2526
  * frameworks find them where they look. Only for isolates with a surface; a
2519
2527
  * headless isolate never calls this.
2528
+ *
2529
+ * @internal
2520
2530
  */
2521
2531
  function install(dom, scope) {
2522
2532
  const define = (name, value) => {
@@ -2535,8 +2545,8 @@ function install(dom, scope) {
2535
2545
  dom.document.defaultView = scope;
2536
2546
  }
2537
2547
  var DOM_GLOBALS;
2538
- var init_install_MjRUtHp6 = __esmMin((() => {
2539
- init_dom_BHxfO_GX();
2548
+ var init_install_0dSawI50 = __esmMin((() => {
2549
+ init_dom_Bd22osRG();
2540
2550
  DOM_GLOBALS = {
2541
2551
  Node: WNode,
2542
2552
  Element: WElement,
@@ -2555,11 +2565,11 @@ var init_install_MjRUtHp6 = __esmMin((() => {
2555
2565
  }));
2556
2566
  // ../dom/dist/worker.js
2557
2567
  var init_worker = __esmMin((() => {
2558
- init_records_wVZrPpqr();
2559
- init_dom_BHxfO_GX();
2560
- init_install_MjRUtHp6();
2568
+ init_records_qo23ZEsQ();
2569
+ init_dom_Bd22osRG();
2570
+ init_install_0dSawI50();
2561
2571
  }));
2562
- // src/render/index.ts
2572
+ // src/render/render.ts
2563
2573
  function sharedDom(scope) {
2564
2574
  if (!shared) {
2565
2575
  shared = createDom({
@@ -2693,8 +2703,8 @@ var init_render = __esmMin((() => {
2693
2703
  headListeners = /* @__PURE__ */ new Set();
2694
2704
  }));
2695
2705
  // package.json
2696
- var version = "0.1.0-next.8";
2697
- // src/vanilla.ts
2706
+ var version = "0.1.0";
2707
+ // src/isolate/vanilla.ts
2698
2708
  var vanilla_exports = /* @__PURE__ */ __exportAll({
2699
2709
  REQUIRES: () => REQUIRES,
2700
2710
  createVanillaIsolate: () => createVanillaIsolate,
@@ -2702,7 +2712,23 @@ var vanilla_exports = /* @__PURE__ */ __exportAll({
2702
2712
  });
2703
2713
  /**
2704
2714
  * Types an isolate module's default export and stamps it with the version
2705
- * it was built against. `Root` defaults to the structural `IsolateRoot`.
2715
+ * it was built against.
2716
+ * @param def The isolate: its `start(ctx)`.
2717
+ * @returns The definition, with `requires` set to this build's `major.minor`.
2718
+ * @example
2719
+ * ```ts
2720
+ * import { defineIsolate } from '@isoloop/runtime';
2721
+ * import { count } from './topics';
2722
+ *
2723
+ * export default defineIsolate<HTMLElement>({
2724
+ * start(ctx) {
2725
+ * const button = document.createElement('button');
2726
+ * button.textContent = 'Add one';
2727
+ * button.addEventListener('click', () => void ctx.bus.publish(count, { value: 1 }));
2728
+ * ctx.root?.append(button);
2729
+ * },
2730
+ * });
2731
+ * ```
2706
2732
  */
2707
2733
  function defineIsolate(def) {
2708
2734
  return {
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import { a as defineIsolate, c as ScrollIntoViewOptionsLike, d as IsolateInspector, i as REQUIRES, l as BusClient, n as IsolateDefinition, o as HostApi, r as IsolateRoot, s as HostCallError, t as IsolateContext, u as SubscribeOptions } from "./vanilla-BB6MHSfu.js";
1
+ import { a as defineIsolate, c as ScrollIntoViewOptionsLike, d as SubscribeOptions, f as IsolateInspector, i as REQUIRES, l as BusClient, n as IsolateDefinition, o as HostApi, r as IsolateRoot, s as HostCallError, t as IsolateContext, u as RequestOptions } from "./vanilla-C6wHItsG.js";
2
2
  import { BusError } from "@isoloop/protocol";
3
- export { type BusClient, BusError, type HostApi, HostCallError, type IsolateContext, type IsolateDefinition, type IsolateInspector, type IsolateRoot, REQUIRES, type ScrollIntoViewOptionsLike, type SubscribeOptions, defineIsolate };
3
+ export { type BusClient, BusError, type HostApi, HostCallError, type IsolateContext, type IsolateDefinition, type IsolateInspector, type IsolateRoot, REQUIRES, type RequestOptions, type ScrollIntoViewOptionsLike, type SubscribeOptions, defineIsolate };
package/dist/index.js CHANGED
@@ -1,21 +1,46 @@
1
1
  import { BusError, majorMinor } from "@isoloop/protocol";
2
2
  import "@isoloop/dom/worker";
3
- // src/host-calls.ts
4
- /** A host call that failed, with the protocol's error code. */
3
+ // src/isolate/host-calls.ts
4
+ /** A host call (`ctx.host`) that failed, with an error code to branch on. */
5
5
  var HostCallError = class extends Error {
6
+ /** What went wrong: `not_found`, `no_surface`, `forbidden`, `invalid`, `timeout`, … */
6
7
  code;
8
+ /**
9
+ * @param code What went wrong.
10
+ * @param message A sentence for people.
11
+ */
7
12
  constructor(code, message) {
8
13
  super(message);
9
14
  this.name = "HostCallError";
10
15
  this.code = code;
11
16
  }
12
17
  };
13
- // src/vanilla.ts
14
- /** This build's stamp: `major.minor` of the package version, inlined when tsdown bundles it. */
15
- const REQUIRES = majorMinor("0.1.0-next.8") ?? "0.0";
18
+ // src/isolate/vanilla.ts
19
+ /**
20
+ * This build's stamp: `major.minor` of the package version, inlined when tsdown bundles it.
21
+ *
22
+ * @internal
23
+ */
24
+ const REQUIRES = majorMinor("0.1.0") ?? "0.0";
16
25
  /**
17
26
  * Types an isolate module's default export and stamps it with the version
18
- * it was built against. `Root` defaults to the structural `IsolateRoot`.
27
+ * it was built against.
28
+ * @param def The isolate: its `start(ctx)`.
29
+ * @returns The definition, with `requires` set to this build's `major.minor`.
30
+ * @example
31
+ * ```ts
32
+ * import { defineIsolate } from '@isoloop/runtime';
33
+ * import { count } from './topics';
34
+ *
35
+ * export default defineIsolate<HTMLElement>({
36
+ * start(ctx) {
37
+ * const button = document.createElement('button');
38
+ * button.textContent = 'Add one';
39
+ * button.addEventListener('click', () => void ctx.bus.publish(count, { value: 1 }));
40
+ * ctx.root?.append(button);
41
+ * },
42
+ * });
43
+ * ```
19
44
  */
20
45
  function defineIsolate(def) {
21
46
  return {
package/dist/testing.d.ts CHANGED
@@ -1,16 +1,21 @@
1
- import { o as HostApi, t as IsolateContext } from "./vanilla-BB6MHSfu.js";
1
+ import { o as HostApi, t as IsolateContext } from "./vanilla-C6wHItsG.js";
2
2
  import { ShutdownReason } from "@isoloop/protocol";
3
+ /** `createTestContext`'s options. */
3
4
  export type TestContextOptions = {
5
+ /** `ctx.id`. Default `'test'`. */
4
6
  id?: string;
7
+ /** `ctx.config`. Default `null`. */
5
8
  config?: unknown;
6
- /** The render root, e.g. an element on the test page; omit for a headless context. */
9
+ /** The render root, for example an element on the test page. Default: none, a headless context. */
7
10
  root?: object;
8
- /** Answers `bus.request`; default: reject with `timeout` after the request's `timeoutMs`. */
11
+ /** Answers `bus.request`. Default: reject with `timeout` after the request's `timeoutMs`. */
9
12
  respond?: (topic: string, value: unknown) => unknown;
10
- /** Answers host calls; default: `measure` gives a zero rect, the rest resolve. */
13
+ /** Answers host calls. Default: `measure` gives a zero rect, the rest resolve. */
11
14
  host?: Partial<HostApi>;
12
15
  };
16
+ /** A context to test an isolate with, and the handles to drive it and see what it did. */
13
17
  export type TestContext = {
18
+ /** The context to pass to `start(ctx)`, or to render a component with. */
14
19
  readonly ctx: IsolateContext;
15
20
  /** What `ctx.reportError` got, in order. */
16
21
  readonly errors: {
@@ -19,7 +24,7 @@ export type TestContext = {
19
24
  }[];
20
25
  /** Delivers a record to every subscription on `topic`, in order. */
21
26
  emit: (topic: string, value: unknown, key?: string) => void;
22
- /** Runs `onReset` of every subscription on `topic` (a bus restart lost it, ADR 0007). */
27
+ /** Runs `onReset` of every subscription on `topic`, as when a bus restart loses a memory topic. */
23
28
  reset: (topic: string) => void;
24
29
  /** Every `publish`, in order. */
25
30
  readonly published: {
@@ -32,7 +37,15 @@ export type TestContext = {
32
37
  topic: string;
33
38
  from: string;
34
39
  }[];
35
- /** Runs the `onShutdown` handlers, as the runtime does on `stop`. */
40
+ /** Runs the `onShutdown` handlers, as the runtime does on `stop`. `reason` defaults to `'stop'`. */
36
41
  shutdown: (reason?: ShutdownReason) => Promise<void>;
37
42
  };
43
+ /**
44
+ * Makes an `IsolateContext` with no worker, bus or host behind it, for
45
+ * testing an isolate or a component on a page. The test drives the bus
46
+ * (`emit`, `reset`) and sees what the isolate did (`published`,
47
+ * `subscriptions`, `errors`). `ctx.inspector` is `undefined`.
48
+ * @param o The id, config, root, and how requests and host calls are answered.
49
+ * @returns The context and its handles.
50
+ */
38
51
  export declare function createTestContext(o?: TestContextOptions): TestContext;
package/dist/testing.js CHANGED
@@ -6,6 +6,14 @@ const ZERO = {
6
6
  width: 0,
7
7
  height: 0
8
8
  };
9
+ /**
10
+ * Makes an `IsolateContext` with no worker, bus or host behind it, for
11
+ * testing an isolate or a component on a page. The test drives the bus
12
+ * (`emit`, `reset`) and sees what the isolate did (`published`,
13
+ * `subscriptions`, `errors`). `ctx.inspector` is `undefined`.
14
+ * @param o The id, config, root, and how requests and host calls are answered.
15
+ * @returns The context and its handles.
16
+ */
9
17
  function createTestContext(o = {}) {
10
18
  const subs = /* @__PURE__ */ new Set();
11
19
  const published = [];
@@ -0,0 +1,359 @@
1
+ import { ErrorCode, InspectMessage, InspectRecord, InspectSnapshot, Rect, ShutdownReason, StartPosition, TopicRef, TypedRecord } from "@isoloop/protocol";
2
+ /**
3
+ * The inspection feed and its controls, as an isolate with `inspect: true`
4
+ * sees them: `ctx.inspector`, on a port of its own to the host. It's
5
+ * `undefined` without the grant.
6
+ * @experimental
7
+ */
8
+ type IsolateInspector = {
9
+ /**
10
+ * The feed's state now.
11
+ * @returns Isolates, pools, the bus, and the last 200 events. Once the port
12
+ * has ended, the last snapshot it got, if any.
13
+ */
14
+ snapshot(): Promise<InspectSnapshot>;
15
+ /**
16
+ * The host's feed messages from now on: events at once, beats and bus
17
+ * metrics about once a second.
18
+ * @param fn Called with each message.
19
+ * @returns The function that stops them.
20
+ */
21
+ on(fn: (m: InspectMessage) => void): () => void;
22
+ /**
23
+ * The last records of a topic still retained, oldest first, as previews.
24
+ * @param topic The topic's name.
25
+ * @param o `last`: how many, at most 100.
26
+ * @returns The records.
27
+ * @throws Rejects with a `BusError` from the bus.
28
+ */
29
+ records(topic: string, o: {
30
+ last: number;
31
+ }): Promise<InspectRecord[]>;
32
+ /**
33
+ * Records appended to matching topics, as previews. Not a consumer: it
34
+ * isn't in the bus figures.
35
+ * @param o `topics`: names or patterns. `maxPerSec`: at most this many a
36
+ * second, default 50. `onSkipped`: gets each count of records left out
37
+ * over the cap, at most once a second.
38
+ * @param fn Called with each record.
39
+ * @returns The function that ends it.
40
+ */
41
+ tap(o: {
42
+ topics: string[];
43
+ maxPerSec?: number;
44
+ onSkipped?: (count: number) => void;
45
+ }, fn: (r: InspectRecord) => void): () => void;
46
+ /**
47
+ * Restarts an isolate, as `kernel.restart(id)` does.
48
+ * @param id The isolate's id.
49
+ * @returns Resolves once the restart has started.
50
+ * @throws Rejects for an unknown id, and once the port has ended.
51
+ */
52
+ restart(id: string): Promise<void>;
53
+ /**
54
+ * Ends an isolate's worker as a crash (`worker_error`), so the supervisor
55
+ * restarts it. For a pooled isolate that ends the pool's worker: every
56
+ * member crashes and restarts, and the feed shows each one.
57
+ * @param id The isolate's id.
58
+ * @returns Resolves once the kill is sent, not when the isolate is back.
59
+ * @throws Rejects for an unknown id, and once the port has ended.
60
+ */
61
+ kill(id: string): Promise<void>;
62
+ /**
63
+ * Busy-loops an isolate's worker.
64
+ * @param id The isolate's id.
65
+ * @param ms How long, at most 10 000 (10 s).
66
+ * @returns Resolves once the block is sent.
67
+ * @throws Rejects for an unknown id, and once the port has ended.
68
+ */
69
+ block(id: string, ms: number): Promise<void>;
70
+ };
71
+ /** `bus.request`'s options. @experimental */
72
+ type RequestOptions = {
73
+ /** The request record's key. Default: none. */
74
+ key?: string;
75
+ /** How long to wait for a reply, in ms; the call then rejects with `BusError('timeout')`. Default 30 000 (30 s). */
76
+ timeoutMs?: number;
77
+ };
78
+ /** `bus.subscribe`'s options. */
79
+ type SubscribeOptions = {
80
+ /**
81
+ * Where to start. Default `latest`. With `group`, it applies only while the
82
+ * group has no committed offset: a group that has committed resumes after
83
+ * it, and a member joining a group that has members takes records from
84
+ * where the group is.
85
+ */
86
+ from?: StartPosition;
87
+ /** Share the topic with other subscribers of the same group: each record goes to one of them. Default: none. @experimental */
88
+ group?: string;
89
+ /** Commit each record once its handler settles. Default true. @experimental */
90
+ autoCommit?: boolean;
91
+ /**
92
+ * A bus restart lost this memory topic. Clear what was built from it;
93
+ * records from the new bus follow. Default: none.
94
+ */
95
+ onReset?: () => void;
96
+ /**
97
+ * Records in `[from, to]` were dropped by retention before they reached
98
+ * this subscription, for example while the isolate was suspended. Default: none.
99
+ */
100
+ onGap?: (gap: {
101
+ from: number;
102
+ to: number;
103
+ }) => void;
104
+ };
105
+ /** An isolate's side of the bus: `ctx.bus`, on its own port to the bus worker. */
106
+ interface BusClient {
107
+ /**
108
+ * Appends a record to a topic.
109
+ * @param topic The topic, from the contracts.
110
+ * @param rec The record's `value`, and its `key` if any.
111
+ * @returns Resolves once the broker has appended the record.
112
+ * @throws Rejects with a `BusError`, for example `forbidden` without a
113
+ * publish grant, `invalid` when the value fails the schema, or
114
+ * `bus_unavailable` while 1,000 calls wait for an answer.
115
+ */
116
+ publish<In>(topic: TopicRef<In, any, any, any>, rec: {
117
+ key?: string;
118
+ value: In;
119
+ }): Promise<void>;
120
+ /**
121
+ * Delivers a topic's records to `fn`, one at a time, in order: the next
122
+ * waits until `fn`'s promise settles. One subscription per topic:
123
+ * subscribing again replaces the last one.
124
+ * @param topic The topic, from the contracts.
125
+ * @param fn Called with each record.
126
+ * @param opts Where to start, the group, and the reset and gap handlers.
127
+ * @returns The function that unsubscribes.
128
+ */
129
+ subscribe<Out>(topic: TopicRef<any, Out, any, any>, fn: (r: TypedRecord<Out>) => void | Promise<void>, opts?: SubscribeOptions): () => void;
130
+ /**
131
+ * Appends a request record to a topic with a `reply` schema and waits for
132
+ * the first valid reply.
133
+ * @param topic A topic with a `reply` schema.
134
+ * @param value The request's value, checked against the topic's schema.
135
+ * @param opts The key and the timeout.
136
+ * @returns The reply's value, checked against the `reply` schema.
137
+ * @throws Rejects with a `BusError`: `timeout` after `timeoutMs` (default
138
+ * 30 s); `dropped` at once when the topic's retention removed the request
139
+ * before the bus delivered it to any subscriber; `not_requestable`,
140
+ * `forbidden`, `invalid` or `reply_invalid` from the broker.
141
+ * @experimental
142
+ */
143
+ request<In, ROut>(topic: TopicRef<In, any, any, ROut>, value: In, opts?: RequestOptions): Promise<ROut>;
144
+ /**
145
+ * Answers a request record delivered to this isolate.
146
+ * @param record The request, as the subscription delivered it.
147
+ * @param value The answer; the broker checks it against the topic's `reply` schema.
148
+ * @returns Resolves once the broker has the reply.
149
+ * @throws `Error` for a record not delivered to this isolate; rejects with a
150
+ * `BusError` (`reply_invalid`, …) from the broker.
151
+ * @experimental
152
+ */
153
+ reply(record: TypedRecord<unknown>, value: unknown): Promise<void>;
154
+ /**
155
+ * Commits a record by hand, for `autoCommit: false`: that record and every
156
+ * one delivered before it on its subscription count as settled.
157
+ * @param record A record this isolate's subscription delivered.
158
+ * @throws `Error` for a record not delivered to this isolate.
159
+ * @experimental
160
+ */
161
+ commit(record: TypedRecord<unknown>): void;
162
+ }
163
+ /** A host call (`ctx.host`) that failed, with an error code to branch on. */
164
+ declare class HostCallError extends Error {
165
+ /** What went wrong: `not_found`, `no_surface`, `forbidden`, `invalid`, `timeout`, … */
166
+ readonly code: ErrorCode;
167
+ /**
168
+ * @param code What went wrong.
169
+ * @param message A sentence for people.
170
+ */
171
+ constructor(code: ErrorCode, message: string);
172
+ }
173
+ /**
174
+ * What `ctx.host` offers: calls the host runs on the main thread for the
175
+ * isolate. Each runs after the DOM changes made before it, and rejects with a
176
+ * `HostCallError`: `timeout` after 5 s without an answer, `not_found` for a
177
+ * node that isn't rendered, `no_surface` in a headless isolate.
178
+ */
179
+ type HostApi = {
180
+ /**
181
+ * Asks the host to navigate. `/x` resolves against the host's `basePath`.
182
+ * @param href Where to go: same-origin.
183
+ * @param o `replace` the current history entry rather than push one, and the `state` to set.
184
+ * @returns The host's answer: `true` accepted, `false` refused.
185
+ * @throws `HostCallError` `forbidden` without `grants.navigate`, or
186
+ * `routing_disabled` when the host turned routing off.
187
+ */
188
+ navigate(href: string, o?: {
189
+ replace?: boolean;
190
+ state?: unknown;
191
+ }): Promise<boolean>;
192
+ /**
193
+ * Measures a rendered node, after pending changes apply.
194
+ * @param node A node this isolate rendered.
195
+ * @returns Its box relative to the surface, in CSS pixels.
196
+ */
197
+ measure(node: unknown): Promise<Rect>;
198
+ /**
199
+ * Focuses a rendered node on the page.
200
+ * @param node A node this isolate rendered.
201
+ * @returns Resolves once the host has focused it.
202
+ */
203
+ focus(node: unknown): Promise<void>;
204
+ /**
205
+ * Scrolls the page so a rendered node is in view.
206
+ * @param node A node this isolate rendered.
207
+ * @param opts As the DOM's `scrollIntoView` takes them.
208
+ * @returns Resolves once the host has scrolled.
209
+ */
210
+ scrollIntoView(node: unknown, opts?: ScrollIntoViewOptionsLike): Promise<void>;
211
+ /**
212
+ * Hands a rendered `<canvas>` to the isolate as an `OffscreenCanvas`. Draw
213
+ * on it from the worker. The element then gets `resize` events with its CSS
214
+ * `width` and `height`, the first at once.
215
+ * @param node A `<canvas>` this isolate rendered.
216
+ * @param size Its size in canvas pixels, if given.
217
+ * @returns The canvas, transferred to the worker.
218
+ * @throws `HostCallError` `invalid` for a canvas already handed over (each
219
+ * goes once), `no_surface` in a headless isolate.
220
+ */
221
+ requestCanvas(node: unknown, size?: {
222
+ width?: number;
223
+ height?: number;
224
+ }): Promise<OffscreenCanvas>;
225
+ };
226
+ /** `scrollIntoView`'s options, typed here since worker code has no DOM types. */
227
+ type ScrollIntoViewOptionsLike = {
228
+ /** Default `'auto'`. */
229
+ behavior?: "auto" | "instant" | "smooth";
230
+ /** Default `'start'`. */
231
+ block?: "start" | "center" | "end" | "nearest";
232
+ /** Default `'nearest'`. */
233
+ inline?: "start" | "center" | "end" | "nearest";
234
+ };
235
+ /**
236
+ * What a render root offers, structurally, so DOM-typed isolate code can pass
237
+ * its own nodes. Each member works as the DOM's. Use
238
+ * `defineIsolate<HTMLElement>(…)` to get the DOM's types instead.
239
+ */
240
+ interface IsolateRoot {
241
+ append(...nodes: (object | string)[]): void;
242
+ replaceChildren(...nodes: (object | string)[]): void;
243
+ appendChild<T extends object>(node: T): T;
244
+ insertBefore<T extends object>(node: T, ref: object | null): T;
245
+ removeChild<T extends object>(node: T): T;
246
+ readonly childNodes: ArrayLike<object>;
247
+ textContent: string | null;
248
+ addEventListener(type: string, fn: (event: never) => void, options?: unknown): void;
249
+ }
250
+ /** An isolate's whole world, as `start(ctx)` gets it. */
251
+ type IsolateContext<Root = IsolateRoot> = {
252
+ /** The isolate's id, from the manifest. */
253
+ readonly id: string;
254
+ /** `manifest.config`, passed through. */
255
+ readonly config: unknown;
256
+ /** The render target inside the surface; `undefined` for a headless isolate. */
257
+ readonly root: Root | undefined;
258
+ /**
259
+ * A container inside `root` for modals, tooltips, toasts and menus that
260
+ * libraries would otherwise render into `document.body`, which reaches no
261
+ * surface. Made on first use, typically from a component while it renders,
262
+ * and added as `root`'s last child once that render has committed;
263
+ * `undefined` for a headless isolate.
264
+ */
265
+ readonly portalRoot: Root | undefined;
266
+ /** Calls the host runs on the main thread: navigate, measure, focus, scroll, hand over a canvas. */
267
+ readonly host: HostApi;
268
+ /** The bus, on its own port to the bus worker. */
269
+ readonly bus: BusClient;
270
+ /**
271
+ * The inspection feed and its controls, on a port of its own to the host;
272
+ * `undefined` unless the isolate holds `inspect: true`.
273
+ * @experimental
274
+ */
275
+ readonly inspector: IsolateInspector | undefined;
276
+ /**
277
+ * Adds stylesheets to the surface; a no-op when headless.
278
+ * @param css One stylesheet's text, or several.
279
+ */
280
+ styles(css: string | readonly string[]): void;
281
+ /**
282
+ * Sets an `<input>`'s, `<textarea>`'s or `<select>`'s value. The host owns
283
+ * a focused input's value, so a plain `input.value = …` waits until the
284
+ * user leaves it; with `force` the host applies it at once and keeps the
285
+ * caret at the end, for example to clear a chat box after sending.
286
+ * @param node The element, as this isolate rendered it.
287
+ * @param value The new value.
288
+ * @param opts `force`: apply it even while the user is in the field. Default false.
289
+ */
290
+ setValue(node: unknown, value: string, opts?: {
291
+ force?: boolean;
292
+ }): void;
293
+ /**
294
+ * Reports an error as this isolate's own: framework adapters call it for a
295
+ * render error their boundary caught, so in a pool it crashes only this
296
+ * member, not the whole worker.
297
+ * @param error What was thrown.
298
+ * @param opts `fatal`: crash the isolate, which the host restarts as usual.
299
+ * Default true. With false the host emits `isolate:error` and the isolate
300
+ * keeps running.
301
+ */
302
+ reportError(error: unknown, opts?: {
303
+ fatal?: boolean;
304
+ }): void;
305
+ /**
306
+ * Adds a handler that runs on stop, within the host's grace period (1 s),
307
+ * before the worker goes.
308
+ * @param fn Gets why the isolate is stopping; may return a promise.
309
+ */
310
+ onShutdown(fn: (reason: ShutdownReason) => void | Promise<void>): void;
311
+ /**
312
+ * Holds the isolate against suspension. Only matters for isolates spawned
313
+ * with `suspend`.
314
+ * @returns The function that releases the hold.
315
+ */
316
+ keepAwake(): () => void;
317
+ };
318
+ /** An isolate module's default export: what `defineIsolate` returns. */
319
+ type IsolateDefinition<Root = IsolateRoot> = {
320
+ /**
321
+ * Runs once, when the isolate starts in its worker. A `start` that throws,
322
+ * or whose promise rejects, crashes the isolate.
323
+ */
324
+ start(ctx: IsolateContext<Root>): void | Promise<void>;
325
+ /**
326
+ * The Isoloop `major.minor` it was built against, stamped by
327
+ * `defineIsolate`. The host fails an isolate built for a newer Isoloop, or
328
+ * for one older than it still runs; a definition without one isn't checked.
329
+ */
330
+ requires?: string;
331
+ };
332
+ /**
333
+ * This build's stamp: `major.minor` of the package version, inlined when tsdown bundles it.
334
+ *
335
+ * @internal
336
+ */
337
+ declare const REQUIRES: string;
338
+ /**
339
+ * Types an isolate module's default export and stamps it with the version
340
+ * it was built against.
341
+ * @param def The isolate: its `start(ctx)`.
342
+ * @returns The definition, with `requires` set to this build's `major.minor`.
343
+ * @example
344
+ * ```ts
345
+ * import { defineIsolate } from '@isoloop/runtime';
346
+ * import { count } from './topics';
347
+ *
348
+ * export default defineIsolate<HTMLElement>({
349
+ * start(ctx) {
350
+ * const button = document.createElement('button');
351
+ * button.textContent = 'Add one';
352
+ * button.addEventListener('click', () => void ctx.bus.publish(count, { value: 1 }));
353
+ * ctx.root?.append(button);
354
+ * },
355
+ * });
356
+ * ```
357
+ */
358
+ declare function defineIsolate<Root = IsolateRoot>(def: IsolateDefinition<Root>): IsolateDefinition<Root>;
359
+ export { defineIsolate as a, ScrollIntoViewOptionsLike as c, SubscribeOptions as d, IsolateInspector as f, REQUIRES as i, BusClient as l, IsolateDefinition as n, HostApi as o, IsolateRoot as r, HostCallError as s, IsolateContext as t, RequestOptions as u };
package/dist/worker.js CHANGED
@@ -240,8 +240,11 @@ var init_dist = __esmMin((() => {
240
240
  seq
241
241
  };
242
242
  BusError = class extends Error {
243
+ /** What went wrong, to branch on: `invalid`, `forbidden`, `timeout`, `bus_unavailable`, … */
243
244
  code;
245
+ /** For `invalid`: where the value failed its schema, and why. */
244
246
  issues;
247
+ /** @param error The broker's error: its code, message and issues. */
245
248
  constructor(error) {
246
249
  super(error.message);
247
250
  this.name = "BusError";
@@ -293,7 +296,7 @@ var init_dist = __esmMin((() => {
293
296
  };
294
297
  isInspectToIsolate = envelopeGuard(INSPECT_TO_ISOLATE);
295
298
  }));
296
- // src/busy.ts
299
+ // src/pools/busy.ts
297
300
  function createBusy(now = () => performance.now()) {
298
301
  let frame = null;
299
302
  const spent = /* @__PURE__ */ new Map();
@@ -606,7 +609,7 @@ var init_busy = __esmMin((() => {
606
609
  busy = createBusy();
607
610
  BUSY = Symbol.for("isoloop.busy");
608
611
  }));
609
- // src/freeze.ts
612
+ // src/pools/freeze.ts
610
613
  /** The protected properties, per prototype, that are left as frozen data. */
611
614
  function protectedProperties() {
612
615
  const out = /* @__PURE__ */ new Map();
@@ -750,7 +753,7 @@ var init_freeze = __esmMin((() => {
750
753
  ];
751
754
  ITERATOR = Symbol.iterator;
752
755
  }));
753
- // src/inspector.ts
756
+ // src/isolate/inspector.ts
754
757
  function createInspectorClient() {
755
758
  let port = null;
756
759
  let ended = false;
@@ -932,7 +935,7 @@ var init_inspector = __esmMin((() => {
932
935
  failure = (r) => r.ok ? void 0 : new BusError(r.error);
933
936
  endedError = () => /* @__PURE__ */ new Error("inspector: its port has ended; controls no longer act");
934
937
  }));
935
- // src/bus-client.ts
938
+ // src/bus/bus-client.ts
936
939
  function createBusClient(first, o = {}) {
937
940
  const limit = o.inboxLimit ?? 1e3;
938
941
  const now = o.now ?? (() => performance.now());
@@ -1223,7 +1226,7 @@ var init_bus_client = __esmMin((() => {
1223
1226
  };
1224
1227
  REQUEST_TIMEOUT_MS = 3e4;
1225
1228
  }));
1226
- // src/host-calls.ts
1229
+ // src/isolate/host-calls.ts
1227
1230
  function createHostCalls(opts) {
1228
1231
  const timeoutMs = opts.timeoutMs ?? 5e3;
1229
1232
  const pending = /* @__PURE__ */ new Map();
@@ -1297,7 +1300,12 @@ function createHostCalls(opts) {
1297
1300
  var HostCallError, isRect;
1298
1301
  var init_host_calls = __esmMin((() => {
1299
1302
  HostCallError = class extends Error {
1303
+ /** What went wrong: `not_found`, `no_surface`, `forbidden`, `invalid`, `timeout`, … */
1300
1304
  code;
1305
+ /**
1306
+ * @param code What went wrong.
1307
+ * @param message A sentence for people.
1308
+ */
1301
1309
  constructor(code, message) {
1302
1310
  super(message);
1303
1311
  this.name = "HostCallError";
@@ -1311,7 +1319,7 @@ var init_host_calls = __esmMin((() => {
1311
1319
  "height"
1312
1320
  ].every((k) => typeof Reflect.get(x, k) === "number");
1313
1321
  }));
1314
- // src/keep-awake.ts
1322
+ // src/isolate/keep-awake.ts
1315
1323
  function createKeepAwake(now = () => performance.now()) {
1316
1324
  const holds = /* @__PURE__ */ new Map();
1317
1325
  let next = 0;
@@ -1336,12 +1344,12 @@ function createKeepAwake(now = () => performance.now()) {
1336
1344
  };
1337
1345
  }
1338
1346
  var init_keep_awake = __esmMin((() => {}));
1339
- // ../dom/dist/records-wVZrPpqr.js
1347
+ // ../dom/dist/records-qo23ZEsQ.js
1340
1348
  var ISOLOOP_DOM_FMT;
1341
- var init_records_wVZrPpqr = __esmMin((() => {
1349
+ var init_records_qo23ZEsQ = __esmMin((() => {
1342
1350
  ISOLOOP_DOM_FMT = "isoloop-dom@1";
1343
1351
  }));
1344
- // ../dom/dist/dom-BHxfO_GX.js
1352
+ // ../dom/dist/dom-Bd22osRG.js
1345
1353
  function createStyle(el) {
1346
1354
  const decls = /* @__PURE__ */ new Map();
1347
1355
  const write = () => {
@@ -1393,7 +1401,7 @@ function createStyle(el) {
1393
1401
  });
1394
1402
  }
1395
1403
  /**
1396
- * `element.dataset` (DOM group 1, ADR 0010): camelCase keys over the
1404
+ * `element.dataset`: camelCase keys over the
1397
1405
  * `data-*` attributes, so every write travels as an `attr` record.
1398
1406
  */
1399
1407
  function createDataset(el) {
@@ -1555,6 +1563,7 @@ function recorder(root, onBatch, schedule) {
1555
1563
  }
1556
1564
  };
1557
1565
  }
1566
+ /** @internal */
1558
1567
  function createDom(opts) {
1559
1568
  const schedule = opts.schedule ?? ((fn) => queueMicrotask(fn));
1560
1569
  const core = {
@@ -1633,7 +1642,7 @@ function toEvent(e) {
1633
1642
  return ev;
1634
1643
  }
1635
1644
  var HTML_NS, SVG_NS, str, domError, emit, WEventTarget, WNode, WCharacterData, WText, WComment, kebab, WDOMTokenList, dataAttr, dataKey, WParentNode, WDocumentFragment, WElement, HANDLER_EVENTS, MEASURE, ANIMATE, LAYOUT_METHODS, LAYOUT_PROPERTIES, layoutError, WHTMLElement, WSVGElement, WHTMLIFrameElement, WHTMLInputElement, WHTMLTextAreaElement, WHTMLSelectElement, WHTMLOptionElement, HTML_CLASSES, WDocument, isControl;
1636
- var init_dom_BHxfO_GX = __esmMin((() => {
1645
+ var init_dom_Bd22osRG = __esmMin((() => {
1637
1646
  HTML_NS = "http://www.w3.org/1999/xhtml";
1638
1647
  SVG_NS = "http://www.w3.org/2000/svg";
1639
1648
  str = (v) => typeof v === "string" ? v : typeof v === "number" || typeof v === "boolean" || typeof v === "bigint" ? String(v) : "";
@@ -1790,7 +1799,7 @@ var init_dom_BHxfO_GX = __esmMin((() => {
1790
1799
  /**
1791
1800
  * @internal The root this node was rendered under, once it has been. Kept
1792
1801
  * after the node leaves the tree, so it can't move to another member's root
1793
- * in a pool (ADR 0015, decision 3).
1802
+ * in a pool.
1794
1803
  */
1795
1804
  isoOwner = null;
1796
1805
  constructor(doc, id) {
@@ -2081,7 +2090,7 @@ var init_dom_BHxfO_GX = __esmMin((() => {
2081
2090
  /** @internal */ isoStyle = null;
2082
2091
  /** @internal */ isoClassList = null;
2083
2092
  /** @internal */ isoDataset = null;
2084
- /** @internal The offsets the last `scroll` event brought (design §4.6). */
2093
+ /** @internal The offsets the last `scroll` event brought. */
2085
2094
  isoScroll = {
2086
2095
  top: 0,
2087
2096
  left: 0
@@ -2410,7 +2419,7 @@ var init_dom_BHxfO_GX = __esmMin((() => {
2410
2419
  body;
2411
2420
  /**
2412
2421
  * A detached head: React 19 hoists `<title>`, `<meta>` and `<link>` into
2413
- * it. The bridge doesn't render it (M3a task 3).
2422
+ * it. The bridge doesn't render it.
2414
2423
  */
2415
2424
  head;
2416
2425
  constructor(core) {
@@ -2440,7 +2449,7 @@ var init_dom_BHxfO_GX = __esmMin((() => {
2440
2449
  return new WDocumentFragment(this, -3);
2441
2450
  }
2442
2451
  /**
2443
- * The first element in tree order with this `id` (DOM group 1, ADR 0010):
2452
+ * The first element in tree order with this `id`:
2444
2453
  * under the render root, then the detached head. Nodes not yet inserted
2445
2454
  * aren't in the document, so they aren't found.
2446
2455
  */
@@ -2461,8 +2470,7 @@ var init_dom_BHxfO_GX = __esmMin((() => {
2461
2470
  }
2462
2471
  /**
2463
2472
  * Type selectors only, alone or as a `head > title` child chain: what React
2464
- * 19 asks when it hoists a `<title>`. Queries in general are DOM group 2
2465
- * (ADR 0010), built when a library needs them; anything else throws.
2473
+ * 19 asks when it hoists a `<title>`. Anything else throws.
2466
2474
  */
2467
2475
  querySelector(selector) {
2468
2476
  const steps = selector.split(">").map((t) => t.trim().toLowerCase());
@@ -2494,11 +2502,13 @@ var init_dom_BHxfO_GX = __esmMin((() => {
2494
2502
  defineHandlers(WDocument.prototype);
2495
2503
  isControl = (node) => node instanceof WHTMLInputElement || node instanceof WHTMLTextAreaElement || node instanceof WHTMLSelectElement;
2496
2504
  }));
2497
- // ../dom/dist/install-MjRUtHp6.js
2505
+ // ../dom/dist/install-0dSawI50.js
2498
2506
  /**
2499
2507
  * Exposes `dom` as the worker's `document`, `window` and DOM classes, so
2500
2508
  * frameworks find them where they look. Only for isolates with a surface; a
2501
2509
  * headless isolate never calls this.
2510
+ *
2511
+ * @internal
2502
2512
  */
2503
2513
  function install(dom, scope) {
2504
2514
  const define = (name, value) => {
@@ -2517,8 +2527,8 @@ function install(dom, scope) {
2517
2527
  dom.document.defaultView = scope;
2518
2528
  }
2519
2529
  var DOM_GLOBALS;
2520
- var init_install_MjRUtHp6 = __esmMin((() => {
2521
- init_dom_BHxfO_GX();
2530
+ var init_install_0dSawI50 = __esmMin((() => {
2531
+ init_dom_Bd22osRG();
2522
2532
  DOM_GLOBALS = {
2523
2533
  Node: WNode,
2524
2534
  Element: WElement,
@@ -2537,11 +2547,11 @@ var init_install_MjRUtHp6 = __esmMin((() => {
2537
2547
  }));
2538
2548
  // ../dom/dist/worker.js
2539
2549
  var init_worker = __esmMin((() => {
2540
- init_records_wVZrPpqr();
2541
- init_dom_BHxfO_GX();
2542
- init_install_MjRUtHp6();
2550
+ init_records_qo23ZEsQ();
2551
+ init_dom_Bd22osRG();
2552
+ init_install_0dSawI50();
2543
2553
  }));
2544
- // src/render/index.ts
2554
+ // src/render/render.ts
2545
2555
  function sharedDom(scope) {
2546
2556
  if (!shared) {
2547
2557
  shared = createDom({
@@ -2676,8 +2686,8 @@ var init_render = __esmMin((() => {
2676
2686
  headListeners = /* @__PURE__ */ new Set();
2677
2687
  }));
2678
2688
  // package.json
2679
- var version = "0.1.0-next.8";
2680
- // src/vanilla.ts
2689
+ var version = "0.1.0";
2690
+ // src/isolate/vanilla.ts
2681
2691
  var vanilla_exports = /* @__PURE__ */ __exportAll({
2682
2692
  REQUIRES: () => REQUIRES,
2683
2693
  createVanillaIsolate: () => createVanillaIsolate,
@@ -2685,7 +2695,23 @@ var vanilla_exports = /* @__PURE__ */ __exportAll({
2685
2695
  });
2686
2696
  /**
2687
2697
  * Types an isolate module's default export and stamps it with the version
2688
- * it was built against. `Root` defaults to the structural `IsolateRoot`.
2698
+ * it was built against.
2699
+ * @param def The isolate: its `start(ctx)`.
2700
+ * @returns The definition, with `requires` set to this build's `major.minor`.
2701
+ * @example
2702
+ * ```ts
2703
+ * import { defineIsolate } from '@isoloop/runtime';
2704
+ * import { count } from './topics';
2705
+ *
2706
+ * export default defineIsolate<HTMLElement>({
2707
+ * start(ctx) {
2708
+ * const button = document.createElement('button');
2709
+ * button.textContent = 'Add one';
2710
+ * button.addEventListener('click', () => void ctx.bus.publish(count, { value: 1 }));
2711
+ * ctx.root?.append(button);
2712
+ * },
2713
+ * });
2714
+ * ```
2689
2715
  */
2690
2716
  function defineIsolate(def) {
2691
2717
  return {
package/package.json CHANGED
@@ -1,7 +1,15 @@
1
1
  {
2
2
  "name": "@isoloop/runtime",
3
- "version": "0.1.0-next.8",
4
- "description": "Isolate-side runtime that runs inside each worker.",
3
+ "version": "0.1.0",
4
+ "description": "The Isoloop runtime: write a part of your app as an isolate in a worker, with a UI or headless, in plain TypeScript.",
5
+ "keywords": [
6
+ "isoloop",
7
+ "web-workers",
8
+ "event-loop",
9
+ "isolation",
10
+ "microfrontends"
11
+ ],
12
+ "homepage": "https://isoloop.dev/docs/api/runtime/",
5
13
  "license": "MIT",
6
14
  "type": "module",
7
15
  "sideEffects": [
@@ -35,8 +43,8 @@
35
43
  }
36
44
  },
37
45
  "dependencies": {
38
- "@isoloop/dom": "0.1.0-next.8",
39
- "@isoloop/protocol": "0.1.0-next.8"
46
+ "@isoloop/dom": "0.1.0",
47
+ "@isoloop/protocol": "0.1.0"
40
48
  },
41
49
  "devDependencies": {
42
50
  "typescript": "7.0.2"
@@ -1,220 +0,0 @@
1
- import { ErrorCode, InspectMessage, InspectRecord, InspectSnapshot, Rect, ShutdownReason, StartPosition, TopicRef, TypedRecord } from "@isoloop/protocol";
2
- /**
3
- * The inspection feed and its controls, as an isolate with `inspect: true`
4
- * sees them (ADR 0020, decision 6). `ctx.inspector` is `undefined` without
5
- * the grant.
6
- * @experimental
7
- */
8
- type IsolateInspector = {
9
- /**
10
- * Isolates, pools, the bus, and the last 200 events. Once the port has
11
- * ended it resolves with the last snapshot it got, if any.
12
- */
13
- snapshot(): Promise<InspectSnapshot>;
14
- /**
15
- * The host's feed messages from now on: events at once, beats and bus
16
- * metrics about once a second. Returns the function that stops them.
17
- */
18
- on(fn: (m: InspectMessage) => void): () => void;
19
- /** The last `last` retained records of `topic` (at most 100), oldest first, as previews. */
20
- records(topic: string, o: {
21
- last: number;
22
- }): Promise<InspectRecord[]>;
23
- /**
24
- * Records appended to matching topics, at most `maxPerSec` a second
25
- * (default 50), as previews. Not a consumer: it isn't in the bus figures.
26
- * `onSkipped` gets each count of records left out over the cap, at most
27
- * once a second (ADR 0020, decision 6, amended 2026-11-19). Returns the
28
- * function that ends it.
29
- */
30
- tap(o: {
31
- topics: string[];
32
- maxPerSec?: number;
33
- onSkipped?: (count: number) => void;
34
- }, fn: (r: InspectRecord) => void): () => void;
35
- /**
36
- * Restarts an isolate, as `kernel.restart(id)` does. Like `kill` and
37
- * `block`, it rejects for an unknown id, and once the port has ended.
38
- */
39
- restart(id: string): Promise<void>;
40
- /**
41
- * Ends an isolate's worker as a crash (`worker-error`), so the supervisor
42
- * restarts it. For a pooled isolate that ends the pool's worker: every
43
- * member crashes and restarts, and the feed shows each one. Resolves once
44
- * the kill is sent, not when the isolate is back.
45
- */
46
- kill(id: string): Promise<void>;
47
- /** Busy-loops an isolate's worker for `ms`, at most 10 s. */
48
- block(id: string, ms: number): Promise<void>;
49
- };
50
- type SubscribeOptions = {
51
- /**
52
- * Default `latest`. With `group`, it applies only while the group has no
53
- * committed offset: a group that has committed resumes after it, and a
54
- * member joining a group that has members takes records from where the
55
- * group is (ADR 0016, point 14).
56
- */
57
- from?: StartPosition;
58
- /** Share the topic with other subscribers of the same group: each record goes to one of them. */
59
- group?: string;
60
- /** Default true: commit each record once its handler settles. */
61
- autoCommit?: boolean;
62
- /**
63
- * A bus restart lost this memory topic (ADR 0007). Clear what was built
64
- * from it; records from the new bus follow.
65
- */
66
- onReset?: () => void;
67
- /**
68
- * Records in `[from, to]` were dropped by retention before they reached
69
- * this subscription (design §6.3), e.g. while the isolate was suspended.
70
- */
71
- onGap?: (gap: {
72
- from: number;
73
- to: number;
74
- }) => void;
75
- };
76
- interface BusClient {
77
- /** Resolves once the broker appended the record; rejects with a `BusError`. */
78
- publish<In>(topic: TopicRef<In, any, any, any>, rec: {
79
- key?: string;
80
- value: In;
81
- }): Promise<void>;
82
- /** Returns the unsubscribe function. */
83
- subscribe<Out>(topic: TopicRef<any, Out, any, any>, fn: (r: TypedRecord<Out>) => void | Promise<void>, opts?: SubscribeOptions): () => void;
84
- /**
85
- * Topics with a `reply` schema only. Rejects with `timeout` after
86
- * `timeoutMs` (default 30 s), or with `dropped` at once when the topic's
87
- * retention removed the request before the bus delivered it to any
88
- * subscriber.
89
- */
90
- request<In, ROut>(topic: TopicRef<In, any, any, ROut>, value: In, opts?: {
91
- key?: string;
92
- timeoutMs?: number;
93
- }): Promise<ROut>;
94
- /** Answers a request record delivered to this isolate; the broker checks `value` against the topic's `reply` schema. */
95
- reply(record: TypedRecord<unknown>, value: unknown): Promise<void>;
96
- /**
97
- * Commits a record by hand, for `autoCommit: false`: that record and every
98
- * one delivered before it on its subscription count as settled.
99
- */
100
- commit(record: TypedRecord<unknown>): void;
101
- }
102
- /** A host call that failed, with the protocol's error code. */
103
- declare class HostCallError extends Error {
104
- readonly code: ErrorCode;
105
- constructor(code: ErrorCode, message: string);
106
- }
107
- /** What `ctx.host` offers (design §4.7). */
108
- type HostApi = {
109
- /**
110
- * Asks the host to navigate (ADR 0002). Needs `grants.navigate`, else
111
- * `forbidden`. `/x` resolves against the host's `basePath`. Resolves with
112
- * the host's answer: `true` accepted, `false` refused.
113
- */
114
- navigate(href: string, o?: {
115
- replace?: boolean;
116
- state?: unknown;
117
- }): Promise<boolean>;
118
- /** The node's box relative to the surface, after pending patches apply. */
119
- measure(node: unknown): Promise<Rect>;
120
- focus(node: unknown): Promise<void>;
121
- scrollIntoView(node: unknown, opts?: ScrollIntoViewOptionsLike): Promise<void>;
122
- /**
123
- * Hands a rendered `<canvas>` to the isolate as an `OffscreenCanvas`
124
- * (design §4.7), at `size` in canvas pixels if given. Draw on it from the
125
- * worker. The element then gets `resize` events with its CSS `width` and
126
- * `height`, the first at once. Each canvas goes once; a second request
127
- * rejects with `invalid`, a headless isolate's with `no_surface`.
128
- */
129
- requestCanvas(node: unknown, size?: {
130
- width?: number;
131
- height?: number;
132
- }): Promise<OffscreenCanvas>;
133
- };
134
- type ScrollIntoViewOptionsLike = {
135
- behavior?: "auto" | "instant" | "smooth";
136
- block?: "start" | "center" | "end" | "nearest";
137
- inline?: "start" | "center" | "end" | "nearest";
138
- };
139
- /**
140
- * What a render root offers, structurally, so DOM-typed isolate code can pass
141
- * its own nodes. Use `defineIsolate<HTMLElement>(…)` to get the DOM's types.
142
- */
143
- interface IsolateRoot {
144
- append(...nodes: (object | string)[]): void;
145
- replaceChildren(...nodes: (object | string)[]): void;
146
- appendChild<T extends object>(node: T): T;
147
- insertBefore<T extends object>(node: T, ref: object | null): T;
148
- removeChild<T extends object>(node: T): T;
149
- readonly childNodes: ArrayLike<object>;
150
- textContent: string | null;
151
- addEventListener(type: string, fn: (event: never) => void, options?: unknown): void;
152
- }
153
- type IsolateContext<Root = IsolateRoot> = {
154
- readonly id: string;
155
- /** `manifest.config`, passed through. */
156
- readonly config: unknown;
157
- /** The render target; `undefined` for a headless isolate (ADR 0008). */
158
- readonly root: Root | undefined;
159
- /**
160
- * A container inside `root` for modals, tooltips, toasts and menus that
161
- * libraries would otherwise render into `document.body`, which reaches no
162
- * surface (ADR 0015, decision 11). Made on first use, typically from a
163
- * component while it renders, and added as `root`'s last child once that
164
- * render has committed; `undefined` for a headless isolate.
165
- */
166
- readonly portalRoot: Root | undefined;
167
- readonly host: HostApi;
168
- /** The log bus, on its own port to the bus worker (design §5). */
169
- readonly bus: BusClient;
170
- /**
171
- * The inspection feed and its controls (ADR 0020, decision 6), on a port of
172
- * its own to the host; `undefined` unless the isolate holds `inspect: true`.
173
- * @experimental
174
- */
175
- readonly inspector: IsolateInspector | undefined;
176
- /** Adds stylesheets to the surface (design §4.8); a no-op when headless. */
177
- styles(css: string | readonly string[]): void;
178
- /**
179
- * Sets an `<input>`'s, `<textarea>`'s or `<select>`'s value. The host owns
180
- * a focused input's value (design §4.6), so a plain `input.value = …` waits
181
- * until the user leaves it; with `force` the host applies it at once and
182
- * keeps the caret at the end, e.g. to clear a chat box after sending.
183
- */
184
- setValue(node: unknown, value: string, opts?: {
185
- force?: boolean;
186
- }): void;
187
- /**
188
- * Reports an error as this isolate's own: framework adapters call it for a
189
- * render error their boundary caught, so in a pool it crashes only this
190
- * member, not the whole worker (ADR 0015, decision 8). `fatal` (the
191
- * default) crashes the isolate, which the host restarts as usual.
192
- */
193
- reportError(error: unknown, opts?: {
194
- fatal?: boolean;
195
- }): void;
196
- /** Runs on stop, within the host's grace period, before the worker goes. */
197
- onShutdown(fn: (reason: ShutdownReason) => void | Promise<void>): void;
198
- /**
199
- * Holds the isolate against suspension (ADR 0001); call the result to
200
- * release. Only matters for isolates spawned with `suspend`.
201
- */
202
- keepAwake(): () => void;
203
- };
204
- type IsolateDefinition<Root = IsolateRoot> = {
205
- start(ctx: IsolateContext<Root>): void | Promise<void>;
206
- /**
207
- * The Isoloop `major.minor` it was built against, stamped by
208
- * `defineIsolate` (ADR 0012). The host fails an isolate outside its
209
- * `[FLOOR, VERSION]`; a definition without one isn't checked.
210
- */
211
- requires?: string;
212
- };
213
- /** This build's stamp: `major.minor` of the package version, inlined when tsdown bundles it. */
214
- declare const REQUIRES: string;
215
- /**
216
- * Types an isolate module's default export and stamps it with the version
217
- * it was built against. `Root` defaults to the structural `IsolateRoot`.
218
- */
219
- declare function defineIsolate<Root = IsolateRoot>(def: IsolateDefinition<Root>): IsolateDefinition<Root>;
220
- export { defineIsolate as a, ScrollIntoViewOptionsLike as c, IsolateInspector as d, REQUIRES as i, BusClient as l, IsolateDefinition as n, HostApi as o, IsolateRoot as r, HostCallError as s, IsolateContext as t, SubscribeOptions as u };