|
1 | | -import { array, hole, text } from './handler.js'; |
| 1 | +import { array, hole } from './handler.js'; |
2 | 2 | import { cache as newCache } from './literals.js'; |
3 | 3 | import { empty } from './utils.js'; |
4 | 4 | import create from './creator.js'; |
5 | 5 | import parser from './parser.js'; |
6 | 6 |
|
| 7 | +const { assign } = Object; |
| 8 | + |
7 | 9 | const parseHTML = create(parser(false)); |
8 | 10 | const parseSVG = create(parser(true)); |
9 | 11 |
|
10 | | -const createCache = ({ u }) => ( |
11 | | - u === array ? |
12 | | - newCache([]) : ( |
13 | | - u === hole ? |
14 | | - newCache(empty) : |
15 | | - null |
16 | | - ) |
17 | | -); |
18 | | - |
19 | 12 | /** |
20 | 13 | * @param {import("./literals.js").Cache} cache |
21 | 14 | * @param {Hole} hole |
22 | 15 | * @returns {Node} |
23 | 16 | */ |
24 | | -export const unroll = (cache, { s, t, v }) => { |
25 | | - let i = 0, { d: details, s: stack } = cache; |
26 | | - if (cache.t !== t) { |
27 | | - const { n, d } = (s ? parseSVG : parseHTML)(t, v); |
28 | | - cache.t = t; |
29 | | - cache.n = n; |
30 | | - cache.d = (details = d); |
31 | | - if (v.length) cache.s = (stack = d.map(createCache)); |
32 | | - } |
33 | | - for (; i < details.length; i++) { |
| 17 | +const unroll = (cache, { s, t, v }) => { |
| 18 | + if (cache.t !== t) |
| 19 | + assign(cache, (s ? parseSVG : parseHTML)(t, v)); |
| 20 | + for (let { d, s } = cache, i = 0; i < d.length; i++) { |
34 | 21 | const value = v[i]; |
35 | | - const detail = details[i]; |
| 22 | + const detail = d[i]; |
36 | 23 | const { v: previous, u: update, t: target, n: name } = detail; |
37 | 24 | switch (update) { |
38 | 25 | case array: |
39 | 26 | detail.v = array( |
40 | 27 | target, |
41 | | - unrollValues(stack[i], value), |
| 28 | + unrollValues(s[i], value), |
42 | 29 | previous |
43 | 30 | ); |
44 | 31 | break; |
45 | 32 | case hole: |
46 | 33 | const current = value instanceof Hole ? |
47 | | - unroll(stack[i], value) : |
48 | | - value |
| 34 | + unroll(s[i] || (s[i] = newCache(empty)), value) : |
| 35 | + (s[i] = null, value) |
49 | 36 | ; |
50 | 37 | if (current !== previous) |
51 | 38 | detail.v = hole.call(detail, target, current); |
@@ -89,4 +76,7 @@ export class Hole { |
89 | 76 | this.t = template; |
90 | 77 | this.v = values; |
91 | 78 | } |
| 79 | + toDOM(cache = newCache(empty)) { |
| 80 | + return unroll(cache, this); |
| 81 | + } |
92 | 82 | }; |
0 commit comments