Skip to content

Commit

Permalink
Merge pull request #7 from github/refactor-move-render-logic-into-cla…
Browse files Browse the repository at this point in the history
…ss-renderinto

refactor: move `render()` logic into class `renderInto()`
  • Loading branch information
keithamus authored Jan 19, 2021
2 parents 469b5d5 + c768956 commit 1f7f3be
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {TemplatePart, TemplateTypeInit} from '@github/template-parts'

function processSubTemplate(part: TemplatePart, value: unknown): boolean {
if (value instanceof TemplateResult && part instanceof NodeTemplatePart) {
render(value, part)
value.renderInto(part)
return true
}
return false
Expand All @@ -36,7 +36,7 @@ function processIterable(part: TemplatePart, value: unknown): boolean {
for (const item of value) {
if (item instanceof TemplateResult) {
const fragment = document.createDocumentFragment()
render(item, fragment)
item.renderInto(fragment)
nodes.push(...fragment.children)
} else if (item instanceof DocumentFragment) {
nodes.push(...item.children)
Expand All @@ -63,6 +63,8 @@ export function processPart(part: TemplatePart, value: unknown): void {
}

const templates = new WeakMap<TemplateStringsArray, HTMLTemplateElement>()
const renderedTemplates = new WeakMap<Node | NodeTemplatePart, HTMLTemplateElement>()
const renderedTemplateInstances = new WeakMap<Node | NodeTemplatePart, TemplateInstance>()
export class TemplateResult {
constructor(
public readonly strings: TemplateStringsArray,
Expand All @@ -81,25 +83,28 @@ export class TemplateResult {
return template
}
}

renderInto(element: Node | NodeTemplatePart): void {
const template = this.template
if (renderedTemplates.get(element) !== template) {
renderedTemplates.set(element, template)
const instance = new TemplateInstance(template, this.values, this.processor)
renderedTemplateInstances.set(element, instance)
if (element instanceof NodeTemplatePart) {
element.replace(...instance.children)
} else {
element.appendChild(instance)
}
}
renderedTemplateInstances.get(element)!.update((this.values as unknown) as Record<string, unknown>)
}
}

const defaultProcessor = createProcessor(processPart)
export function html(strings: TemplateStringsArray, ...values: unknown[]): TemplateResult {
return new TemplateResult(strings, values, defaultProcessor)
}

const renderedTemplates = new WeakMap<Node | NodeTemplatePart, HTMLTemplateElement>()
const renderedTemplateInstances = new WeakMap<Node | NodeTemplatePart, TemplateInstance>()
export function render(result: TemplateResult, element: Node | NodeTemplatePart): void {
if (renderedTemplates.get(element) !== result.template) {
renderedTemplates.set(element, result.template)
const instance = new TemplateInstance(result.template, result.values, result.processor)
renderedTemplateInstances.set(element, instance)
if (element instanceof NodeTemplatePart) {
element.replace(...instance.children)
} else {
element.appendChild(instance)
}
}
renderedTemplateInstances.get(element)!.update((result.values as unknown) as Record<string, unknown>)
result.renderInto(element)
}

0 comments on commit 1f7f3be

Please sign in to comment.