@praxisjs/runtime 0.5.5 → 0.7.0

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 (68) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/dist/__tests__/children.test.js +54 -0
  3. package/dist/__tests__/children.test.js.map +1 -1
  4. package/dist/__tests__/component.test.js +60 -1
  5. package/dist/__tests__/component.test.js.map +1 -1
  6. package/dist/__tests__/dom.test.js +100 -0
  7. package/dist/__tests__/dom.test.js.map +1 -1
  8. package/dist/__tests__/hydration.test.d.ts +2 -0
  9. package/dist/__tests__/hydration.test.d.ts.map +1 -0
  10. package/dist/__tests__/hydration.test.js +299 -0
  11. package/dist/__tests__/hydration.test.js.map +1 -0
  12. package/dist/__tests__/portal.test.js +22 -0
  13. package/dist/__tests__/portal.test.js.map +1 -1
  14. package/dist/__tests__/remove.test.d.ts +2 -0
  15. package/dist/__tests__/remove.test.d.ts.map +1 -0
  16. package/dist/__tests__/remove.test.js +45 -0
  17. package/dist/__tests__/remove.test.js.map +1 -0
  18. package/dist/component.d.ts.map +1 -1
  19. package/dist/component.js +22 -1
  20. package/dist/component.js.map +1 -1
  21. package/dist/dom/events.d.ts.map +1 -1
  22. package/dist/dom/events.js +18 -2
  23. package/dist/dom/events.js.map +1 -1
  24. package/dist/dom/props.d.ts.map +1 -1
  25. package/dist/dom/props.js +27 -5
  26. package/dist/dom/props.js.map +1 -1
  27. package/dist/dom/remove.d.ts +2 -0
  28. package/dist/dom/remove.d.ts.map +1 -0
  29. package/dist/dom/remove.js +23 -0
  30. package/dist/dom/remove.js.map +1 -0
  31. package/dist/element.d.ts.map +1 -1
  32. package/dist/element.js +5 -0
  33. package/dist/element.js.map +1 -1
  34. package/dist/hydration-context.d.ts +21 -0
  35. package/dist/hydration-context.d.ts.map +1 -0
  36. package/dist/hydration-context.js +40 -0
  37. package/dist/hydration-context.js.map +1 -0
  38. package/dist/hydration.d.ts +18 -0
  39. package/dist/hydration.d.ts.map +1 -0
  40. package/dist/hydration.js +67 -0
  41. package/dist/hydration.js.map +1 -0
  42. package/dist/index.d.ts +6 -0
  43. package/dist/index.d.ts.map +1 -1
  44. package/dist/index.js +45 -10
  45. package/dist/index.js.map +1 -1
  46. package/dist/portal.d.ts.map +1 -1
  47. package/dist/portal.js +16 -7
  48. package/dist/portal.js.map +1 -1
  49. package/dist/reactive.d.ts.map +1 -1
  50. package/dist/reactive.js +16 -10
  51. package/dist/reactive.js.map +1 -1
  52. package/package.json +4 -4
  53. package/src/__tests__/children.test.ts +58 -0
  54. package/src/__tests__/component.test.ts +67 -1
  55. package/src/__tests__/dom.test.ts +108 -0
  56. package/src/__tests__/hydration.test.ts +360 -0
  57. package/src/__tests__/portal.test.ts +24 -0
  58. package/src/__tests__/remove.test.ts +51 -0
  59. package/src/component.ts +23 -1
  60. package/src/dom/events.ts +21 -2
  61. package/src/dom/props.ts +30 -5
  62. package/src/dom/remove.ts +22 -0
  63. package/src/element.ts +5 -0
  64. package/src/hydration-context.ts +53 -0
  65. package/src/hydration.ts +70 -0
  66. package/src/index.ts +57 -11
  67. package/src/portal.ts +16 -7
  68. package/src/reactive.ts +15 -9
@@ -0,0 +1,22 @@
1
+ // Replaces Range.deleteContents(): creating a Range per removal is several times slower than
2
+ // walking the siblings, and unreachable Ranges stay registered with the document until collected.
3
+ // Walking first → last (rather than only the tracked nodes) also removes anything a nested reactive
4
+ // child inserted between them after mount, which is what the Range used to cover.
5
+ export function removeNodes(nodes: Node[]): void {
6
+ const first = nodes[0];
7
+ const last = nodes[nodes.length - 1];
8
+ const parent = first.parentNode;
9
+
10
+ if (parent === null || last.parentNode !== parent) {
11
+ for (const node of nodes) node.parentNode?.removeChild(node);
12
+ return;
13
+ }
14
+
15
+ let node: Node | null = first;
16
+ while (node) {
17
+ const next: Node | null = node.nextSibling;
18
+ parent.removeChild(node);
19
+ if (node === last) return;
20
+ node = next;
21
+ }
22
+ }
package/src/element.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { mountChildren } from "./children";
2
2
  import { createElement } from "./dom/create";
