修改模板缓存逻辑

dev
yutent 2023-08-11 17:42:27 +08:00
parent 289c7142ef
commit 5748e6a760
1 changed files with 11 additions and 8 deletions

View File

@ -48,7 +48,7 @@ const EVENT_PART = 5
const ELEMENT_PART = 6 const ELEMENT_PART = 6
const COMMENT_PART = 7 const COMMENT_PART = 7
const templateCache = new WeakMap() const TEMPLATE_CACHE = new Map()
const walker = document.createTreeWalker(document, 129, null, false) const walker = document.createTreeWalker(document, 129, null, false)
function noop() {} function noop() {}
@ -436,10 +436,11 @@ class ChildPart {
} }
} }
#getTemplate(result) { #getTemplate(result) {
let template = templateCache.get(result.strings) let template = TEMPLATE_CACHE.get(result.strings.join())
if (template === void 0) { if (template === void 0) {
template = new Template(result, this.options) template = new Template(result, this.options)
templateCache.set(result.strings, template) TEMPLATE_CACHE.set(result.strings.join(), template)
} }
return template return template
} }
@ -701,12 +702,13 @@ export function render(value, container, options = {}) {
let part = container[WC_PART] let part = container[WC_PART]
if (part === void 0) { if (part === void 0) {
container[WC_PART] = part = new ChildPart( part = new ChildPart(
container.insertBefore(createMarker(), null), container.insertBefore(createMarker(), null),
null, null,
void 0, void 0,
options options
) )
container[WC_PART] = part
} }
part._$setValue(value) part._$setValue(value)
@ -720,10 +722,11 @@ export function html(strings, ...values) {
values values
} }
} }
export function raw(str, ...args) {
let strings = args.length ? str.split('%s') : [str] export function raw(str, values = []) {
strings.raw = strings let strings = values.length ? str.split('%s') : [str]
return html(strings, ...args) strings.raw = true
return html(strings, ...values)
} }
export function svg(strings, ...values) { export function svg(strings, ...values) {
return { return {