Transform snippets into a map in the browser for faster access

This commit is contained in:
Andrew Kingston 2024-03-13 12:37:49 +00:00
parent 64855bbdf0
commit 861d48dbf3
1 changed files with 8 additions and 4 deletions

View File

@ -51,10 +51,16 @@ module.exports.processJS = (handlebars, context) => {
// This is required to allow the final `return` statement to be valid.
const js = iifeWrapper(atob(handlebars))
// Transform snippets into an object for faster access
let snippetMap = {}
for (let snippet of context.snippets || []) {
snippetMap[snippet.name] = snippet.code
}
// Our $ context function gets a value from context.
// We clone the context to avoid mutation in the binding affecting real
// app context.
const clonedContext = cloneDeep(context)
const clonedContext = cloneDeep({ ...context, snippets: null })
const sandboxContext = {
$: path => getContextValue(path, clonedContext),
helpers: getJsHelperList(),
@ -64,9 +70,7 @@ module.exports.processJS = (handlebars, context) => {
{},
{
get: function (_, name) {
// This will error if the snippet doesn't exist, but that's intended
const snippet = (context.snippets || []).find(x => x.name === name)
return eval(iifeWrapper(snippet.code))
return eval(iifeWrapper(snippetMap[name]))
},
}
),