@isoloop/preact 0.0.0-next.4da23df
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 +21 -0
- package/dist/index.d.ts +39 -0
- package/dist/index.js +78 -0
- package/package.json +39 -0
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.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
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
|
+
type UseTopicOptions = {
|
|
12
|
+
/** Default `latest`. `earliest` replays the topic, so a compacted one gives its current value at once. */
|
|
13
|
+
from?: StartPosition;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* The latest value on `topic`, or `undefined` until one arrives. A bus
|
|
17
|
+
* restart that lost the memory topic (`onReset`, ADR 0007) clears it.
|
|
18
|
+
* Subscribes on mount and unsubscribes on unmount; changing `topic` or
|
|
19
|
+
* `from` resubscribes.
|
|
20
|
+
*/
|
|
21
|
+
export declare function useTopic<Out>(topic: TopicRef<any, Out, any, any>, opts?: UseTopicOptions): Out | undefined;
|
|
22
|
+
/** A function that publishes to `topic`; it resolves once the broker appended the record (`BusClient.publish`). */
|
|
23
|
+
export declare function usePublish<In>(topic: TopicRef<In, any, any, any>): (value: In, opts?: {
|
|
24
|
+
key?: string;
|
|
25
|
+
}) => Promise<void>;
|
|
26
|
+
type RequestOptions = {
|
|
27
|
+
key?: string;
|
|
28
|
+
timeoutMs?: number;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* A function that sends a request on `topic` and resolves with the reply.
|
|
32
|
+
* It rejects with a `BusError`, `timeout` after `timeoutMs` (default 30 s).
|
|
33
|
+
*/
|
|
34
|
+
export declare function useRequest<In, ROut>(topic: TopicRef<In, any, any, ROut>): (value: In, opts?: RequestOptions) => Promise<ROut>;
|
|
35
|
+
/** `measure`, `focus`, `scrollIntoView` and `navigate`, as `ctx.host` has them. */
|
|
36
|
+
export declare function useHost(): HostApi$1;
|
|
37
|
+
/** The current route, or `undefined` until the first record arrives (or with routing off). */
|
|
38
|
+
export declare function useRoute(): RouteValue$1 | undefined;
|
|
39
|
+
export { BusError, type HostApi, HostCallError, type IsolateContext, type IsolateProps, type RequestOptions, type RouteValue, type UseTopicOptions };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { BusError, HostCallError, REQUIRES } from "@isoloop/runtime";
|
|
2
|
+
import { createContext, h, 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
|
+
// src/define.ts
|
|
14
|
+
function defineIsolate(Component) {
|
|
15
|
+
return {
|
|
16
|
+
requires: REQUIRES,
|
|
17
|
+
start(ctx) {
|
|
18
|
+
const container = ctx.root;
|
|
19
|
+
if (!(container instanceof Element)) throw new Error(`defineIsolate (Preact): "${ctx.id}" is headless; Preact needs a surface`);
|
|
20
|
+
render(h(IsolateCtx.Provider, { value: ctx }, h(Component, { config: ctx.config })), container);
|
|
21
|
+
ctx.onShutdown(() => {
|
|
22
|
+
render(null, container);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
// src/hooks/topic.ts
|
|
28
|
+
/**
|
|
29
|
+
* The latest value on `topic`, or `undefined` until one arrives. A bus
|
|
30
|
+
* restart that lost the memory topic (`onReset`, ADR 0007) clears it.
|
|
31
|
+
* Subscribes on mount and unsubscribes on unmount; changing `topic` or
|
|
32
|
+
* `from` resubscribes.
|
|
33
|
+
*/
|
|
34
|
+
function useTopic(topic, opts = {}) {
|
|
35
|
+
const { bus } = useIsolate();
|
|
36
|
+
const [value, setValue] = useState(void 0);
|
|
37
|
+
const from = opts.from ?? "latest";
|
|
38
|
+
useLayoutEffect(() => {
|
|
39
|
+
setValue(void 0);
|
|
40
|
+
return bus.subscribe(topic, (r) => setValue({ v: r.value }), {
|
|
41
|
+
from,
|
|
42
|
+
onReset: () => setValue(void 0)
|
|
43
|
+
});
|
|
44
|
+
}, [
|
|
45
|
+
bus,
|
|
46
|
+
topic.name,
|
|
47
|
+
from
|
|
48
|
+
]);
|
|
49
|
+
return value?.v;
|
|
50
|
+
}
|
|
51
|
+
/** A function that publishes to `topic`; it resolves once the broker appended the record (`BusClient.publish`). */
|
|
52
|
+
function usePublish(topic) {
|
|
53
|
+
const { bus } = useIsolate();
|
|
54
|
+
return useCallback((value, o = {}) => bus.publish(topic, o.key === void 0 ? { value } : {
|
|
55
|
+
key: o.key,
|
|
56
|
+
value
|
|
57
|
+
}), [bus, topic.name]);
|
|
58
|
+
}
|
|
59
|
+
// src/hooks/request.ts
|
|
60
|
+
/**
|
|
61
|
+
* A function that sends a request on `topic` and resolves with the reply.
|
|
62
|
+
* It rejects with a `BusError`, `timeout` after `timeoutMs` (default 30 s).
|
|
63
|
+
*/
|
|
64
|
+
function useRequest(topic) {
|
|
65
|
+
const { bus } = useIsolate();
|
|
66
|
+
return useCallback((value, o) => bus.request(topic, value, o), [bus, topic.name]);
|
|
67
|
+
}
|
|
68
|
+
// src/hooks/host.ts
|
|
69
|
+
/** `measure`, `focus`, `scrollIntoView` and `navigate`, as `ctx.host` has them. */
|
|
70
|
+
function useHost() {
|
|
71
|
+
return useIsolate().host;
|
|
72
|
+
}
|
|
73
|
+
// src/hooks/route.ts
|
|
74
|
+
/** The current route, or `undefined` until the first record arrives (or with routing off). */
|
|
75
|
+
function useRoute() {
|
|
76
|
+
return useTopic(routeTopic, { from: "earliest" });
|
|
77
|
+
}
|
|
78
|
+
export { BusError, HostCallError, defineIsolate, useHost, useIsolate, usePublish, useRequest, useRoute, useTopic };
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@isoloop/preact",
|
|
3
|
+
"version": "0.0.0-next.4da23df",
|
|
4
|
+
"description": "Preact adapter for Isoloop isolates: defineIsolate(Component) and bus and host hooks.",
|
|
5
|
+
"license": "MIT",
|
|
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.0.0-next.4da23df",
|
|
27
|
+
"@isoloop/runtime": "0.0.0-next.4da23df"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@isoloop/dom": "0.0.0-next.4da23df",
|
|
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
|
+
}
|