@isoloop/preact 0.0.0 → 0.1.0-next.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Sabin Pandelovitch
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,45 @@
1
+ import { BusError, HostApi, HostApi as HostApi$1, HostCallError, IsolateContext, IsolateContext as IsolateContext$1, IsolateDefinition } from "@isoloop/runtime";
2
+ import { ComponentType } from "preact";
3
+ import { RouteValue, RouteValue as RouteValue$1, StartPosition, TopicRef } from "@isoloop/protocol";
4
+ /** Props every isolate component gets: `manifest.config`, passed through. */
5
+ type IsolateProps = {
6
+ config: unknown;
7
+ };
8
+ export declare function defineIsolate(Component: ComponentType<IsolateProps>): IsolateDefinition;
9
+ /** The isolate context. Throws outside a component that `defineIsolate` rendered. */
10
+ export declare function useIsolate(): IsolateContext$1;
11
+ /**
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.
15
+ */
16
+ export declare function usePortalRoot(): Element;
17
+ type UseTopicOptions = {
18
+ /** Default `latest`. `earliest` replays the topic, so a compacted one gives its current value at once. */
19
+ from?: StartPosition;
20
+ };
21
+ /**
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.
26
+ */
27
+ 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`). */
29
+ export declare function usePublish<In>(topic: TopicRef<In, any, any, any>): (value: In, opts?: {
30
+ key?: string;
31
+ }) => Promise<void>;
32
+ type RequestOptions = {
33
+ key?: string;
34
+ timeoutMs?: number;
35
+ };
36
+ /**
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).
39
+ */
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
+ export declare function useHost(): HostApi$1;
43
+ /** The current route, or `undefined` until the first record arrives (or with routing off). */
44
+ export declare function useRoute(): RouteValue$1 | undefined;
45
+ export { BusError, type HostApi, HostCallError, type IsolateContext, type IsolateProps, type RequestOptions, type RouteValue, type UseTopicOptions };
package/dist/index.js ADDED
@@ -0,0 +1,144 @@
1
+ import { BusError, HostCallError, REQUIRES } from "@isoloop/runtime";
2
+ import { Component, createContext, h, options, render } from "preact";
3
+ import { useCallback, useContext, useLayoutEffect, useState } from "preact/hooks";
4
+ import { routeTopic } from "@isoloop/protocol";
5
+ // src/context.ts
6
+ const IsolateCtx = createContext(null);
7
+ /** The isolate context. Throws outside a component that `defineIsolate` rendered. */
8
+ function useIsolate() {
9
+ const ctx = useContext(IsolateCtx);
10
+ if (!ctx) throw new Error("useIsolate: not inside a component rendered by defineIsolate()");
11
+ return ctx;
12
+ }
13
+ /**
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.
17
+ */
18
+ function usePortalRoot() {
19
+ const portal = useIsolate().portalRoot;
20
+ if (!(portal instanceof Element)) throw new Error("usePortalRoot: this isolate has no surface");
21
+ return portal;
22
+ }
23
+ // src/define.ts
24
+ /**
25
+ * Catches a render error at the isolate's root and reports it as this
26
+ * isolate's own (`ctx.reportError`), so in a pool it crashes only this member
27
+ * (ADR 0015, decision 8). Under a runtime without `reportError` (ADR 0012) the
28
+ * error is rethrown, as before.
29
+ */
30
+ var Boundary = class extends Component {
31
+ state = { failed: false };
32
+ static getDerivedStateFromError() {
33
+ return { failed: true };
34
+ }
35
+ componentDidCatch(error) {
36
+ const { ctx } = this.props;
37
+ if (typeof ctx.reportError === "function") ctx.reportError(error);
38
+ else queueMicrotask(() => {
39
+ throw error;
40
+ });
41
+ }
42
+ render() {
43
+ return this.state.failed ? null : this.props.children;
44
+ }
45
+ };
46
+ /** The runtime's internal hook on a context (ADR 0017), by its `Symbol.for` key. */
47
+ const BUSY = Symbol.for("isoloop.busy");
48
+ /** Set on `options` once its render batches are bound, by any copy of this adapter. */
49
+ const BOUND = Symbol.for("isoloop.preact.batches");
50
+ /** Preact 10's default: a resolved promise's callback. */
51
+ const defer = (cb) => {
52
+ Promise.resolve().then(cb);
53
+ };
54
+ /**
55
+ * Preact renders a batch in a promise callback, which the runtime can't see.
56
+ * Through `options.debounceRendering` (public), each batch runs as the code
57
+ * that scheduled it, so the members whose roots it changes share its time
58
+ * (ADR 0017). The batch is still scheduled as Preact schedules it by default,
59
+ * or by a `debounceRendering` already set.
60
+ */
61
+ function bindBatches(ctx) {
62
+ if (Reflect.get(options, BOUND) === true) return;
63
+ const hook = Reflect.get(ctx, BUSY);
64
+ const bind = typeof hook === "object" && hook !== null ? Reflect.get(hook, "bind") : void 0;
65
+ if (typeof bind !== "function") return;
66
+ const prev = Reflect.get(options, "debounceRendering");
67
+ const schedule = (cb) => {
68
+ if (typeof prev === "function") Reflect.apply(prev, void 0, [cb]);
69
+ else defer(cb);
70
+ };
71
+ options.debounceRendering = (cb) => {
72
+ const bound = Reflect.apply(bind, hook, [cb]);
73
+ schedule(typeof bound === "function" ? () => {
74
+ Reflect.apply(bound, void 0, []);
75
+ } : cb);
76
+ };
77
+ Reflect.set(options, BOUND, true);
78
+ }
79
+ function defineIsolate(Component) {
80
+ return {
81
+ requires: REQUIRES,
82
+ start(ctx) {
83
+ const container = ctx.root;
84
+ if (!(container instanceof Element)) throw new Error(`defineIsolate (Preact): "${ctx.id}" is headless; Preact needs a surface`);
85
+ bindBatches(ctx);
86
+ render(h(IsolateCtx.Provider, { value: ctx }, h(Boundary, { ctx }, h(Component, { config: ctx.config }))), container);
87
+ ctx.onShutdown(() => {
88
+ render(null, container);
89
+ });
90
+ }
91
+ };
92
+ }
93
+ // src/hooks/topic.ts
94
+ /**
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.
99
+ */
100
+ function useTopic(topic, opts = {}) {
101
+ const { bus } = useIsolate();
102
+ const [value, setValue] = useState(void 0);
103
+ const from = opts.from ?? "latest";
104
+ useLayoutEffect(() => {
105
+ setValue(void 0);
106
+ return bus.subscribe(topic, (r) => setValue({ v: r.value }), {
107
+ from,
108
+ onReset: () => setValue(void 0)
109
+ });
110
+ }, [
111
+ bus,
112
+ topic.name,
113
+ from
114
+ ]);
115
+ return value?.v;
116
+ }
117
+ /** A function that publishes to `topic`; it resolves once the broker appended the record (`BusClient.publish`). */
118
+ function usePublish(topic) {
119
+ const { bus } = useIsolate();
120
+ return useCallback((value, o = {}) => bus.publish(topic, o.key === void 0 ? { value } : {
121
+ key: o.key,
122
+ value
123
+ }), [bus, topic.name]);
124
+ }
125
+ // src/hooks/request.ts
126
+ /**
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).
129
+ */
130
+ function useRequest(topic) {
131
+ const { bus } = useIsolate();
132
+ return useCallback((value, o) => bus.request(topic, value, o), [bus, topic.name]);
133
+ }
134
+ // src/hooks/host.ts
135
+ /** `measure`, `focus`, `scrollIntoView` and `navigate`, as `ctx.host` has them. */
136
+ function useHost() {
137
+ return useIsolate().host;
138
+ }
139
+ // src/hooks/route.ts
140
+ /** The current route, or `undefined` until the first record arrives (or with routing off). */
141
+ function useRoute() {
142
+ return useTopic(routeTopic, { from: "earliest" });
143
+ }
144
+ export { BusError, HostCallError, defineIsolate, useHost, useIsolate, usePortalRoot, usePublish, useRequest, useRoute, useTopic };
package/package.json CHANGED
@@ -1,9 +1,39 @@
1
1
  {
2
2
  "name": "@isoloop/preact",
3
- "version": "0.0.0",
4
- "description": "Placeholder for @isoloop/preact. Real releases come from https://github.com/isoloop/isoloop.",
3
+ "version": "0.1.0-next.0",
4
+ "description": "Preact adapter for Isoloop isolates: defineIsolate(Component) and bus and host hooks.",
5
5
  "license": "MIT",
6
- "repository": { "type": "git", "url": "git+https://github.com/isoloop/isoloop.git", "directory": "packages/preact" },
7
- "homepage": "https://github.com/isoloop/isoloop",
8
- "publishConfig": { "access": "public" }
9
- }
6
+ "type": "module",
7
+ "sideEffects": false,
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/isoloop/isoloop.git",
11
+ "directory": "packages/preact"
12
+ },
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "import": "./dist/index.js"
20
+ }
21
+ },
22
+ "peerDependencies": {
23
+ "preact": "^10 || ^11"
24
+ },
25
+ "dependencies": {
26
+ "@isoloop/protocol": "0.1.0-next.0",
27
+ "@isoloop/runtime": "0.1.0-next.0"
28
+ },
29
+ "devDependencies": {
30
+ "@isoloop/dom": "0.1.0-next.0",
31
+ "preact10": "npm:preact@10.29.8",
32
+ "preact11": "npm:preact@11.1.1",
33
+ "typescript": "7.0.2"
34
+ },
35
+ "scripts": {
36
+ "build": "tsdown",
37
+ "typecheck": "tsc --noEmit"
38
+ }
39
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # @isoloop/preact
2
-
3
- Placeholder. Real releases are published from [isoloop/isoloop](https://github.com/isoloop/isoloop).