Skip to content

Cache template digests #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 1 addition & 35 deletions src/lib/dom-shim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,41 +57,7 @@ class CustomElementRegistry {
}
}

// btoa Polyfill from https://github.com/MaxArt2501/base64-js/blob/master/base64.js
// base64 character set, plus padding character (=)
const b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';

const btoa = (string: string) => {
string = String(string);
let bitmap,
a,
b,
c,
result = '',
i = 0,
rest = string.length % 3; // To determine the final padding

for (; i < string.length; ) {
if (
(a = string.charCodeAt(i++)) > 255 ||
(b = string.charCodeAt(i++)) > 255 ||
(c = string.charCodeAt(i++)) > 255
)
throw new TypeError(
"Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range."
);

bitmap = (a << 16) | (b << 8) | c;
result +=
b64.charAt((bitmap >> 18) & 63) +
b64.charAt((bitmap >> 12) & 63) +
b64.charAt((bitmap >> 6) & 63) +
b64.charAt(bitmap & 63);
}

// If there's need of padding, replace the last 'A's with equal signs
return rest ? result.slice(0, rest - 3) + '==='.substring(rest) : result;
};
const btoa = (s: string) => Buffer.from(s, 'binary').toString('base64');

export const window = {
HTMLElement,
Expand Down
22 changes: 12 additions & 10 deletions src/lib/render-lit-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ declare module 'parse5' {
}
}

const templateCache = new Map<
TemplateStringsArray,
{
ops: Array<Op>;
}
>();
type Template = {
ops: Array<Op>;
digest: string;
};

const templateCache = new Map<TemplateStringsArray, Template>();

/**
* Operation to output static text
Expand Down Expand Up @@ -290,7 +290,7 @@ const getTemplate = (result: TemplateResult) => {
},
});
flushTo();
const t = {ops};
const t = {ops, digest: result.digest};
templateCache.set(result.strings, t);
return t;
};
Expand Down Expand Up @@ -331,8 +331,9 @@ export function* renderValue(
renderInfo: RenderInfo
): IterableIterator<string> {
if (value instanceof TemplateResult) {
yield `<!--lit-part ${value.digest}-->`;
yield* renderTemplateResult(value, renderInfo);
const template = getTemplate(value);
yield `<!--lit-part ${template.digest}-->`;
yield* renderTemplateResult(value, template, renderInfo);
} else {
yield `<!--lit-part-->`;
if (value === undefined || value === null) {
Expand Down Expand Up @@ -362,6 +363,7 @@ export function* renderValue(

export function* renderTemplateResult(
result: TemplateResult,
template: Template,
renderInfo: RenderInfo
): IterableIterator<string> {
// In order to render a TemplateResult we have to handle and stream out
Expand All @@ -379,7 +381,7 @@ export function* renderTemplateResult(
// elements. For each we will record the offset of the node, and output the
// previous span of HTML.

const {ops} = getTemplate(result);
const {ops} = template;

/* The next value in result.values to render */
let partIndex = 0;
Expand Down