@isoloop/preact 0.0.0-next.69da74b → 0.0.0-next.800569e

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
@@ -8,6 +8,12 @@ type IsolateProps = {
8
8
  export declare function defineIsolate(Component: ComponentType<IsolateProps>): IsolateDefinition;
9
9
  /** The isolate context. Throws outside a component that `defineIsolate` rendered. */
10
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;
11
17
  type UseTopicOptions = {
12
18
  /** Default `latest`. `earliest` replays the topic, so a compacted one gives its current value at once. */
13
19
  from?: StartPosition;
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { BusError, HostCallError, REQUIRES } from "@isoloop/runtime";
2
- import { createContext, h, render } from "preact";
2
+ import { Component, createContext, h, options, render } from "preact";
3
3
  import { useCallback, useContext, useLayoutEffect, useState } from "preact/hooks";
4
4
  import { routeTopic } from "@isoloop/protocol";
5
5
  // src/context.ts
@@ -10,14 +10,80 @@ function useIsolate() {
10
10
  if (!ctx) throw new Error("useIsolate: not inside a component rendered by defineIsolate()");
11
11
  return ctx;
12
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
+ }
13
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
+ }
14
79
  function defineIsolate(Component) {
15
80
  return {
16
81
  requires: REQUIRES,
17
82
  start(ctx) {
18
83
  const container = ctx.root;
19
84
  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);
85
+ bindBatches(ctx);
86
+ render(h(IsolateCtx.Provider, { value: ctx }, h(Boundary, { ctx }, h(Component, { config: ctx.config }))), container);
21
87
  ctx.onShutdown(() => {
22
88
  render(null, container);
23
89
  });
@@ -75,4 +141,4 @@ function useHost() {
75
141
  function useRoute() {
76
142
  return useTopic(routeTopic, { from: "earliest" });
77
143
  }
78
- export { BusError, HostCallError, defineIsolate, useHost, useIsolate, usePublish, useRequest, useRoute, useTopic };
144
+ export { BusError, HostCallError, defineIsolate, useHost, useIsolate, usePortalRoot, usePublish, useRequest, useRoute, useTopic };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@isoloop/preact",
3
- "version": "0.0.0-next.69da74b",
3
+ "version": "0.0.0-next.800569e",
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.0.0-next.69da74b",
27
- "@isoloop/runtime": "0.0.0-next.69da74b"
26
+ "@isoloop/protocol": "0.0.0-next.800569e",
27
+ "@isoloop/runtime": "0.0.0-next.800569e"
28
28
  },
29
29
  "devDependencies": {
30
- "@isoloop/dom": "0.0.0-next.69da74b",
30
+ "@isoloop/dom": "0.0.0-next.800569e",
31
31
  "preact10": "npm:preact@10.29.8",
32
32
  "preact11": "npm:preact@11.1.1",
33
33
  "typescript": "7.0.2"