@isoloop/preact 0.1.0-next.0 → 0.1.0-next.10

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/index.d.ts CHANGED
@@ -1,45 +1,114 @@
1
- import { BusError, HostApi, HostApi as HostApi$1, HostCallError, IsolateContext, IsolateContext as IsolateContext$1, IsolateDefinition } from "@isoloop/runtime";
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
+ * @param topic - The topic to read. The isolate needs a subscribe grant for it.
62
+ * @param opts - Where reading starts ({@link UseTopicOptions}).
63
+ * @returns The latest value, or `undefined` until one arrives. A bus restart that lost a
64
+ * memory topic clears it back to `undefined`.
65
+ * @throws `Error` outside a component that `defineIsolate` rendered.
26
66
  */
27
67
  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`). */
68
+ /**
69
+ * A function that publishes to `topic`. The function is stable while `topic` is the same.
70
+ *
71
+ * @param topic - The topic to publish to. The isolate needs a publish grant for it.
72
+ * @returns `(value, opts?) => Promise<void>`, with `opts.key` for the record's key. The
73
+ * promise resolves once the bus has appended the record, and rejects with a `BusError`:
74
+ * `forbidden` without a publish grant, `invalid` when the schema rejects the value
75
+ * (`issues` says why), `too_large` over the topic's `maxRecordBytes`, `not_encodable`
76
+ * for a value the bus can't store, `quota_exceeded` when storage is full,
77
+ * `unknown_topic`, or `bus_unavailable`.
78
+ * @throws `Error` outside a component that `defineIsolate` rendered.
79
+ */
29
80
  export declare function usePublish<In>(topic: TopicRef<In, any, any, any>): (value: In, opts?: {
30
81
  key?: string;
31
82
  }) => Promise<void>;
32
- type RequestOptions = {
33
- key?: string;
34
- timeoutMs?: number;
35
- };
36
83
  /**
37
- * A function that sends a request on `topic` and resolves with the reply.
38
- * It rejects with a `BusError`, `timeout` after `timeoutMs` (default 30 s).
84
+ * A function that sends a request on `topic` and resolves with the first valid reply.
85
+ * The function is stable while `topic` is the same.
86
+ *
87
+ * @param topic - A topic with a `reply` schema.
88
+ * @returns `(value, opts?) => Promise<reply>`. `opts` takes `key` and `timeoutMs`
89
+ * (default 30 s). The promise rejects with a `BusError`: `timeout` when no reply came
90
+ * in time, `dropped` when retention removed the request before any subscriber got it,
91
+ * `reply_invalid` when the reply failed the `reply` schema, `not_requestable` on a
92
+ * topic without one, and the publish codes (`forbidden`, `invalid`, `too_large`,
93
+ * `unknown_topic`, `bus_unavailable`) for the request itself.
94
+ * @experimental
95
+ */
96
+ export declare function useRequest<In, ROut>(topic: TopicRef<In, any, any, ROut>): (value: In, opts?: RequestOptions$1) => Promise<ROut>;
97
+ /**
98
+ * The isolate's host calls (`ctx.host`): `measure`, `focus`, `scrollIntoView` and
99
+ * `navigate`. Pass them the elements a ref holds; the host acts on what it rendered for
100
+ * them, after pending changes are applied.
101
+ *
102
+ * @returns The host API.
103
+ * @throws `Error` outside a component that `defineIsolate` rendered.
39
104
  */
40
- export declare function useRequest<In, ROut>(topic: TopicRef<In, any, any, ROut>): (value: In, opts?: RequestOptions) => Promise<ROut>;
41
- /** `measure`, `focus`, `scrollIntoView` and `navigate`, as `ctx.host` has them. */
42
105
  export declare function useHost(): HostApi$1;
43
- /** The current route, or `undefined` until the first record arrives (or with routing off). */
106
+ /**
107
+ * The host's current location. The route is read only while a component uses this hook.
108
+ *
109
+ * @returns The current route, or `undefined` until the first one arrives, and always
110
+ * with routing off.
111
+ * @throws `Error` outside a component that `defineIsolate` rendered.
112
+ */
44
113
  export declare function useRoute(): RouteValue$1 | undefined;
45
114
  export { BusError, type HostApi, HostCallError, type IsolateContext, type IsolateProps, type RequestOptions, type RouteValue, type UseTopicOptions };
package/dist/index.js CHANGED
@@ -4,16 +4,25 @@ import { useCallback, useContext, useLayoutEffect, useState } from "preact/hooks
4
4
  import { routeTopic } from "@isoloop/protocol";
5
5
  // src/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;
@@ -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,14 @@ 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
+ * @param topic - The topic to read. The isolate needs a subscribe grant for it.
133
+ * @param opts - Where reading starts ({@link UseTopicOptions}).
134
+ * @returns The latest value, or `undefined` until one arrives. A bus restart that lost a
135
+ * memory topic clears it back to `undefined`.
136
+ * @throws `Error` outside a component that `defineIsolate` rendered.
99
137
  */
100
138
  function useTopic(topic, opts = {}) {
101
139
  const { bus } = useIsolate();
@@ -114,7 +152,18 @@ function useTopic(topic, opts = {}) {
114
152
  ]);
115
153
  return value?.v;
116
154
  }
117
- /** A function that publishes to `topic`; it resolves once the broker appended the record (`BusClient.publish`). */
155
+ /**
156
+ * A function that publishes to `topic`. The function is stable while `topic` is the same.
157
+ *
158
+ * @param topic - The topic to publish to. The isolate needs a publish grant for it.
159
+ * @returns `(value, opts?) => Promise<void>`, with `opts.key` for the record's key. The
160
+ * promise resolves once the bus has appended the record, and rejects with a `BusError`:
161
+ * `forbidden` without a publish grant, `invalid` when the schema rejects the value
162
+ * (`issues` says why), `too_large` over the topic's `maxRecordBytes`, `not_encodable`
163
+ * for a value the bus can't store, `quota_exceeded` when storage is full,
164
+ * `unknown_topic`, or `bus_unavailable`.
165
+ * @throws `Error` outside a component that `defineIsolate` rendered.
166
+ */
118
167
  function usePublish(topic) {
119
168
  const { bus } = useIsolate();
120
169
  return useCallback((value, o = {}) => bus.publish(topic, o.key === void 0 ? { value } : {
@@ -124,20 +173,42 @@ function usePublish(topic) {
124
173
  }
125
174
  // src/hooks/request.ts
126
175
  /**
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).
176
+ * A function that sends a request on `topic` and resolves with the first valid reply.
177
+ * The function is stable while `topic` is the same.
178
+ *
179
+ * @param topic - A topic with a `reply` schema.
180
+ * @returns `(value, opts?) => Promise<reply>`. `opts` takes `key` and `timeoutMs`
181
+ * (default 30 s). The promise rejects with a `BusError`: `timeout` when no reply came
182
+ * in time, `dropped` when retention removed the request before any subscriber got it,
183
+ * `reply_invalid` when the reply failed the `reply` schema, `not_requestable` on a
184
+ * topic without one, and the publish codes (`forbidden`, `invalid`, `too_large`,
185
+ * `unknown_topic`, `bus_unavailable`) for the request itself.
186
+ * @experimental
129
187
  */
130
188
  function useRequest(topic) {
131
189
  const { bus } = useIsolate();
132
190
  return useCallback((value, o) => bus.request(topic, value, o), [bus, topic.name]);
133
191
  }
134
192
  // src/hooks/host.ts
135
- /** `measure`, `focus`, `scrollIntoView` and `navigate`, as `ctx.host` has them. */
193
+ /**
194
+ * The isolate's host calls (`ctx.host`): `measure`, `focus`, `scrollIntoView` and
195
+ * `navigate`. Pass them the elements a ref holds; the host acts on what it rendered for
196
+ * them, after pending changes are applied.
197
+ *
198
+ * @returns The host API.
199
+ * @throws `Error` outside a component that `defineIsolate` rendered.
200
+ */
136
201
  function useHost() {
137
202
  return useIsolate().host;
138
203
  }
139
204
  // src/hooks/route.ts
140
- /** The current route, or `undefined` until the first record arrives (or with routing off). */
205
+ /**
206
+ * The host's current location. The route is read only while a component uses this hook.
207
+ *
208
+ * @returns The current route, or `undefined` until the first one arrives, and always
209
+ * with routing off.
210
+ * @throws `Error` outside a component that `defineIsolate` rendered.
211
+ */
141
212
  function useRoute() {
142
213
  return useTopic(routeTopic, { from: "earliest" });
143
214
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@isoloop/preact",
3
- "version": "0.1.0-next.0",
3
+ "version": "0.1.0-next.10",
4
4
  "description": "Preact adapter for Isoloop isolates: defineIsolate(Component) and bus and host hooks.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -23,11 +23,11 @@
23
23
  "preact": "^10 || ^11"
24
24
  },
25
25
  "dependencies": {
26
- "@isoloop/protocol": "0.1.0-next.0",
27
- "@isoloop/runtime": "0.1.0-next.0"
26
+ "@isoloop/protocol": "0.1.0-next.10",
27
+ "@isoloop/runtime": "0.1.0-next.10"
28
28
  },
29
29
  "devDependencies": {
30
- "@isoloop/dom": "0.1.0-next.0",
30
+ "@isoloop/dom": "0.1.0-next.10",
31
31
  "preact10": "npm:preact@10.29.8",
32
32
  "preact11": "npm:preact@11.1.1",
33
33
  "typescript": "7.0.2"