isoloop
0.1.1GitHub
Docs Reference · @isoloop/preact

Docs · ReferenceView as Markdown

@isoloop/preact

The Preact adapter: defineIsolate for a component, and hooks for the bus and the host.

On this page

@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.

ts
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:

tsx
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.

ts
function useHost(): HostApi;

Returns: The host API.

Throws:

  • Error outside a component that defineIsolate rendered.

Guides: Writing isolates.

useIsolate

The isolate's context, as start(ctx) gets it: ctx.bus, ctx.host, ctx.config and the rest.

ts
function useIsolate(): IsolateContext;

Returns: The context of the isolate this component runs in.

Throws:

  • Error outside a component that defineIsolate rendered.

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.

ts
function usePortalRoot(): Element;

Returns: The portal container.

Throws:

  • Error in a headless isolate, or outside a component that defineIsolate rendered.

Guides: Worker pools.

usePublish

A function that publishes to topic. The function is stable while topic is the same.

ts
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:

  • Error outside a component that defineIsolate rendered.

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.

ts
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.

ts
function useRoute(): RouteValue | undefined;

Returns: The current route, or undefined until the first one arrives, and always with routing off.

Throws:

  • Error outside a component that defineIsolate rendered.

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.

ts
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:

  • Error outside a component that defineIsolate rendered.

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.

ts
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.

ts
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.