@isoloop/runtime 0.1.0-next.1 → 0.1.0-next.11
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/dist/bootstrap.js +382 -46
- package/dist/index.d.ts +2 -2
- package/dist/index.js +31 -6
- package/dist/testing.d.ts +19 -6
- package/dist/testing.js +9 -0
- package/dist/vanilla-C6wHItsG.d.ts +359 -0
- package/dist/worker.js +383 -47
- package/package.json +3 -3
- package/dist/vanilla-BO8hc_41.d.ts +0 -161
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { a as defineIsolate, c as ScrollIntoViewOptionsLike, i as REQUIRES, l as BusClient, n as IsolateDefinition, o as HostApi, r as IsolateRoot, s as HostCallError, t as IsolateContext, u as
|
|
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 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
|
|
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
|
-
/**
|
|
15
|
-
|
|
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-next.11") ?? "0.0";
|
|
16
25
|
/**
|
|
17
26
|
* Types an isolate module's default export and stamps it with the version
|
|
18
|
-
* it was built against.
|
|
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-
|
|
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,
|
|
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
|
|
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
|
|
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
|
|
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 = [];
|
|
@@ -72,6 +80,7 @@ function createTestContext(o = {}) {
|
|
|
72
80
|
},
|
|
73
81
|
host,
|
|
74
82
|
bus,
|
|
83
|
+
inspector: void 0,
|
|
75
84
|
styles: () => {},
|
|
76
85
|
setValue: () => {},
|
|
77
86
|
onShutdown: (fn) => {
|
|
@@ -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 };
|