Skip to content

Commit f6ab1b5

Browse files
committed
using always the same kind of abc object
1 parent 8bdb589 commit f6ab1b5

6 files changed

Lines changed: 36 additions & 56 deletions

File tree

esm/creator.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { PersistentFragment } from './persistent-fragment.js';
2-
import { detail, resolved } from './literals.js';
2+
import { abc, detail } from './literals.js';
33
import { array, hole } from './handler.js';
44
import { empty } from './utils.js';
55
import { cache } from './literals.js';
@@ -16,24 +16,26 @@ const childNodesIndex = (node, i) => node.childNodes[i];
1616
export default parse => (
1717
/** @param {(template: TemplateStringsArray, values: any[]) => import("./literals.js").Cache} parse */
1818
(template, values) => {
19-
const { f: fragment, e: entries, d: direct } = parse(template, values);
19+
const { a: fragment, b: entries, c: direct } = parse(template, values);
2020
const root = fragment.cloneNode(true);
21-
let details = empty, stack = empty;
21+
let details = empty;
2222
if (entries !== empty) {
2323
details = [];
24-
stack = [];
2524
for (let current, prev, i = 0; i < entries.length; i++) {
26-
const { p: path, u: update, n: name } = entries[i];
25+
const { a: path, b: update, c: name } = entries[i];
2726
const node = path === prev ? current : (current = find(root, (prev = path)));
28-
details[i] = detail(empty, update, node, name);
29-
stack[i] = update === array ? [] : (update === hole ? cache() : null);
27+
details[i] = detail(
28+
update,
29+
node,
30+
name,
31+
update === array ? [] : (update === hole ? cache() : null)
32+
);
3033
}
3134
}
32-
return resolved(
35+
return abc(
3336
template,
3437
direct ? root.firstChild : new PersistentFragment(root),
3538
details,
36-
stack,
3739
);
3840
}
3941
);

esm/literals.js

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,32 @@ import { empty } from './utils.js';
77
/** @typedef {Node | Element | PersistentFragment} Target */
88
/** @typedef {null | undefined | string | number | boolean | Node | Element | PersistentFragment} DOMValue */
99

10-
/**
11-
* @param {DocumentFragment} f content retrieved from the template
12-
* @param {Entry[]} e entries per each hole in the template
13-
* @param {boolean} d direct node to handle
14-
* @returns
15-
*/
16-
export const cel = (f, e, d) => ({ f, e, d });
10+
export const abc = (a, b, c) => ({ a, b, c });
1711

1812
/**
1913
* @typedef {Object} Detail
2014
* @property {any} v the current value of the interpolation / hole
2115
* @property {function} u the callback to update the value
2216
* @property {Node} t the target comment node or element
2317
* @property {string | null} n the attribute name, if any, or `null`
18+
* @param {Cache | Hole[] | Node[] | null} c the cache value for this detail
2419
*/
2520

2621
/**
27-
* @param {any} v the current value of the interpolation / hole
2822
* @param {function} u the callback to update the value
2923
* @param {Node} t the target comment node or element
3024
* @param {string | null} n the attribute name, if any, or `null`
3125
* @param {Cache | null} c the cache value for this detail
3226
* @returns {Detail}
3327
*/
34-
export const detail = (v, u, t, n, c) => ({ v, u, t, n, c });
28+
export const detail = (u, t, n, c) => ({ v: empty, u, t, n, c });
3529

3630
/**
3731
* @typedef {Object} Entry
38-
* @property {number[]} p the path to retrieve the node
39-
* @property {function} u the update function
40-
* @property {string | null} n the attribute name, if any, or `null`
41-
*/
42-
43-
/**
44-
* @param {number[]} p the path to retrieve the node
45-
* @param {function} u the update function
46-
* @param {string | null} n the attribute name, if any, or `null`
47-
* @returns {Entry}
32+
* @property {number[]} a the path to retrieve the node
33+
* @property {function} b the update function
34+
* @property {string | null} c the attribute name, if any, or `null`
4835
*/
49-
export const entry = (p, u, n) => ({ p, u, n });
5036

5137
/**
5238
* @typedef {Object} Cache
@@ -59,16 +45,7 @@ export const entry = (p, u, n) => ({ p, u, n });
5945
/**
6046
* @returns {Cache}
6147
*/
62-
export const cache = () => resolved(null, null, empty, empty);
63-
64-
/**
65-
* @property {null | TemplateStringsArray} t the cached template
66-
* @property {null | Node | PersistentFragment} n the node returned when parsing the template
67-
* @property {Detail[]} d the list of updates to perform
68-
* @property {Cache[]} s the stack of caches per each interpolation / hole
69-
* @returns {Cache}
70-
*/
71-
export const resolved = (t, n, d, s) => ({ t, n, d, s });
48+
export const cache = () => abc(null, null, empty);
7249

7350
/**
7451
* @typedef {Object} Parsed

esm/parser.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { TEXT_ELEMENTS } from 'domconstants/re';
33
import parser from '@webreflection/uparser';
44

55
import { empty, isArray, set } from './utils.js';
6-
import { cel, entry } from './literals.js';
6+
import { abc } from './literals.js';
77

88
import { array, attribute, hole, text, removeAttribute } from './handler.js';
99
import createContent from './create-content.js';
@@ -55,7 +55,7 @@ const resolve = (template, values, xml) => {
5555
// ⚠️ once array, always array!
5656
const update = isArray(values[i - 1]) ? array : hole;
5757
if (update === hole) replace.push(node);
58-
entries.push(entry(createPath(node), update, null));
58+
entries.push(abc(createPath(node), update, null));
5959
search = `${prefix}${i++}`;
6060
}
6161
}
@@ -65,7 +65,7 @@ const resolve = (template, values, xml) => {
6565
while (node.hasAttribute(search)) {
6666
if (!path) path = createPath(node);
6767
const name = node.getAttribute(search);
68-
entries.push(entry(path, attribute(node, name, xml), name));
68+
entries.push(abc(path, attribute(node, name, xml), name));
6969
removeAttribute(node, search);
7070
search = `${prefix}${i++}`;
7171
}
@@ -75,7 +75,7 @@ const resolve = (template, values, xml) => {
7575
TEXT_ELEMENTS.test(node.localName) &&
7676
node.textContent.trim() === `<!--${search}-->`
7777
) {
78-
entries.push(entry(path || createPath(node), text, null));
78+
entries.push(abc(path || createPath(node), text, null));
7979
search = `${prefix}${i++}`;
8080
}
8181
}
@@ -107,7 +107,7 @@ const resolve = (template, values, xml) => {
107107
len = 0;
108108
}
109109

110-
return set(cache, template, cel(content, entries, len === 1));
110+
return set(cache, template, abc(content, entries, len === 1));
111111
};
112112

113113
/** @type {WeakMap<TemplateStringsArray, Resolved>} */

esm/rabbit.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@ const parseSVG = create(parser(true));
1414
* @returns {Node}
1515
*/
1616
const unroll = (info, { s, t, v }) => {
17-
if (info.t !== t)
17+
if (info.a !== t)
1818
assign(info, (s ? parseSVG : parseHTML)(t, v));
19-
for (let { d, s } = info, i = 0; i < d.length; i++) {
19+
for (let { c } = info, i = 0; i < c.length; i++) {
2020
const value = v[i];
21-
const detail = d[i];
21+
const detail = c[i];
2222
const { v: previous, u: update, t: target, n: name } = detail;
2323
switch (update) {
2424
case array:
2525
detail.v = array(
2626
target,
27-
unrollValues(s[i], value),
27+
unrollValues(detail.c, value),
2828
previous
2929
);
3030
break;
3131
case hole:
3232
const current = value instanceof Hole ?
33-
unroll(s[i] || (s[i] = cache()), value) :
34-
(s[i] = null, value)
33+
unroll(detail.c || (detail.c = cache()), value) :
34+
(detail.c = null, value)
3535
;
3636
if (current !== previous)
3737
detail.v = hole.call(detail, target, current);
@@ -42,7 +42,7 @@ const unroll = (info, { s, t, v }) => {
4242
break;
4343
}
4444
}
45-
return info.n;
45+
return info.b;
4646
};
4747

4848
/**

esm/render/hole.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ const known = new WeakMap;
1515
*/
1616
export default (where, what) => {
1717
const info = known.get(where) || set(known, where, cache());
18-
if (info.n !== (typeof what === 'function' ? what() : what).toDOM(info))
19-
where.replaceChildren(info.n.valueOf());
18+
const { b } = info;
19+
if (b !== (typeof what === 'function' ? what() : what).toDOM(info))
20+
where.replaceChildren(info.b.valueOf());
2021
return where;
2122
};

esm/render/shared.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ const known = new WeakMap;
1414
*/
1515
export default (where, what, check) => {
1616
const info = known.get(where) || set(known, where, cache());
17+
const { b } = info;
1718
const hole = (check && typeof what === 'function') ? what() : what;
18-
const { n } = info;
1919
const node = hole instanceof Hole ? hole.toDOM(info) : hole;
20-
if (n !== node)
21-
where.replaceChildren((info.n = node).valueOf());
20+
if (b !== node)
21+
where.replaceChildren((info.b = node).valueOf());
2222
return where;
2323
};

0 commit comments

Comments
 (0)