Skip to content

Commit

Permalink
fix: duplicate render by using value cache key
Browse files Browse the repository at this point in the history
not a reference cache key related muxinc#517
  • Loading branch information
luwes committed Dec 12, 2022
1 parent 8ee139d commit 031a89a
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions packages/mux-player/src/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,24 +108,29 @@ export function processPart(part: Part, value: unknown): void {
processPropertyIdentity(part, value);
}

const templates = new WeakMap<TemplateStringsArray, HTMLTemplateElement>();
const templates = new Map<string, HTMLTemplateElement>();
const renderedTemplates = new WeakMap<Node | ChildNodePart, HTMLTemplateElement>();
const renderedTemplateInstances = new WeakMap<Node | ChildNodePart, TemplateInstance>();
export class TemplateResult {
public readonly stringsKey: string;

constructor(
public readonly strings: TemplateStringsArray,
public readonly values: unknown[],
public readonly processor: any
) {}
) {
// Use a control character to join the expression boundaries.
this.stringsKey = this.strings.join('\x01');
}

get template(): HTMLTemplateElement {
if (templates.has(this.strings)) {
return templates.get(this.strings) as HTMLTemplateElement;
if (templates.has(this.stringsKey)) {
return templates.get(this.stringsKey) as HTMLTemplateElement;
} else {
const template = document.createElement('template');
const end = this.strings.length - 1;
template.innerHTML = this.strings.reduce((str, cur, i) => str + cur + (i < end ? `{{ ${i} }}` : ''), '');
templates.set(this.strings, template);
templates.set(this.stringsKey, template);
return template;
}
}
Expand Down

0 comments on commit 031a89a

Please sign in to comment.