@isoloop/preact 0.1.0-next.9 → 0.1.1

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,39 @@
1
+ # @isoloop/preact
2
+
3
+ Preact for [Isoloop](https://isoloop.dev): write an isolate as a Preact component, rendered in its worker and drawn on the page.
4
+
5
+ ## What you get
6
+
7
+ - **Your components in a worker:** `defineIsolate(Component)` turns a Preact 10 or 11 component into an isolate.
8
+ - **Topics as state:** `useTopic` follows a topic's latest value, read again after a restart with `from: 'earliest'`, and `usePublish` writes to a topic.
9
+ - **The page through hooks:** `useHost` to measure, focus, scroll, navigate and get a canvas, and `useRoute` for the current location.
10
+ - **Portals:** `usePortalRoot` gives modals, tooltips and menus a place inside the isolate.
11
+ - **Errors reach the supervisor:** a render error restarts that isolate, and in a pool only that member.
12
+ - **Request and reply** (experimental): `useRequest`.
13
+
14
+ ## Install
15
+
16
+ ```sh
17
+ npm install @isoloop/preact @isoloop/runtime preact
18
+ ```
19
+
20
+ ## Example
21
+
22
+ ```tsx
23
+ import { defineIsolate, usePublish, useTopic } from '@isoloop/preact';
24
+ import { count } from './topics'; // a topic from your contracts
25
+
26
+ function Counter() {
27
+ const n = useTopic(count, { from: 'earliest' }) ?? 0;
28
+ const publish = usePublish(count);
29
+ return <button onClick={() => void publish(n + 1)}>Count: {n}</button>;
30
+ }
31
+
32
+ export default defineIsolate(Counter);
33
+ ```
34
+
35
+ ## Learn more
36
+
37
+ - [Preact isolates](https://isoloop.dev/docs/isolates/#preact) and [the hooks](https://isoloop.dev/docs/isolates/#the-hooks)
38
+ - [What Isoloop can't hide](https://isoloop.dev/docs/limits/)
39
+ - [API reference](https://isoloop.dev/docs/api/preact/)
package/dist/index.d.ts CHANGED
@@ -1,42 +1,121 @@
1
1
  import { BusError, HostApi, HostApi as HostApi$1, HostCallError, IsolateContext, IsolateContext as IsolateContext$1, IsolateDefinition, RequestOptions, RequestOptions as RequestOptions$1 } from "@isoloop/runtime";
2
2
  import { ComponentType } from "preact";
3
3
  import { RouteValue, RouteValue as RouteValue$1, StartPosition, TopicRef } from "@isoloop/protocol";
4
- /** Props every isolate component gets: `manifest.config`, passed through. */
4
+ /** The props `defineIsolate` passes to the isolate's component. */
5
5
  type IsolateProps = {
6
+ /** The `config` the isolate was spawned with (`manifest.config`), passed through unchanged; `undefined` when it has none. */
6
7
  config: unknown;
7
8
  };
9
+ /**
10
+ * Makes `Component` an isolate: the module's default export, which renders it into the
11
+ * isolate's surface (with Preact) and unmounts it on shutdown, so effects clean up and
12
+ * subscriptions end. Every hook in this package works inside it.
13
+ *
14
+ * A render error is caught at the root and reported as this isolate's own crash, so in
15
+ * a pool it fails only this member. The isolate needs a surface: started headless, it
16
+ * throws an `Error` at start.
17
+ *
18
+ * @param Component - The root component. It gets {@link IsolateProps}.
19
+ * @returns The isolate definition, stamped with the runtime version it was built against.
20
+ * @example
21
+ * ```tsx
22
+ * import { defineIsolate, usePublish, useTopic } from '@isoloop/preact';
23
+ * import { count } from './topics';
24
+ *
25
+ * function Counter() {
26
+ * const n = useTopic(count, { from: 'earliest' }) ?? 0;
27
+ * const publish = usePublish(count);
28
+ * return <button onClick={() => void publish(n + 1)}>Count: {n}</button>;
29
+ * }
30
+ *
31
+ * export default defineIsolate(Counter);
32
+ * ```
33
+ */
8
34
  export declare function defineIsolate(Component: ComponentType<IsolateProps>): IsolateDefinition;
9
- /** The isolate context. Throws outside a component that `defineIsolate` rendered. */
35
+ /**
36
+ * The isolate's context, as `start(ctx)` gets it: `ctx.bus`, `ctx.host`, `ctx.config` and
37
+ * the rest.
38
+ *
39
+ * @returns The context of the isolate this component runs in.
40
+ * @throws `Error` outside a component that `defineIsolate` rendered.
41
+ */
10
42
  export declare function useIsolate(): IsolateContext$1;
11
43
  /**
12
- * `ctx.portalRoot`: the container inside this isolate's surface for modals,
13
- * tooltips, toasts and menus (ADR 0015), for `createPortal` or a library's
14
- * container option. Throws in a headless isolate.
44
+ * The container inside this isolate's surface for modals, tooltips, toasts and menus
45
+ * (`ctx.portalRoot`), for `createPortal` or a library's container option. Content
46
+ * portalled there stays inside the isolate's surface.
47
+ *
48
+ * @returns The portal container.
49
+ * @throws `Error` in a headless isolate, or outside a component that `defineIsolate` rendered.
15
50
  */
16
51
  export declare function usePortalRoot(): Element;
52
+ /** Options for {@link useTopic}. */
17
53
  type UseTopicOptions = {
18
- /** Default `latest`. `earliest` replays the topic, so a compacted one gives its current value at once. */
54
+ /** Where reading starts. Default `'latest'`: only records appended from now. `'earliest'` replays the topic, so a compacted one gives its current value at once. */
19
55
  from?: StartPosition;
20
56
  };
21
57
  /**
22
- * The latest value on `topic`, or `undefined` until one arrives. A bus
23
- * restart that lost the memory topic (`onReset`, ADR 0007) clears it.
24
- * Subscribes on mount and unsubscribes on unmount; changing `topic` or
25
- * `from` resubscribes.
58
+ * The latest value on `topic`. It subscribes on mount and unsubscribes on unmount;
59
+ * a different `topic` or `from` subscribes again.
60
+ *
61
+ * An isolate has one subscription per topic. A second `useTopic` on the same topic, in
62
+ * another component or another call in the same one, replaces the first with no error: only
63
+ * the last one subscribed gets records, and the first stops updating. When the last one
64
+ * unmounts, the topic has no subscription, and the first doesn't get it back. Call
65
+ * `useTopic` once per topic, and pass the value down as props or share it through context.
66
+ * With the kernel's `dev: true`, the bus warns in the console when this happens.
67
+ *
68
+ * @param topic - The topic to read. The isolate needs a subscribe grant for it.
69
+ * @param opts - Where reading starts ({@link UseTopicOptions}).
70
+ * @returns The latest value, or `undefined` until one arrives. A bus restart that lost a
71
+ * memory topic clears it back to `undefined`.
72
+ * @throws `Error` outside a component that `defineIsolate` rendered.
26
73
  */
27
74
  export declare function useTopic<Out>(topic: TopicRef<any, Out, any, any>, opts?: UseTopicOptions): Out | undefined;
28
- /** A function that publishes to `topic`; it resolves once the broker appended the record (`BusClient.publish`). */
75
+ /**
76
+ * A function that publishes to `topic`. The function is stable while `topic` is the same.
77
+ *
78
+ * @param topic - The topic to publish to. The isolate needs a publish grant for it.
79
+ * @returns `(value, opts?) => Promise<void>`, with `opts.key` for the record's key. The
80
+ * promise resolves once the bus has appended the record, and rejects with a `BusError`:
81
+ * `forbidden` without a publish grant, `invalid` when the schema rejects the value
82
+ * (`issues` says why), `too_large` over the topic's `maxRecordBytes`, `not_encodable`
83
+ * for a value the bus can't store, `quota_exceeded` when storage is full,
84
+ * `unknown_topic`, or `bus_unavailable`.
85
+ * @throws `Error` outside a component that `defineIsolate` rendered.
86
+ */
29
87
  export declare function usePublish<In>(topic: TopicRef<In, any, any, any>): (value: In, opts?: {
30
88
  key?: string;
31
89
  }) => Promise<void>;
32
90
  /**
33
- * A function that sends a request on `topic` and resolves with the reply.
34
- * It rejects with a `BusError`, `timeout` after `timeoutMs` (default 30 s).
91
+ * A function that sends a request on `topic` and resolves with the first valid reply.
92
+ * The function is stable while `topic` is the same.
93
+ *
94
+ * @param topic - A topic with a `reply` schema.
95
+ * @returns `(value, opts?) => Promise<reply>`. `opts` takes `key` and `timeoutMs`
96
+ * (default 30 s). The promise rejects with a `BusError`: `timeout` when no reply came
97
+ * in time, `dropped` when retention removed the request before any subscriber got it,
98
+ * `reply_invalid` when the reply failed the `reply` schema, `not_requestable` on a
99
+ * topic without one, and the publish codes (`forbidden`, `invalid`, `too_large`,
100
+ * `unknown_topic`, `bus_unavailable`) for the request itself.
35
101
  * @experimental
36
102
  */
37
103
  export declare function useRequest<In, ROut>(topic: TopicRef<In, any, any, ROut>): (value: In, opts?: RequestOptions$1) => Promise<ROut>;
38
- /** `measure`, `focus`, `scrollIntoView` and `navigate`, as `ctx.host` has them. */
104
+ /**
105
+ * The isolate's host calls (`ctx.host`): `measure`, `focus`, `scrollIntoView`, `navigate`
106
+ * and `requestCanvas`. Pass them the elements a ref holds; the host acts on what it
107
+ * rendered for them, after pending changes are applied.
108
+ *
109
+ * @returns The host API.
110
+ * @throws `Error` outside a component that `defineIsolate` rendered.
111
+ */
39
112
  export declare function useHost(): HostApi$1;
40
- /** The current route, or `undefined` until the first record arrives (or with routing off). */
113
+ /**
114
+ * The host's current location. The route is read only while a component uses this hook.
115
+ *
116
+ * @returns The current route, or `undefined` until the first one arrives, and always
117
+ * with routing off.
118
+ * @throws `Error` outside a component that `defineIsolate` rendered.
119
+ */
41
120
  export declare function useRoute(): RouteValue$1 | undefined;
42
121
  export { BusError, type HostApi, HostCallError, type IsolateContext, type IsolateProps, type RequestOptions, type RouteValue, type UseTopicOptions };
package/dist/index.js CHANGED
@@ -2,25 +2,34 @@ import { BusError, HostCallError, REQUIRES } from "@isoloop/runtime";
2
2
  import { Component, createContext, h, options, render } from "preact";
3
3
  import { useCallback, useContext, useLayoutEffect, useState } from "preact/hooks";
4
4
  import { routeTopic } from "@isoloop/protocol";
5
- // src/context.ts
5
+ // src/isolate/context.ts
6
6
  const IsolateCtx = createContext(null);
7
- /** The isolate context. Throws outside a component that `defineIsolate` rendered. */
7
+ /**
8
+ * The isolate's context, as `start(ctx)` gets it: `ctx.bus`, `ctx.host`, `ctx.config` and
9
+ * the rest.
10
+ *
11
+ * @returns The context of the isolate this component runs in.
12
+ * @throws `Error` outside a component that `defineIsolate` rendered.
13
+ */
8
14
  function useIsolate() {
9
15
  const ctx = useContext(IsolateCtx);
10
16
  if (!ctx) throw new Error("useIsolate: not inside a component rendered by defineIsolate()");
11
17
  return ctx;
12
18
  }
13
19
  /**
14
- * `ctx.portalRoot`: the container inside this isolate's surface for modals,
15
- * tooltips, toasts and menus (ADR 0015), for `createPortal` or a library's
16
- * container option. Throws in a headless isolate.
20
+ * The container inside this isolate's surface for modals, tooltips, toasts and menus
21
+ * (`ctx.portalRoot`), for `createPortal` or a library's container option. Content
22
+ * portalled there stays inside the isolate's surface.
23
+ *
24
+ * @returns The portal container.
25
+ * @throws `Error` in a headless isolate, or outside a component that `defineIsolate` rendered.
17
26
  */
18
27
  function usePortalRoot() {
19
28
  const portal = useIsolate().portalRoot;
20
29
  if (!(portal instanceof Element)) throw new Error("usePortalRoot: this isolate has no surface");
21
30
  return portal;
22
31
  }
23
- // src/define.ts
32
+ // src/isolate/define.ts
24
33
  /**
25
34
  * Catches a render error at the isolate's root and reports it as this
26
35
  * isolate's own (`ctx.reportError`), so in a pool it crashes only this member
@@ -76,6 +85,31 @@ function bindBatches(ctx) {
76
85
  };
77
86
  Reflect.set(options, BOUND, true);
78
87
  }
88
+ /**
89
+ * Makes `Component` an isolate: the module's default export, which renders it into the
90
+ * isolate's surface (with Preact) and unmounts it on shutdown, so effects clean up and
91
+ * subscriptions end. Every hook in this package works inside it.
92
+ *
93
+ * A render error is caught at the root and reported as this isolate's own crash, so in
94
+ * a pool it fails only this member. The isolate needs a surface: started headless, it
95
+ * throws an `Error` at start.
96
+ *
97
+ * @param Component - The root component. It gets {@link IsolateProps}.
98
+ * @returns The isolate definition, stamped with the runtime version it was built against.
99
+ * @example
100
+ * ```tsx
101
+ * import { defineIsolate, usePublish, useTopic } from '@isoloop/preact';
102
+ * import { count } from './topics';
103
+ *
104
+ * function Counter() {
105
+ * const n = useTopic(count, { from: 'earliest' }) ?? 0;
106
+ * const publish = usePublish(count);
107
+ * return <button onClick={() => void publish(n + 1)}>Count: {n}</button>;
108
+ * }
109
+ *
110
+ * export default defineIsolate(Counter);
111
+ * ```
112
+ */
79
113
  function defineIsolate(Component) {
80
114
  return {
81
115
  requires: REQUIRES,
@@ -92,10 +126,21 @@ function defineIsolate(Component) {
92
126
  }
93
127
  // src/hooks/topic.ts
94
128
  /**
95
- * The latest value on `topic`, or `undefined` until one arrives. A bus
96
- * restart that lost the memory topic (`onReset`, ADR 0007) clears it.
97
- * Subscribes on mount and unsubscribes on unmount; changing `topic` or
98
- * `from` resubscribes.
129
+ * The latest value on `topic`. It subscribes on mount and unsubscribes on unmount;
130
+ * a different `topic` or `from` subscribes again.
131
+ *
132
+ * An isolate has one subscription per topic. A second `useTopic` on the same topic, in
133
+ * another component or another call in the same one, replaces the first with no error: only
134
+ * the last one subscribed gets records, and the first stops updating. When the last one
135
+ * unmounts, the topic has no subscription, and the first doesn't get it back. Call
136
+ * `useTopic` once per topic, and pass the value down as props or share it through context.
137
+ * With the kernel's `dev: true`, the bus warns in the console when this happens.
138
+ *
139
+ * @param topic - The topic to read. The isolate needs a subscribe grant for it.
140
+ * @param opts - Where reading starts ({@link UseTopicOptions}).
141
+ * @returns The latest value, or `undefined` until one arrives. A bus restart that lost a
142
+ * memory topic clears it back to `undefined`.
143
+ * @throws `Error` outside a component that `defineIsolate` rendered.
99
144
  */
100
145
  function useTopic(topic, opts = {}) {
101
146
  const { bus } = useIsolate();
@@ -114,7 +159,18 @@ function useTopic(topic, opts = {}) {
114
159
  ]);
115
160
  return value?.v;
116
161
  }
117
- /** A function that publishes to `topic`; it resolves once the broker appended the record (`BusClient.publish`). */
162
+ /**
163
+ * A function that publishes to `topic`. The function is stable while `topic` is the same.
164
+ *
165
+ * @param topic - The topic to publish to. The isolate needs a publish grant for it.
166
+ * @returns `(value, opts?) => Promise<void>`, with `opts.key` for the record's key. The
167
+ * promise resolves once the bus has appended the record, and rejects with a `BusError`:
168
+ * `forbidden` without a publish grant, `invalid` when the schema rejects the value
169
+ * (`issues` says why), `too_large` over the topic's `maxRecordBytes`, `not_encodable`
170
+ * for a value the bus can't store, `quota_exceeded` when storage is full,
171
+ * `unknown_topic`, or `bus_unavailable`.
172
+ * @throws `Error` outside a component that `defineIsolate` rendered.
173
+ */
118
174
  function usePublish(topic) {
119
175
  const { bus } = useIsolate();
120
176
  return useCallback((value, o = {}) => bus.publish(topic, o.key === void 0 ? { value } : {
@@ -124,8 +180,16 @@ function usePublish(topic) {
124
180
  }
125
181
  // src/hooks/request.ts
126
182
  /**
127
- * A function that sends a request on `topic` and resolves with the reply.
128
- * It rejects with a `BusError`, `timeout` after `timeoutMs` (default 30 s).
183
+ * A function that sends a request on `topic` and resolves with the first valid reply.
184
+ * The function is stable while `topic` is the same.
185
+ *
186
+ * @param topic - A topic with a `reply` schema.
187
+ * @returns `(value, opts?) => Promise<reply>`. `opts` takes `key` and `timeoutMs`
188
+ * (default 30 s). The promise rejects with a `BusError`: `timeout` when no reply came
189
+ * in time, `dropped` when retention removed the request before any subscriber got it,
190
+ * `reply_invalid` when the reply failed the `reply` schema, `not_requestable` on a
191
+ * topic without one, and the publish codes (`forbidden`, `invalid`, `too_large`,
192
+ * `unknown_topic`, `bus_unavailable`) for the request itself.
129
193
  * @experimental
130
194
  */
131
195
  function useRequest(topic) {
@@ -133,12 +197,25 @@ function useRequest(topic) {
133
197
  return useCallback((value, o) => bus.request(topic, value, o), [bus, topic.name]);
134
198
  }
135
199
  // src/hooks/host.ts
136
- /** `measure`, `focus`, `scrollIntoView` and `navigate`, as `ctx.host` has them. */
200
+ /**
201
+ * The isolate's host calls (`ctx.host`): `measure`, `focus`, `scrollIntoView`, `navigate`
202
+ * and `requestCanvas`. Pass them the elements a ref holds; the host acts on what it
203
+ * rendered for them, after pending changes are applied.
204
+ *
205
+ * @returns The host API.
206
+ * @throws `Error` outside a component that `defineIsolate` rendered.
207
+ */
137
208
  function useHost() {
138
209
  return useIsolate().host;
139
210
  }
140
211
  // src/hooks/route.ts
141
- /** The current route, or `undefined` until the first record arrives (or with routing off). */
212
+ /**
213
+ * The host's current location. The route is read only while a component uses this hook.
214
+ *
215
+ * @returns The current route, or `undefined` until the first one arrives, and always
216
+ * with routing off.
217
+ * @throws `Error` outside a component that `defineIsolate` rendered.
218
+ */
142
219
  function useRoute() {
143
220
  return useTopic(routeTopic, { from: "earliest" });
144
221
  }
package/package.json CHANGED
@@ -1,7 +1,16 @@
1
1
  {
2
2
  "name": "@isoloop/preact",
3
- "version": "0.1.0-next.9",
4
- "description": "Preact adapter for Isoloop isolates: defineIsolate(Component) and bus and host hooks.",
3
+ "version": "0.1.1",
4
+ "description": "Preact for Isoloop: run a component as an isolate in a worker, with hooks for the message bus and the host.",
5
+ "keywords": [
6
+ "isoloop",
7
+ "web-workers",
8
+ "event-loop",
9
+ "isolation",
10
+ "preact",
11
+ "microfrontends"
12
+ ],
13
+ "homepage": "https://isoloop.dev/docs/api/preact/",
5
14
  "license": "MIT",
6
15
  "type": "module",
7
16
  "sideEffects": false,
@@ -23,11 +32,11 @@
23
32
  "preact": "^10 || ^11"
24
33
  },
25
34
  "dependencies": {
26
- "@isoloop/protocol": "0.1.0-next.9",
27
- "@isoloop/runtime": "0.1.0-next.9"
35
+ "@isoloop/protocol": "0.1.1",
36
+ "@isoloop/runtime": "0.1.1"
28
37
  },
29
38
  "devDependencies": {
30
- "@isoloop/dom": "0.1.0-next.9",
39
+ "@isoloop/dom": "0.1.1",
31
40
  "preact10": "npm:preact@10.29.8",
32
41
  "preact11": "npm:preact@11.1.1",
33
42
  "typescript": "7.0.2"