@isoloop/preact
defineIsolate
Makes Component an isolate: the module's default export, which renders it into the
isolate's surface (with Preact) and unmounts it on shutdown, so effects clean up and
subscriptions end. Every hook in this package works inside it.
A render error is caught at the root and reported as this isolate's own crash, so in
a pool it fails only this member. The isolate needs a surface: started headless, it
throws an Error at start.
function defineIsolate(Component: ComponentType<IsolateProps>): IsolateDefinition;
| Parameter | Description |
|---|---|
Component |
The root component. It gets IsolateProps. |
Returns: The isolate definition, stamped with the runtime version it was built against.
Example:
import { defineIsolate, usePublish, useTopic } from '@isoloop/preact';
import { count } from './topics';
function Counter() {
const n = useTopic(count, { from: 'earliest' }) ?? 0;
const publish = usePublish(count);
return <button onClick={() => void publish(n + 1)}>Count: {n}</button>;
}
export default defineIsolate(Counter);
Guides: Writing isolates.
useHost
The isolate's host calls (ctx.host): measure, focus, scrollIntoView, navigate
and requestCanvas. Pass them the elements a ref holds; the host acts on what it
rendered for them, after pending changes are applied.
function useHost(): HostApi;
Returns: The host API.
Throws:
Erroroutside a component thatdefineIsolaterendered.
Guides: Writing isolates.
useIsolate
The isolate's context, as start(ctx) gets it: ctx.bus, ctx.host, ctx.config and
the rest.
function useIsolate(): IsolateContext;
Returns: The context of the isolate this component runs in.
Throws:
Erroroutside a component thatdefineIsolaterendered.
Guides: Writing isolates.
usePortalRoot
The container inside this isolate's surface for modals, tooltips, toasts and menus
(ctx.portalRoot), for createPortal or a library's container option. Content
portalled there stays inside the isolate's surface.
function usePortalRoot(): Element;
Returns: The portal container.
Throws:
Errorin a headless isolate, or outside a component thatdefineIsolaterendered.
Guides: Worker pools.
usePublish
A function that publishes to topic. The function is stable while topic is the same.
function usePublish<In>(topic: TopicRef<In, any, any, any>): (value: In, opts?: {
key?: string;
}) => Promise<void>;
| Parameter | Description |
|---|---|
topic |
The topic to publish to. The isolate needs a publish grant for it. |
Returns: (value, opts?) => Promise<void>, with opts.key for the record's key. The
promise resolves once the bus has appended the record, and rejects with a BusError:
forbidden without a publish grant, invalid when the schema rejects the value
(issues says why), too_large over the topic's maxRecordBytes, not_encodable
for a value the bus can't store, quota_exceeded when storage is full,
unknown_topic, or bus_unavailable.
Throws:
Erroroutside a component thatdefineIsolaterendered.
Guides: Writing isolates.
useRequest
Experimental
A function that sends a request on topic and resolves with the first valid reply.
The function is stable while topic is the same.
function useRequest<In, ROut>(topic: TopicRef<In, any, any, ROut>): (value: In, opts?: RequestOptions) => Promise<ROut>;
| Parameter | Description |
|---|---|
topic |
A topic with a reply schema. |
Returns: (value, opts?) => Promise<reply>. opts takes key and timeoutMs
(default 30 s). The promise rejects with a BusError: timeout when no reply came
in time, dropped when retention removed the request before any subscriber got it,
reply_invalid when the reply failed the reply schema, not_requestable on a
topic without one, and the publish codes (forbidden, invalid, too_large,
unknown_topic, bus_unavailable) for the request itself.
Guides: Writing isolates.
useRoute
The host's current location. The route is read only while a component uses this hook.
function useRoute(): RouteValue | undefined;
Returns: The current route, or undefined until the first one arrives, and always
with routing off.
Throws:
Erroroutside a component thatdefineIsolaterendered.
Guides: Writing isolates.
useTopic
The latest value on topic. It subscribes on mount and unsubscribes on unmount;
a different topic or from subscribes again.
An isolate has one subscription per topic. A second useTopic on the same topic, in
another component or another call in the same one, replaces the first with no error: only
the last one subscribed gets records, and the first stops updating. When the last one
unmounts, the topic has no subscription, and the first doesn't get it back. Call
useTopic once per topic, and pass the value down as props or share it through context.
With the kernel's dev: true, the bus warns in the console when this happens.
function useTopic<Out>(topic: TopicRef<any, Out, any, any>, opts?: UseTopicOptions): Out | undefined;
| Parameter | Description |
|---|---|
topic |
The topic to read. The isolate needs a subscribe grant for it. |
opts |
Where reading starts (UseTopicOptions). |
Returns: The latest value, or undefined until one arrives. A bus restart that lost a
memory topic clears it back to undefined.
Throws:
Erroroutside a component thatdefineIsolaterendered.
Guides: Writing isolates.
BusError
Also exported by @isoloop/host, which documents it: see BusError.
HostCallError
Re-exported from @isoloop/runtime: see HostCallError.
HostApi
Re-exported from @isoloop/runtime: see HostApi.
IsolateContext
Re-exported from @isoloop/runtime: see IsolateContext.
IsolateProps
The props defineIsolate passes to the isolate's component.
type IsolateProps = {
config: unknown;
};
| Field | Description |
|---|---|
config |
The config the isolate was spawned with (manifest.config), passed through unchanged; undefined when it has none. |
Guides: Writing isolates.
RequestOptions
Re-exported from @isoloop/runtime: see RequestOptions.
RouteValue
Also exported by @isoloop/react, which documents it: see RouteValue.
UseTopicOptions
Options for useTopic.
type UseTopicOptions = {
from?: StartPosition;
};
| Field | Description |
|---|---|
from? |
Where reading starts. Default 'latest': only records appended from now. 'earliest' replays the topic, so a compacted one gives its current value at once. |
Guides: Writing isolates.