@praxisjs/runtime 0.6.0 → 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.
- package/CHANGELOG.md +15 -0
- package/dist/__tests__/children.test.js +54 -0
- package/dist/__tests__/children.test.js.map +1 -1
- package/dist/__tests__/component.test.js +60 -1
- package/dist/__tests__/component.test.js.map +1 -1
- package/dist/__tests__/dom.test.js +100 -0
- package/dist/__tests__/dom.test.js.map +1 -1
- package/dist/__tests__/portal.test.js +22 -0
- package/dist/__tests__/portal.test.js.map +1 -1
- package/dist/__tests__/remove.test.d.ts +2 -0
- package/dist/__tests__/remove.test.d.ts.map +1 -0
- package/dist/__tests__/remove.test.js +45 -0
- package/dist/__tests__/remove.test.js.map +1 -0
- package/dist/component.d.ts.map +1 -1
- package/dist/component.js +22 -1
- package/dist/component.js.map +1 -1
- package/dist/dom/events.d.ts.map +1 -1
- package/dist/dom/events.js +18 -2
- package/dist/dom/events.js.map +1 -1
- package/dist/dom/props.d.ts.map +1 -1
- package/dist/dom/props.js +27 -5
- package/dist/dom/props.js.map +1 -1
- package/dist/dom/remove.d.ts +2 -0
- package/dist/dom/remove.d.ts.map +1 -0
- package/dist/dom/remove.js +23 -0
- package/dist/dom/remove.js.map +1 -0
- package/dist/portal.d.ts.map +1 -1
- package/dist/portal.js +8 -7
- package/dist/portal.js.map +1 -1
- package/dist/reactive.d.ts.map +1 -1
- package/dist/reactive.js +6 -10
- package/dist/reactive.js.map +1 -1
- package/package.json +3 -3
- package/src/__tests__/children.test.ts +58 -0
- package/src/__tests__/component.test.ts +67 -1
- package/src/__tests__/dom.test.ts +108 -0
- package/src/__tests__/portal.test.ts +24 -0
- package/src/__tests__/remove.test.ts +51 -0
- package/src/component.ts +23 -1
- package/src/dom/events.ts +21 -2
- package/src/dom/props.ts +30 -5
- package/src/dom/remove.ts +22 -0
- package/src/portal.ts +8 -7
- package/src/reactive.ts +5 -9
package/dist/dom/events.js
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
|
+
import { batch } from "@praxisjs/core/internal";
|
|
2
|
+
// One wrapper per handler keeps addEventListener's dedupe of an identical listener intact.
|
|
3
|
+
const batchedHandlers = new WeakMap();
|
|
4
|
+
// Handlers run inside batch() so several signal writes in one event flush their effects once,
|
|
5
|
+
// instead of re-running the same reactive subtree after every write.
|
|
6
|
+
function batched(handler) {
|
|
7
|
+
let wrapper = batchedHandlers.get(handler);
|
|
8
|
+
if (!wrapper) {
|
|
9
|
+
wrapper = function (event) {
|
|
10
|
+
batch(() => { handler.call(this, event); });
|
|
11
|
+
};
|
|
12
|
+
batchedHandlers.set(handler, wrapper);
|
|
13
|
+
}
|
|
14
|
+
return wrapper;
|
|
15
|
+
}
|
|
1
16
|
export function addEvent(el, eventName, handler, scope) {
|
|
2
|
-
|
|
3
|
-
|
|
17
|
+
const listener = batched(handler);
|
|
18
|
+
el.addEventListener(eventName, listener);
|
|
19
|
+
scope.add(() => { el.removeEventListener(eventName, listener); });
|
|
4
20
|
}
|
|
5
21
|
//# sourceMappingURL=events.js.map
|
package/dist/dom/events.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"events.js","sourceRoot":"","sources":["../../src/dom/events.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"events.js","sourceRoot":"","sources":["../../src/dom/events.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAC;AAIhD,2FAA2F;AAC3F,MAAM,eAAe,GAAG,IAAI,OAAO,EAAgC,CAAC;AAEpE,8FAA8F;AAC9F,qEAAqE;AACrE,SAAS,OAAO,CAAC,OAAsB;IACrC,IAAI,OAAO,GAAG,eAAe,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IAC3C,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,OAAO,GAAG,UAAyB,KAAK;YACtC,KAAK,CAAC,GAAG,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QAC9C,CAAC,CAAC;QACF,eAAe,CAAC,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IACxC,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC;AAED,MAAM,UAAU,QAAQ,CACtB,EAAW,EACX,SAAiB,EACjB,OAAsB,EACtB,KAAY;IAEZ,MAAM,QAAQ,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAClC,EAAE,CAAC,gBAAgB,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;IACzC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,mBAAmB,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACpE,CAAC"}
|
package/dist/dom/props.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"props.d.ts","sourceRoot":"","sources":["../../src/dom/props.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"props.d.ts","sourceRoot":"","sources":["../../src/dom/props.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AA2FtC,wBAAgB,SAAS,CACvB,EAAE,EAAE,OAAO,EACX,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,OAAO,EACd,KAAK,EAAE,KAAK,GACX,IAAI,CAwBN"}
|
package/dist/dom/props.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { untrack } from "@praxisjs/core/internal";
|
|
1
2
|
import { EVENT_MAP } from "./constants";
|
|
2
3
|
import { addEvent } from "./events";
|
|
3
4
|
function applyClass(el, value) {
|
|
@@ -42,10 +43,9 @@ function applyAttr(el, key, value) {
|
|
|
42
43
|
el.setAttribute(key, String(value));
|
|
43
44
|
}
|
|
44
45
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
let target = el;
|
|
46
|
+
const writableCache = new WeakMap();
|
|
47
|
+
function lookupWritable(proto, key) {
|
|
48
|
+
let target = proto;
|
|
49
49
|
while (target) {
|
|
50
50
|
const descriptor = Object.getOwnPropertyDescriptor(target, key);
|
|
51
51
|
if (descriptor) {
|
|
@@ -55,6 +55,27 @@ function hasWritableProperty(el, key) {
|
|
|
55
55
|
}
|
|
56
56
|
return false;
|
|
57
57
|
}
|
|
58
|
+
// Read-only accessors (SVG geometry, `list`, `form`, `part`, `classList`…)
|
|
59
|
+
// must be excluded here or assignment below throws. The prototype walk is cached per
|
|
60
|
+
// prototype because every element of a given tag repeats the same lookups; a property defined on
|
|
61
|
+
// a prototype after its first lookup (e.g. a late polyfill) is therefore not picked up.
|
|
62
|
+
function hasWritableProperty(el, key) {
|
|
63
|
+
const own = Object.getOwnPropertyDescriptor(el, key);
|
|
64
|
+
if (own)
|
|
65
|
+
return own.set !== undefined || own.writable === true;
|
|
66
|
+
const proto = Object.getPrototypeOf(el);
|
|
67
|
+
let keys = writableCache.get(proto);
|
|
68
|
+
if (!keys) {
|
|
69
|
+
keys = new Map();
|
|
70
|
+
writableCache.set(proto, keys);
|
|
71
|
+
}
|
|
72
|
+
let writable = keys.get(key);
|
|
73
|
+
if (writable === undefined) {
|
|
74
|
+
writable = lookupWritable(proto, key);
|
|
75
|
+
keys.set(key, writable);
|
|
76
|
+
}
|
|
77
|
+
return writable;
|
|
78
|
+
}
|
|
58
79
|
function setProp(el, key, value) {
|
|
59
80
|
if (key === "class" || key === "className") {
|
|
60
81
|
applyClass(el, value);
|
|
@@ -77,7 +98,8 @@ export function applyProp(el, key, value, scope) {
|
|
|
77
98
|
if (normalizedKey === "key" || normalizedKey === "children")
|
|
78
99
|
return;
|
|
79
100
|
if (normalizedKey === "ref") {
|
|
80
|
-
|
|
101
|
+
// untracked: a ref callback that reads a signal must not subscribe the enclosing reactive child
|
|
102
|
+
untrack(() => { value(el); });
|
|
81
103
|
return;
|
|
82
104
|
}
|
|
83
105
|
if (normalizedKey in EVENT_MAP) {
|
package/dist/dom/props.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"props.js","sourceRoot":"","sources":["../../src/dom/props.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAIpC,SAAS,UAAU,CAAC,EAAW,EAAE,KAAc;IAC7C,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QAC1C,EAAE,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC;IAC9B,CAAC;SAAM,CAAC;QACN,gEAAgE;QAChE,EAAE,CAAC,YAAY,CAAC,OAAO,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;IAC1C,CAAC;AACH,CAAC;AAED,SAAS,UAAU,CAAC,EAAW,EAAE,KAAc;IAC7C,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QAC1C,EAAE,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC;IAC9B,CAAC;SAAM,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QACrC,MAAM,MAAM,GAAG,EAAiB,CAAC;QACjC,MAAM,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC;QAChC,KAAK,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,KAA+B,CAAC,EAAE,CAAC;YACrE,IAAI,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;gBACvB,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;YACjC,CAAC;iBAAM,CAAC;gBACL,MAAM,CAAC,KAA2C,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;YAC7D,CAAC;QACH,CAAC;IACH,CAAC;SAAM,CAA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C,GAAG,EAAE;YAChB,OAAO,CAAC,EAAE,EAAE,aAAa,EAAG,KAAuB,EAAE,CAAC,CAAC;QACzD,CAAC,CAAC,CAAC;QACH,OAAO;IACT,CAAC;IAED,OAAO,CAAC,EAAE,EAAE,aAAa,EAAE,KAAK,CAAC,CAAC;AACpC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"remove.d.ts","sourceRoot":"","sources":["../../src/dom/remove.ts"],"names":[],"mappings":"AAIA,wBAAgB,WAAW,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,IAAI,CAiB/C"}
|
|
@@ -0,0 +1,23 @@
|
|
|
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) {
|
|
6
|
+
const first = nodes[0];
|
|
7
|
+
const last = nodes[nodes.length - 1];
|
|
8
|
+
const parent = first.parentNode;
|
|
9
|
+
if (parent === null || last.parentNode !== parent) {
|
|
10
|
+
for (const node of nodes)
|
|
11
|
+
node.parentNode?.removeChild(node);
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
let node = first;
|
|
15
|
+
while (node) {
|
|
16
|
+
const next = node.nextSibling;
|
|
17
|
+
parent.removeChild(node);
|
|
18
|
+
if (node === last)
|
|
19
|
+
return;
|
|
20
|
+
node = next;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=remove.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"remove.js","sourceRoot":"","sources":["../../src/dom/remove.ts"],"names":[],"mappings":"AAAA,6FAA6F;AAC7F,kGAAkG;AAClG,oGAAoG;AACpG,kFAAkF;AAClF,MAAM,UAAU,WAAW,CAAC,KAAa;IACvC,MAAM,KAAK,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;IACvB,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IACrC,MAAM,MAAM,GAAG,KAAK,CAAC,UAAU,CAAC;IAEhC,IAAI,MAAM,KAAK,IAAI,IAAI,IAAI,CAAC,UAAU,KAAK,MAAM,EAAE,CAAC;QAClD,KAAK,MAAM,IAAI,IAAI,KAAK;YAAE,IAAI,CAAC,UAAU,EAAE,WAAW,CAAC,IAAI,CAAC,CAAC;QAC7D,OAAO;IACT,CAAC;IAED,IAAI,IAAI,GAAgB,KAAK,CAAC;IAC9B,OAAO,IAAI,EAAE,CAAC;QACZ,MAAM,IAAI,GAAgB,IAAI,CAAC,WAAW,CAAC;QAC3C,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;QACzB,IAAI,IAAI,KAAK,IAAI;YAAE,OAAO;QAC1B,IAAI,GAAG,IAAI,CAAC;IACd,CAAC;AACH,CAAC"}
|
package/dist/portal.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"portal.d.ts","sourceRoot":"","sources":["../src/portal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"portal.d.ts","sourceRoot":"","sources":["../src/portal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAOpD,MAAM,WAAW,WAAW;IAC1B,EAAE,CAAC,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAAC;CAC9B;AAED,qBAAa,MAAO,SAAQ,kBAAkB,CAAC,WAAW,CAAC;IACzD,MAAM,CAAC,QAAQ,CAAC,aAAa,EAAG,IAAI,CAAU;IAC9C,MAAM,CAAC,QAAQ,CAAC,aAAa,QAAQ;IAErC,MAAM;CA6BP;AAED,wBAAgB,mBAAmB,CAAC,EAAE,CAAC,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,GAAG,OAAO,GAAG,IAAI,CAOhF"}
|
package/dist/portal.js
CHANGED
|
@@ -2,6 +2,7 @@ import { StatelessComponent } from "@praxisjs/core";
|
|
|
2
2
|
import { isServerRenderPass } from "@praxisjs/core/internal";
|
|
3
3
|
import { mountChildren } from "./children";
|
|
4
4
|
import { getCurrentScope } from "./context";
|
|
5
|
+
import { removeNodes } from "./dom/remove";
|
|
5
6
|
export class Portal extends StatelessComponent {
|
|
6
7
|
static __isComponent = true;
|
|
7
8
|
static __isStateless = true;
|
|
@@ -19,16 +20,16 @@ export class Portal extends StatelessComponent {
|
|
|
19
20
|
return null;
|
|
20
21
|
const start = document.createComment("");
|
|
21
22
|
const end = document.createComment("");
|
|
22
|
-
target.
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
// Built off-document and attached once, so the target never sees a half-built subtree.
|
|
24
|
+
const fragment = document.createDocumentFragment();
|
|
25
|
+
fragment.appendChild(start);
|
|
26
|
+
mountChildren(fragment, this.props.children, scope);
|
|
27
|
+
fragment.appendChild(end);
|
|
28
|
+
target.appendChild(fragment);
|
|
25
29
|
scope.add(() => {
|
|
26
30
|
if (!start.parentNode || start.parentNode !== end.parentNode)
|
|
27
31
|
return;
|
|
28
|
-
|
|
29
|
-
range.setStartBefore(start);
|
|
30
|
-
range.setEndAfter(end);
|
|
31
|
-
range.deleteContents();
|
|
32
|
+
removeNodes([start, end]);
|
|
32
33
|
});
|
|
33
34
|
return document.createComment("portal");
|
|
34
35
|
}
|
package/dist/portal.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"portal.js","sourceRoot":"","sources":["../src/portal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAE7D,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CA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,WAAW,CAAC,QAAQ,CAAC,CAAC;QAE7B,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE;YACb,IAAI,CAAC,KAAK,CAAC,UAAU,IAAI,KAAK,CAAC,UAAU,KAAK,GAAG,CAAC,UAAU;gBAAE,OAAO;YACrE,WAAW,CAAC,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,CAAC;QAC5B,CAAC,CAAC,CAAC;QAEH,OAAO,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;IAC1C,CAAC;;AAGH,MAAM,UAAU,mBAAmB,CAAC,EAA4B;IAC9D,IAAI,EAAE,IAAI,IAAI,EAAE,CAAC;QACf,IAAI,OAAO,QAAQ,KAAK,WAAW;YAAE,OAAO,IAAI,CAAC;QACjD,OAAO,QAAQ,CAAC,IAAI,CAAC;IACvB,CAAC;IACD,IAAI,OAAO,EAAE,KAAK,QAAQ;QAAE,OAAO,QAAQ,CAAC,aAAa,CAAC,EAAE,CAAC,CAAC;IAC9D,OAAO,EAAE,CAAC;AACZ,CAAC"}
|
package/dist/reactive.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reactive.d.ts","sourceRoot":"","sources":["../src/reactive.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"reactive.d.ts","sourceRoot":"","sources":["../src/reactive.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAmBhC,wBAAgB,aAAa,CAC3B,MAAM,EAAE,IAAI,EACZ,EAAE,EAAE,MAAM,OAAO,EACjB,WAAW,EAAE,KAAK,GACjB,IAAI,CAwCN"}
|
package/dist/reactive.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { runInScope } from "./context";
|
|
2
|
+
import { removeNodes } from "./dom/remove";
|
|
2
3
|
import { isRecording, registerReactiveNode } from "./hydration-context";
|
|
3
4
|
import { Scope } from "./scope";
|
|
4
5
|
function collectNodes(value, out) {
|
|
@@ -47,16 +48,11 @@ export function mountReactive(parent, fn, parentScope) {
|
|
|
47
48
|
registerReactiveNode(n, newNodes);
|
|
48
49
|
}
|
|
49
50
|
const anchor = end.parentNode ?? parent;
|
|
50
|
-
if (currentNodes.length
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const range = document.createRange();
|
|
56
|
-
range.setStartBefore(currentNodes[0]);
|
|
57
|
-
range.setEndAfter(currentNodes[currentNodes.length - 1]);
|
|
58
|
-
range.deleteContents();
|
|
59
|
-
}
|
|
51
|
+
if (currentNodes.length === 1) {
|
|
52
|
+
anchor.removeChild(currentNodes[0]);
|
|
53
|
+
}
|
|
54
|
+
else if (currentNodes.length > 1) {
|
|
55
|
+
removeNodes(currentNodes);
|
|
60
56
|
}
|
|
61
57
|
if (newNodes.length > 0) {
|
|
62
58
|
anchor.insertBefore(nodesToFragment(newNodes), end);
|
package/dist/reactive.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reactive.js","sourceRoot":"","sources":["../src/reactive.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,WAAW,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AACxE,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC,SAAS,YAAY,CAAC,KAAc,EAAE,GAAW;IAC/C,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,KAAK;QAAE,OAAO;IACrE,IAAI,KAAK,YAAY,IAAI,EAAE,CAAC;QAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAAC,OAAO;IAAC,CAAC;IACvD,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QAAC,KAAK,MAAM,CAAC,IAAI,KAAK;YAAE,YAAY,CAAC,CAAC,EAAE,GAA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C,EAAE,CAAC;YACxB,MAAM,CAAC,YAAY,CAAC,eAAe,CAAC,QAAQ,CAAC,EAAE,GAAG,CAAC,CAAC;QACtD,CAAC;QAED,YAAY,GAAG,QAAQ,CAAC;IAC1B,CAAC,CAAC,CAAC;IAEH,WAAW,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,UAAU,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;AACnD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@praxisjs/runtime",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -14,8 +14,8 @@
|
|
|
14
14
|
"typescript": "^6.0.3"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@praxisjs/core": "2.
|
|
18
|
-
"@praxisjs/decorators": "1.
|
|
17
|
+
"@praxisjs/core": "2.2.0",
|
|
18
|
+
"@praxisjs/decorators": "1.7.0",
|
|
19
19
|
"@praxisjs/shared": "0.3.1"
|
|
20
20
|
},
|
|
21
21
|
"scripts": {
|
|
@@ -216,3 +216,61 @@ describe("mountReactive", () => {
|
|
|
216
216
|
document.body.removeChild(parent);
|
|
217
217
|
});
|
|
218
218
|
});
|
|
219
|
+
|
|
220
|
+
describe("fine-grained rows", () => {
|
|
221
|
+
function setup() {
|
|
222
|
+
const ids = signal([1, 2]);
|
|
223
|
+
const titles = signal<Record<number, string>>({ 1: "a", 2: "b" });
|
|
224
|
+
let listRuns = 0;
|
|
225
|
+
const ul = container();
|
|
226
|
+
const scope = new Scope();
|
|
227
|
+
mountChildren(ul, () => {
|
|
228
|
+
listRuns++;
|
|
229
|
+
return ids().map((id) => {
|
|
230
|
+
const li = document.createElement("li");
|
|
231
|
+
mountChildren(li, () => titles()[id], scope);
|
|
232
|
+
return li;
|
|
233
|
+
});
|
|
234
|
+
}, scope);
|
|
235
|
+
return { ids, titles, ul, scope, runs: () => listRuns };
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
it("changing row content updates text in place without re-running the list function", () => {
|
|
239
|
+
const { titles, ul, scope, runs } = setup();
|
|
240
|
+
const rows = [...ul.querySelectorAll("li")];
|
|
241
|
+
titles.set({ 1: "A", 2: "b" });
|
|
242
|
+
expect(runs()).toBe(1);
|
|
243
|
+
expect([...ul.querySelectorAll("li")]).toEqual(rows);
|
|
244
|
+
expect(rows.map((r) => r.textContent)).toEqual(["A", "b"]);
|
|
245
|
+
scope.dispose();
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it("changing the structure rebuilds the list", () => {
|
|
249
|
+
const { ids, titles, ul, scope, runs } = setup();
|
|
250
|
+
titles.set({ 1: "a", 2: "b", 3: "c" });
|
|
251
|
+
ids.set([1, 2, 3]);
|
|
252
|
+
expect(runs()).toBe(2);
|
|
253
|
+
expect(ul.querySelectorAll("li")).toHaveLength(3);
|
|
254
|
+
scope.dispose();
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
describe("mountReactive multi-node replacement", () => {
|
|
259
|
+
it("removes nodes that were inserted between the tracked ones after mount", () => {
|
|
260
|
+
const toggle = signal(0);
|
|
261
|
+
const parent = container();
|
|
262
|
+
const scope = new Scope();
|
|
263
|
+
let a!: Node;
|
|
264
|
+
mountChildren(parent, () => {
|
|
265
|
+
void toggle();
|
|
266
|
+
a = document.createElement("a");
|
|
267
|
+
return [a, document.createElement("b")];
|
|
268
|
+
}, scope);
|
|
269
|
+
const stray = document.createElement("stray");
|
|
270
|
+
parent.insertBefore(stray, a.nextSibling);
|
|
271
|
+
toggle.set(1);
|
|
272
|
+
expect(parent.contains(stray)).toBe(false);
|
|
273
|
+
expect([...parent.children].map((c) => c.localName)).toEqual(["a", "b"]);
|
|
274
|
+
scope.dispose();
|
|
275
|
+
});
|
|
276
|
+
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// @vitest-environment jsdom
|
|
2
|
-
import { describe, it, expect, vi } from "vitest";
|
|
2
|
+
import { describe, it, expect, vi, afterEach } from "vitest";
|
|
3
3
|
|
|
4
4
|
import { StatefulComponent } from "@praxisjs/core";
|
|
5
5
|
import { getComponentProps } from "@praxisjs/core/internal";
|
|
@@ -295,3 +295,69 @@ describe("mountComponent", () => {
|
|
|
295
295
|
scope.dispose();
|
|
296
296
|
});
|
|
297
297
|
});
|
|
298
|
+
|
|
299
|
+
describe("mountComponent onMount scheduling", () => {
|
|
300
|
+
afterEach(() => {
|
|
301
|
+
vi.unstubAllGlobals();
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
function stubMicrotasks(): Array<() => void> {
|
|
305
|
+
const jobs: Array<() => void> = [];
|
|
306
|
+
vi.stubGlobal("queueMicrotask", (fn: () => void) => { jobs.push(fn); });
|
|
307
|
+
return jobs;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function lifecycleComp(onMount: () => void) {
|
|
311
|
+
return class extends StatefulComponent {
|
|
312
|
+
static __isComponent = true as const;
|
|
313
|
+
static __isStateless = false;
|
|
314
|
+
onMount() { onMount(); }
|
|
315
|
+
render() { return null; }
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
it("drains every component mounted in the same pass with a single microtask, in mount order", () => {
|
|
320
|
+
const jobs = stubMicrotasks();
|
|
321
|
+
const order: string[] = [];
|
|
322
|
+
const scope = new Scope();
|
|
323
|
+
mountComponent(lifecycleComp(() => order.push("a")), {}, scope);
|
|
324
|
+
mountComponent(lifecycleComp(() => order.push("b")), {}, scope);
|
|
325
|
+
expect(jobs).toHaveLength(1);
|
|
326
|
+
jobs.shift()?.();
|
|
327
|
+
expect(order).toEqual(["a", "b"]);
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
it("schedules a new microtask for components mounted after a drain", () => {
|
|
331
|
+
const jobs = stubMicrotasks();
|
|
332
|
+
const onMount = vi.fn();
|
|
333
|
+
const scope = new Scope();
|
|
334
|
+
mountComponent(lifecycleComp(onMount), {}, scope);
|
|
335
|
+
jobs.shift()?.();
|
|
336
|
+
mountComponent(lifecycleComp(onMount), {}, scope);
|
|
337
|
+
expect(jobs).toHaveLength(1);
|
|
338
|
+
jobs.shift()?.();
|
|
339
|
+
expect(onMount).toHaveBeenCalledTimes(2);
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
it("a throwing onMount does not stop the others and is rethrown on its own microtask", () => {
|
|
343
|
+
const jobs = stubMicrotasks();
|
|
344
|
+
const second = vi.fn();
|
|
345
|
+
const scope = new Scope();
|
|
346
|
+
mountComponent(lifecycleComp(() => { throw new Error("mount boom"); }), {}, scope);
|
|
347
|
+
mountComponent(lifecycleComp(second), {}, scope);
|
|
348
|
+
jobs.shift()?.();
|
|
349
|
+
expect(second).toHaveBeenCalledOnce();
|
|
350
|
+
expect(jobs).toHaveLength(1);
|
|
351
|
+
expect(() => { jobs.shift()?.(); }).toThrow("mount boom");
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
it("skips onMount for a component disposed before the drain", () => {
|
|
355
|
+
const jobs = stubMicrotasks();
|
|
356
|
+
const onMount = vi.fn();
|
|
357
|
+
const scope = new Scope();
|
|
358
|
+
mountComponent(lifecycleComp(onMount), {}, scope);
|
|
359
|
+
scope.dispose();
|
|
360
|
+
jobs.shift()?.();
|
|
361
|
+
expect(onMount).not.toHaveBeenCalled();
|
|
362
|
+
});
|
|
363
|
+
});
|
|
@@ -323,4 +323,112 @@ describe("addEvent", () => {
|
|
|
323
323
|
expect(fn).toHaveBeenCalledOnce();
|
|
324
324
|
scope.dispose();
|
|
325
325
|
});
|
|
326
|
+
|
|
327
|
+
it("batches signal writes made by a handler so effects run once", () => {
|
|
328
|
+
const el = document.createElement("button");
|
|
329
|
+
const scope = new Scope();
|
|
330
|
+
const a = signal(0);
|
|
331
|
+
const b = signal(0);
|
|
332
|
+
const runs = vi.fn();
|
|
333
|
+
scope.effect(() => {
|
|
334
|
+
void a();
|
|
335
|
+
void b();
|
|
336
|
+
runs();
|
|
337
|
+
});
|
|
338
|
+
runs.mockClear();
|
|
339
|
+
addEvent(el, "click", () => {
|
|
340
|
+
a.set(1);
|
|
341
|
+
b.set(1);
|
|
342
|
+
}, scope);
|
|
343
|
+
el.dispatchEvent(new MouseEvent("click"));
|
|
344
|
+
expect(runs).toHaveBeenCalledOnce();
|
|
345
|
+
scope.dispose();
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
it("calls the handler with the element as this and forwards the event", () => {
|
|
349
|
+
const el = document.createElement("button");
|
|
350
|
+
const scope = new Scope();
|
|
351
|
+
let seenThis: unknown;
|
|
352
|
+
let seenEvent: Event | undefined;
|
|
353
|
+
addEvent(el, "click", function (this: unknown, e: Event) {
|
|
354
|
+
seenThis = this;
|
|
355
|
+
seenEvent = e;
|
|
356
|
+
}, scope);
|
|
357
|
+
const event = new MouseEvent("click");
|
|
358
|
+
el.dispatchEvent(event);
|
|
359
|
+
expect(seenThis).toBe(el);
|
|
360
|
+
expect(seenEvent).toBe(event);
|
|
361
|
+
scope.dispose();
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
it("registers the same handler on different elements independently", () => {
|
|
365
|
+
const a = document.createElement("button");
|
|
366
|
+
const b = document.createElement("button");
|
|
367
|
+
const scope = new Scope();
|
|
368
|
+
const fn = vi.fn();
|
|
369
|
+
addEvent(a, "click", fn, scope);
|
|
370
|
+
addEvent(b, "click", fn, scope);
|
|
371
|
+
a.dispatchEvent(new MouseEvent("click"));
|
|
372
|
+
b.dispatchEvent(new MouseEvent("click"));
|
|
373
|
+
expect(fn).toHaveBeenCalledTimes(2);
|
|
374
|
+
scope.dispose();
|
|
375
|
+
a.dispatchEvent(new MouseEvent("click"));
|
|
376
|
+
expect(fn).toHaveBeenCalledTimes(2);
|
|
377
|
+
});
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
describe("applyProp writable-property cache", () => {
|
|
381
|
+
it("assigns prototype properties and keeps doing so for later elements of the same tag", () => {
|
|
382
|
+
const scope = new Scope();
|
|
383
|
+
const first = document.createElement("input");
|
|
384
|
+
const second = document.createElement("input");
|
|
385
|
+
applyProp(first, "value", "a", scope);
|
|
386
|
+
applyProp(second, "value", "b", scope);
|
|
387
|
+
expect(first.value).toBe("a");
|
|
388
|
+
expect(second.value).toBe("b");
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
it("falls back to attributes for unknown and read-only properties, consistently across elements", () => {
|
|
392
|
+
const scope = new Scope();
|
|
393
|
+
for (let i = 0; i < 2; i++) {
|
|
394
|
+
const input = document.createElement("input");
|
|
395
|
+
applyProp(input, "data-x", "1", scope);
|
|
396
|
+
applyProp(input, "list", "dl", scope);
|
|
397
|
+
expect(input.getAttribute("data-x")).toBe("1");
|
|
398
|
+
expect(input.getAttribute("list")).toBe("dl");
|
|
399
|
+
}
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
it("honours own properties: writable ones are assigned, getter-only ones become attributes", () => {
|
|
403
|
+
const scope = new Scope();
|
|
404
|
+
const el = document.createElement("div") as unknown as HTMLElement & Record<string, unknown>;
|
|
405
|
+
el.expando = "old";
|
|
406
|
+
Object.defineProperty(el, "readonlyThing", { get: () => "fixed", configurable: true });
|
|
407
|
+
applyProp(el, "expando", "new", scope);
|
|
408
|
+
applyProp(el, "readonlyThing", "attr", scope);
|
|
409
|
+
expect(el.expando).toBe("new");
|
|
410
|
+
expect(el.readonlyThing).toBe("fixed");
|
|
411
|
+
expect(el.getAttribute("readonlyThing")).toBe("attr");
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
it("an own property with a setter is assigned", () => {
|
|
415
|
+
const scope = new Scope();
|
|
416
|
+
const el = document.createElement("div") as unknown as HTMLElement & Record<string, unknown>;
|
|
417
|
+
let stored = "";
|
|
418
|
+
Object.defineProperty(el, "custom", { get: () => stored, set: (v: string) => { stored = v; }, configurable: true });
|
|
419
|
+
applyProp(el, "custom", "set", scope);
|
|
420
|
+
expect(stored).toBe("set");
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
it("runs ref callbacks untracked so they do not subscribe the enclosing effect", () => {
|
|
424
|
+
const scope = new Scope();
|
|
425
|
+
const s = signal(0);
|
|
426
|
+
const runs = vi.fn();
|
|
427
|
+
scope.effect(() => {
|
|
428
|
+
runs();
|
|
429
|
+
applyProp(document.createElement("div"), "ref", () => { void s(); }, scope);
|
|
430
|
+
});
|
|
431
|
+
s.set(1);
|
|
432
|
+
expect(runs).toHaveBeenCalledOnce();
|
|
433
|
+
});
|
|
326
434
|
});
|
|
@@ -150,4 +150,28 @@ describe("resolvePortalTarget", () => {
|
|
|
150
150
|
expect(resolvePortalTarget("#resolve-test")).toBe(el);
|
|
151
151
|
document.body.removeChild(el);
|
|
152
152
|
});
|
|
153
|
+
|
|
154
|
+
it("attaches the whole subtree to the target in one operation", () => {
|
|
155
|
+
const target = document.createElement("div");
|
|
156
|
+
document.body.appendChild(target);
|
|
157
|
+
const spy = vi.spyOn(target, "appendChild");
|
|
158
|
+
const children = [document.createElement("p"), document.createElement("span")];
|
|
159
|
+
const { scope } = mount({ to: target, children });
|
|
160
|
+
expect(spy).toHaveBeenCalledOnce();
|
|
161
|
+
expect(target.querySelector("p")).not.toBeNull();
|
|
162
|
+
expect(target.querySelector("span")).not.toBeNull();
|
|
163
|
+
scope.dispose();
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("reactive children mounted through the portal keep updating after attach", () => {
|
|
167
|
+
const target = document.createElement("div");
|
|
168
|
+
document.body.appendChild(target);
|
|
169
|
+
const s = signal("one");
|
|
170
|
+
const { scope } = mount({ to: target, children: () => s() });
|
|
171
|
+
expect(target.textContent).toContain("one");
|
|
172
|
+
s.set("two");
|
|
173
|
+
expect(target.textContent).toContain("two");
|
|
174
|
+
expect(target.textContent).not.toContain("one");
|
|
175
|
+
scope.dispose();
|
|
176
|
+
});
|
|
153
177
|
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { describe, it, expect } from "vitest";
|
|
3
|
+
|
|
4
|
+
import { removeNodes } from "../dom/remove";
|
|
5
|
+
|
|
6
|
+
function siblings(...names: string[]): { parent: HTMLElement; nodes: HTMLElement[] } {
|
|
7
|
+
const parent = document.createElement("div");
|
|
8
|
+
const nodes = names.map((name) => {
|
|
9
|
+
const el = document.createElement(name);
|
|
10
|
+
parent.appendChild(el);
|
|
11
|
+
return el;
|
|
12
|
+
});
|
|
13
|
+
return { parent, nodes };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
describe("removeNodes", () => {
|
|
17
|
+
it("removes the first-to-last span and leaves the surrounding siblings", () => {
|
|
18
|
+
const { parent, nodes } = siblings("a", "b", "c", "d", "e");
|
|
19
|
+
removeNodes(nodes.slice(1, 4));
|
|
20
|
+
expect([...parent.children].map((c) => c.localName)).toEqual(["a", "e"]);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("also removes nodes inserted between first and last after the list was captured", () => {
|
|
24
|
+
const { parent, nodes } = siblings("a", "b");
|
|
25
|
+
const late = document.createElement("late");
|
|
26
|
+
parent.insertBefore(late, nodes[1]);
|
|
27
|
+
removeNodes(nodes);
|
|
28
|
+
expect(parent.childNodes).toHaveLength(0);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("removes a single node", () => {
|
|
32
|
+
const { parent, nodes } = siblings("a", "b");
|
|
33
|
+
removeNodes([nodes[0]]);
|
|
34
|
+
expect([...parent.children]).toEqual([nodes[1]]);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("removes the attached nodes individually when the first one is detached", () => {
|
|
38
|
+
const { parent, nodes } = siblings("a", "b", "c");
|
|
39
|
+
parent.removeChild(nodes[0]);
|
|
40
|
+
expect(() => { removeNodes(nodes); }).not.toThrow();
|
|
41
|
+
expect(parent.childNodes).toHaveLength(0);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("removes each node from its own parent when first and last have different parents", () => {
|
|
45
|
+
const one = siblings("a", "b");
|
|
46
|
+
const two = siblings("c", "d");
|
|
47
|
+
removeNodes([one.nodes[0], one.nodes[1], two.nodes[0]]);
|
|
48
|
+
expect(one.parent.childNodes).toHaveLength(0);
|
|
49
|
+
expect([...two.parent.children]).toEqual([two.nodes[1]]);
|
|
50
|
+
});
|
|
51
|
+
});
|
package/src/component.ts
CHANGED
|
@@ -11,6 +11,28 @@ import { runInScope } from "./context";
|
|
|
11
11
|
|
|
12
12
|
import type { Scope } from "./scope";
|
|
13
13
|
|
|
14
|
+
let pendingMounts: Array<() => void> = [];
|
|
15
|
+
|
|
16
|
+
// One shared microtask drains every component mounted in the same synchronous pass, in mount
|
|
17
|
+
// order (children before parents). A throwing onMount is rethrown on its own microtask so it
|
|
18
|
+
// can't stop the others, as it couldn't when each component had a microtask of its own.
|
|
19
|
+
function scheduleMount(run: () => void): void {
|
|
20
|
+
if (pendingMounts.length === 0) {
|
|
21
|
+
queueMicrotask(() => {
|
|
22
|
+
const queue = pendingMounts;
|
|
23
|
+
pendingMounts = [];
|
|
24
|
+
for (const job of queue) {
|
|
25
|
+
try {
|
|
26
|
+
job();
|
|
27
|
+
} catch (e) {
|
|
28
|
+
queueMicrotask(() => { throw e; });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
pendingMounts.push(run);
|
|
34
|
+
}
|
|
35
|
+
|
|
14
36
|
export function mountComponent(
|
|
15
37
|
ctor: ComponentConstructor,
|
|
16
38
|
props: Record<string, unknown>,
|
|
@@ -56,7 +78,7 @@ export function mountComponent(
|
|
|
56
78
|
mountChildren(container, dom, scope);
|
|
57
79
|
container.appendChild(end);
|
|
58
80
|
|
|
59
|
-
|
|
81
|
+
scheduleMount(() => {
|
|
60
82
|
if (disposed) return;
|
|
61
83
|
setComponentMounted(instance, true);
|
|
62
84
|
instance.onMount?.();
|