|
| 1 | +//@ts-check |
| 2 | + |
| 3 | +import DEBUG from '../debug.js'; |
| 4 | + |
| 5 | +//@ts-ignore |
| 6 | +import { effectScope } from '@webreflection/alien-signals'; |
| 7 | +export { signal, computed, effect, untracked, batch } from './signals.js'; |
| 8 | + |
| 9 | +import { |
| 10 | + Comment, |
| 11 | + DocumentType, |
| 12 | + Text, |
| 13 | + Fragment, |
| 14 | + Element, |
| 15 | + Component, |
| 16 | +} from './ish.js'; |
| 17 | + |
| 18 | +import parser from '../parser/index.js'; |
| 19 | +import { Hole, dom } from './rabbit.js'; |
| 20 | +import { Keyed } from './keyed.js'; |
| 21 | +import { isKeyed, fragment, update } from './update.js'; |
| 22 | +import { diffFragment } from './persistent-fragment.js'; |
| 23 | + |
| 24 | +import { _get as getDirect, _set as setDirect } from './direct.js'; |
| 25 | + |
| 26 | +import { unsafe } from '../utils.js'; |
| 27 | +export { Hole, fragment, unsafe }; |
| 28 | + |
| 29 | +/** @typedef {globalThis.Element | globalThis.HTMLElement | globalThis.SVGSVGElement | globalThis.DocumentFragment} Container */ |
| 30 | + |
| 31 | +const parse = parser({ |
| 32 | + Comment, |
| 33 | + DocumentType, |
| 34 | + Text, |
| 35 | + Fragment, |
| 36 | + Element, |
| 37 | + Component, |
| 38 | + update, |
| 39 | +}); |
| 40 | + |
| 41 | +/** |
| 42 | + * @param {boolean} xml |
| 43 | + * @param {WeakMap<TemplateStringsArray | string[], [any, any[], Keyed?]>} twm |
| 44 | + * @returns |
| 45 | + */ |
| 46 | +const create = (xml, twm = new WeakMap) => |
| 47 | + /** |
| 48 | + * @param {TemplateStringsArray | string[]} template |
| 49 | + * @param {unknown[]} values |
| 50 | + * @returns {Hole} |
| 51 | + */ |
| 52 | + (template, ...values) => { |
| 53 | + let parsed = twm.get(template); |
| 54 | + if (!parsed) { |
| 55 | + parsed = parse(template, values, xml); |
| 56 | + parsed.push(isKeyed() ? new Keyed : null); |
| 57 | + if (DEBUG) parsed.push(template); |
| 58 | + parsed[0] = fragment(parsed[0].toString(), xml); |
| 59 | + twm.set(template, parsed); |
| 60 | + } |
| 61 | + return new Hole(parsed, values); |
| 62 | + }; |
| 63 | + |
| 64 | +const htmlHole = create(false); |
| 65 | +const svgHole = create(true); |
| 66 | + |
| 67 | +const rendered = new WeakMap; |
| 68 | + |
| 69 | +/** |
| 70 | + * @param {TemplateStringsArray | string[]} template |
| 71 | + * @param {any[]} values |
| 72 | + * @returns {Node | HTMLElement | Hole} |
| 73 | + */ |
| 74 | +export function html(template, ...values) { |
| 75 | + const hole = htmlHole.apply(null, arguments); |
| 76 | + return getDirect() ? hole.valueOf(true) : hole; |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * @param {TemplateStringsArray | string[]} template |
| 81 | + * @param {any[]} values |
| 82 | + * @returns {Node | SVGSVGElement | Hole} |
| 83 | + */ |
| 84 | +export function svg(template, ...values) { |
| 85 | + const hole = svgHole.apply(null, arguments); |
| 86 | + return getDirect() ? hole.valueOf(true) : hole; |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * @param {Container} where |
| 91 | + * @param {Function | Node | Container} what |
| 92 | + * @returns |
| 93 | + */ |
| 94 | +export const render = (where, what) => { |
| 95 | + const known = rendered.get(where); |
| 96 | + if (known) known[0](); |
| 97 | + if (typeof what === 'function') { |
| 98 | + setDirect(false); |
| 99 | + let hole; |
| 100 | + const scope = effectScope(() => { hole = what() }); |
| 101 | + //@ts-ignore |
| 102 | + if (!known || known[1].t !== hole.t) { |
| 103 | + //@ts-ignore |
| 104 | + const d = hole.valueOf(false); |
| 105 | + where.replaceChildren(d); |
| 106 | + } |
| 107 | + else known[1].update(hole); |
| 108 | + rendered.set(where, [scope, hole]); |
| 109 | + } |
| 110 | + else { |
| 111 | + setDirect(true); |
| 112 | + rendered.delete(where); |
| 113 | + where.replaceChildren(what instanceof Hole ? dom(what) : diffFragment(what, 1)); |
| 114 | + } |
| 115 | + return where; |
| 116 | +}; |
0 commit comments