3
3
  import { applyProp } from "./dom/props";
4
+ import { isRecording, recordProps } from "./hydration-context";
4
5
 
5
6
  import type { Scope } from "./scope";
6
7
 
@@ -11,6 +12,10 @@ export function mountElement(
11
12
  ): HTMLElement | SVGElement {
12
13
  const el = createElement(tag);
13
14
 
15
+ // During a hydration build pass, retain (props, scope) so reconcile() can
16
+ // replay them onto whichever real element it ends up keeping instead.
17
+ if (isRecording()) recordProps(el, props, scope);
18
+
14
19
  for (const key in props) {
15
20
  if (key === "children") continue;
16
21
  applyProp(el, key, props[key], scope);
@@ -0,0 +1,53 @@
1
+ import type { Scope } from "./scope";
2
+
3
+ let _recording = false;
4
+
5
+ /**
6
+ * Marks the synchronous "build" pass of a hydration attempt: `factory()` runs
7
+ * completely normally (same create-mode code as a plain client render), but
8
+ * every element additionally records its (props, scope) for reconcile()'s
9
+ * replay step, and every mountReactive() region records which of its output
10
+ * nodes may need to be swapped in-place if reconcile() decides to keep a real
11
+ * element instead of the fresh one. No-op / zero cost outside a build pass.
12
+ */
13
+ export function runInRecording<T>(fn: () => T): T {
14
+ const prev = _recording;
15
+ _recording = true;
16
+ try {
17
+ return fn();
18
+ } finally {
19
+ _recording = prev;
20
+ }
21
+ }
22
+
23
+ export function isRecording(): boolean {
24
+ return _recording;
25
+ }
26
+
27
+ interface PropRecord {
28
+ props: Record<string, unknown>;
29
+ scope: Scope;
30
+ }
31
+
32
+ const _propRecords = new WeakMap<Element, PropRecord>();
33
+
34
+ export function recordProps(el: Element, props: Record<string, unknown>, scope: Scope): void {
35
+ _propRecords.set(el, { props, scope });
36
+ }
37
+
38
+ export function getRecordedProps(el: Element): PropRecord | undefined {
39
+ return _propRecords.get(el);
40
+ }
41
+
42
+ // mountReactive() keeps a `currentNodes` array (by reference) for diffing its
43
+ // *next* update. If reconcile() replaces one of those fresh nodes with a real
44
+ // one, it needs to overwrite that array in place so the closure sees the swap.
45
+ const _reactiveOwners = new WeakMap<Node, Node[]>();
46
+
47
+ export function registerReactiveNode(node: Node, owner: Node[]): void {
48
+ _reactiveOwners.set(node, owner);
49
+ }
50
+
51
+ export function getReactiveOwner(node: Node): Node[] | undefined {
52
+ return _reactiveOwners.get(node);
53
+ }
@@ -0,0 +1,70 @@
1
+ import { applyProp } from "./dom/props";
2
+ import { getReactiveOwner, getRecordedProps } from "./hydration-context";
3
+
4
+ function replayProps(real: Element, fresh: Element): void {
5
+ const record = getRecordedProps(fresh);
6
+ if (!record) return;
7
+ for (const key in record.props) {
8
+ if (key === "children") continue;
9
+ applyProp(real, key, record.props[key], record.scope);
10
+ }
11
+ }
12
+
13
+ // mountReactive()'s `currentNodes` array holds fresh node references for its
14
+ // *next* diff. If we're keeping `real` instead of `fresh` here, that array
15
+ // needs to see the swap too — it's registered by reference, so this mutation
16
+ // is visible to whichever effect closure owns it.
17
+ function notifyReplacement(fresh: Node, real: Node): void {
18
+ const owner = getReactiveOwner(fresh);
19
+ if (!owner) return;
20
+ const idx = owner.indexOf(fresh);
21
+ if (idx !== -1) owner[idx] = real;
22
+ }
23
+
24
+ /**
25
+ * Walks `freshChildren` (the complete, correctly-nested output of a normal
26
+ * create-mode mount into a detached scratch node) against `realParent`'s
27
+ * existing children (the server-rendered DOM already attached to the page),
28
+ * in document order — safe now that both trees are fully built, unlike trying
29
+ * to adopt while `freshChildren` was still being constructed.
30
+ *
31
+ * Matching elements keep the real node (props/listeners replayed onto it,
32
+ * via `applyProp` — the same function normal mounting uses) and recurse.
33
+ * Text/comment nodes are always taken from the fresh side — they carry no
34
+ * listeners or state, so recreating them is free and avoids having to
35
+ * re-point mountComponent/mountReactive's anchor comments. Anything that
36
+ * doesn't line up (different tag, or one side ran out) is a local mismatch:
37
+ * the stale real node is dropped and the fresh one takes its place — no
38
+ * lookahead, no reordering, and no whole-page fallback.
39
+ */
40
+ export function reconcile(realParent: Node, freshChildren: Node[]): void {
41
+ let realCursor: ChildNode | null = realParent.firstChild;
42
+
43
+ for (const fresh of freshChildren) {
44
+ if (
45
+ fresh.nodeType === Node.ELEMENT_NODE &&
46
+ realCursor?.nodeType === Node.ELEMENT_NODE &&
47
+ (realCursor as Element).localName === (fresh as Element).localName
48
+ ) {
49
+ const real = realCursor as Element;
50
+ realCursor = real.nextSibling;
51
+ replayProps(real, fresh as Element);
52
+ reconcile(real, Array.from((fresh as Element).childNodes));
53
+ notifyReplacement(fresh, real);
54
+ continue;
55
+ }
56
+
57
+ if (realCursor) {
58
+ const stale = realCursor;
59
+ realCursor = realCursor.nextSibling;
60
+ stale.remove();
61
+ }
62
+ realParent.insertBefore(fresh, realCursor);
63
+ }
64
+
65
+ while (realCursor) {
66
+ const stale = realCursor;
67
+ realCursor = realCursor.nextSibling;
68
+ stale.remove();
69
+ }
70
+ }
package/src/index.ts CHANGED
@@ -1,22 +1,17 @@
1
1
  import { mountChildren } from "./children";
2
2
  import { runInScope } from "./context";
3
+ import { reconcile } from "./hydration";
4
+ import { runInRecording } from "./hydration-context";
3
5
  import { Scope } from "./scope";
4
6
 
5
- /**
6
- * Renders a component tree into a container element.
7
- *
8
- * The factory function is called once inside the root scope,
9
- * so `jsx()` can access the current scope via `getCurrentScope()`.
10
- *
11
- * @example
12
- * render(() => <App />, document.getElementById('app'));
13
- */
14
- export function render(
7
+ /** Written by `@praxisjs/ssg` onto the mount container in its prerendered HTML. */
8
+ const SSG_MARKER = "data-praxis-ssg";
9
+
10
+ function mountCreate(
15
11
  factory: () => Node | Node[] | null,
16
12
  container: HTMLElement,
17
13
  ): () => void {
18
14
  const rootScope = new Scope();
19
-
20
15
  container.innerHTML = "";
21
16
 
22
17
  runInScope(rootScope, () => {
@@ -30,6 +25,57 @@ export function render(
30
25
  };
31
26
  }
32
27
 
28
+ function hydrate(
29
+ factory: () => Node | Node[] | null,
30
+ container: HTMLElement,
31
+ ): () => void {
32
+ const rootScope = new Scope();
33
+ const scratch = document.createDocumentFragment();
34
+
35
+ // Build normally — same code as mountCreate, just into a detached scratch
36
+ // node instead of the real container, so it can be walked in document
37
+ // order afterwards (its own construction order is bottom-up, not useful
38
+ // for matching against the server-rendered DOM directly).
39
+ runInRecording(() => {
40
+ runInScope(rootScope, () => {
41
+ const result = factory();
42
+ mountChildren(scratch, result, rootScope);
43
+ });
44
+ });
45
+
46
+ reconcile(container, Array.from(scratch.childNodes));
47
+
48
+ return () => {
49
+ rootScope.dispose();
50
+ container.innerHTML = "";
51
+ };
52
+ }
53
+
54
+ /**
55
+ * Renders a component tree into a container element.
56
+ *
57
+ * The factory function is called once inside the root scope,
58
+ * so `jsx()` can access the current scope via `getCurrentScope()`.
59
+ *
60
+ * If `container` carries the marker `@praxisjs/ssg` stamps onto prerendered
61
+ * HTML, the tree is built normally into a detached scratch node and then
62
+ * reconciled against the existing DOM (real elements are kept and have their
63
+ * props/listeners replayed onto them; text/comments and any mismatched nodes
64
+ * are recreated) instead of clearing the container and mounting fresh.
65
+ *
66
+ * @example
67
+ * render(() => <App />, document.getElementById('app'));
68
+ */
69
+ export function render(
70
+ factory: () => Node | Node[] | null,
71
+ container: HTMLElement,
72
+ ): () => void {
73
+ if (container.hasAttribute(SSG_MARKER)) {
74
+ return hydrate(factory, container);
75
+ }
76
+ return mountCreate(factory, container);
77
+ }
78
+
33
79
  export { Scope } from "./scope";
34
80
  export { runInScope, getCurrentScope } from "./context";
35
81
  export { mountElement } from "./element";
package/src/portal.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  import { StatelessComponent } from "@praxisjs/core";
2
+ import { isServerRenderPass } from "@praxisjs/core/internal";
2
3
 
3
4
  import { mountChildren } from "./children";
4
5
  import { getCurrentScope } from "./context";
6
+ import { removeNodes } from "./dom/remove";
5
7
 
6
8
  export interface PortalProps {
7
9
  to?: Element | string | null;
@@ -12,6 +14,13 @@ export class Portal extends StatelessComponent<PortalProps> {
12
14
  static readonly __isStateless = true;
13
15
 
14
16
  render() {
17
+ // Portals escape to an arbitrary external target (default document.body),
18
+ // which would need a hydration cursor shared across every portal mounted at
19
+ // that same target — out of scope for v1. Skip during the server render pass
20
+ // (target content isn't part of the static HTML); the client mounts normally,
21
+ // create-mode, right after hydration completes — no duplication.
22
+ if (isServerRenderPass()) return null;
23
+
15
24
  const scope = getCurrentScope();
16
25
  const target = resolvePortalTarget(this.props.to);
17
26
  if (!target) return null;
@@ -19,16 +28,16 @@ export class Portal extends StatelessComponent<PortalProps> {
19
28
  const start = document.createComment("");
20
29
  const end = document.createComment("");
21
30
 
22
- target.appendChild(start);
23
- mountChildren(target, this.props.children, scope);
24
- target.appendChild(end);
31
+ // Built off-document and attached once, so the target never sees a half-built subtree.
32
+ const fragment = document.createDocumentFragment();
33
+ fragment.appendChild(start);
34
+ mountChildren(fragment, this.props.children, scope);
35
+ fragment.appendChild(end);
36
+ target.appendChild(fragment);
25
37
 
26
38
  scope.add(() => {
27
39
  if (!start.parentNode || start.parentNode !== end.parentNode) return;
28
- const range = document.createRange();
29
- range.setStartBefore(start);
30
- range.setEndAfter(end);
31
- range.deleteContents();
40
+ removeNodes([start, end]);
32
41
  });
33
42
 
34
43
  return document.createComment("portal");
package/src/reactive.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  import { runInScope } from "./context";
2
+ import { removeNodes } from "./dom/remove";
3
+ import { isRecording, registerReactiveNode } from "./hydration-context";
2
4
  import { Scope } from "./scope";
3
5
 
4
6
  function collectNodes(value: unknown, out: Node[]): void {
@@ -37,17 +39,21 @@ export function mountReactive(
37
39
  const newNodes: Node[] = [];
38
40
  collectNodes(result, newNodes);
39
41
 
42
+ // During a hydration build pass, remember that each of these fresh nodes
43
+ // belongs to *this* array — reconcile() mutates `newNodes` in place (by
44
+ // reference, same array `currentNodes` ends up pointing at) if it decides
45
+ // to keep a real element instead of one of them, so future diffs here
46
+ // stay correct.
47
+ if (isRecording()) {
48
+ for (const n of newNodes) registerReactiveNode(n, newNodes);
49
+ }
50
+
40
51
  const anchor = end.parentNode ?? parent;
41
52
 
42
- if (currentNodes.length > 0) {
43
- if (currentNodes.length === 1) {
44
- (anchor as Element).removeChild(currentNodes[0]);
45
- } else {
46
- const range = document.createRange();
47
- range.setStartBefore(currentNodes[0]);
48
- range.setEndAfter(currentNodes[currentNodes.length - 1]);
49
- range.deleteContents();
50
- }
53
+ if (currentNodes.length === 1) {
54
+ (anchor as Element).removeChild(currentNodes[0]);
55
+ } else if (currentNodes.length > 1) {
56
+ removeNodes(currentNodes);
51
57
  }
52
58
 
53
59
  if (newNodes.length > 0) {