@isoloop/preact 0.0.0-next.62ef495 → 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.
Files changed (2) hide show
  1. package/dist/index.js +35 -1
  2. package/package.json +4 -4
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { BusError, HostCallError, REQUIRES } from "@isoloop/runtime";
2
- import { Component, 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
@@ -43,12 +43,46 @@ var Boundary = class extends Component {
43
43
  return this.state.failed ? null : this.props.children;
44
44
  }
45
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
+ }
46
79
  function defineIsolate(Component) {
47
80
  return {
48
81
  requires: REQUIRES,
49
82
  start(ctx) {
50
83
  const container = ctx.root;
51
84
  if (!(container instanceof Element)) throw new Error(`defineIsolate (Preact): "${ctx.id}" is headless; Preact needs a surface`);
85
+ bindBatches(ctx);
52
86
  render(h(IsolateCtx.Provider, { value: ctx }, h(Boundary, { ctx }, h(Component, { config: ctx.config }))), container);
53
87
  ctx.onShutdown(() => {
54
88
  render(null, container);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@isoloop/preact",
3
- "version": "0.0.0-next.62ef495",
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.62ef495",
27
- "@isoloop/runtime": "0.0.0-next.62ef495"
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.62ef495",
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"