1 |
- {"version":3,"file":"cache.js","sources":["../src/directives/cache.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2017 Google LLC\n * SPDX-License-Identifier: BSD-3-Clause\n */\n\nimport {\n TemplateResult,\n ChildPart,\n RootPart,\n render,\n nothing,\n} from '../lit-html.js';\nimport {\n directive,\n Directive,\n DirectiveParameters,\n PartInfo,\n} from '../directive.js';\nimport {\n clearPart,\n getCommittedValue,\n insertPart,\n isTemplateResult,\n setCommittedValue,\n} from '../directive-helpers.js';\n\nclass CacheDirective extends Directive {\n private _templateCache = new WeakMap<TemplateStringsArray, RootPart>();\n private _value?: TemplateResult;\n\n constructor(partInfo: PartInfo) {\n super(partInfo);\n }\n\n render(v: unknown) {\n // Return an array of the value to induce lit-html to create a ChildPart\n // for the value that we can move into the cache.\n return [v];\n }\n\n override update(containerPart: ChildPart, [v]: DirectiveParameters<this>) {\n // If the previous value is a TemplateResult and the new value is not,\n // or is a different Template as the previous value, move the child part\n // into the cache.\n if (\n isTemplateResult(this._value) &&\n (!isTemplateResult(v) || this._value.strings !== v.strings)\n ) {\n // This is always an array because we return [v] in render()\n const partValue = getCommittedValue(containerPart) as Array<ChildPart>;\n const childPart = partValue.pop()!;\n let cachedContainerPart = this._templateCache.get(this._value.strings);\n if (cachedContainerPart === undefined) {\n const fragment = document.createDocumentFragment();\n cachedContainerPart = render(nothing, fragment);\n cachedContainerPart.setConnected(false);\n this._templateCache.set(this._value.strings, cachedContainerPart);\n }\n // Move into cache\n setCommittedValue(cachedContainerPart, [childPart]);\n insertPart(cachedContainerPart, undefined, childPart);\n }\n // If the new value is a TemplateResult and the previous value is not,\n // or is a different Template as the previous value, restore the child\n // part from the cache.\n if (isTemplateResult(v)) {\n if (!isTemplateResult(this._value) || this._value.strings !== v.strings) {\n const cachedContainerPart = this._templateCache.get(v.strings);\n if (cachedContainerPart !== undefined) {\n // Move the cached part back into the container part value\n const partValue = getCommittedValue(\n cachedContainerPart\n ) as Array<ChildPart>;\n const cachedPart = partValue.pop()!;\n // Move cached part back into DOM\n clearPart(containerPart);\n insertPart(containerPart, undefined, cachedPart);\n setCommittedValue(containerPart, [cachedPart]);\n }\n }\n this._value = v;\n } else {\n this._value = undefined;\n }\n return this.render(v);\n }\n}\n\n/**\n * Enables fast switching between multiple templates by caching the DOM nodes\n * and TemplateInstances produced by the templates.\n *\n * Example:\n *\n * ```js\n * let checked = false;\n *\n * html`\n * ${cache(checked ? html`input is checked` : html`input is not checked`)}\n * `\n * ```\n */\nexport const cache = directive(CacheDirective);\n\n/**\n * The type of the class that powers this directive. Necessary for naming the\n * directive's return type.\n */\nexport type {CacheDirective};\n"],"names":["cache","directive","Directive","constructor","partInfo","super","this","WeakMap","render","v","update","containerPart","isTemplateResult","_value","strings","childPart","getCommittedValue","pop","cachedContainerPart","_templateCache","get","undefined","fragment","document","createDocumentFragment","nothing","setConnected","set","setCommittedValue","insertPart","cachedPart","clearPart"],"mappings":";;;;;SAuGaA,EAAQC,EA5ErB,cAA6BC,EAI3BC,YAAYC,GACVC,MAAMD,GAJAE,QAAiB,IAAIC,QAO7BC,OAAOC,GAGL,MAAO,CAACA,GAGDC,OAAOC,GAA2BF,IAIzC,GACEG,EAAiBN,KAAKO,OACpBD,EAAiBH,IAAMH,KAAKO,GAAOC,UAAYL,EAAEK,SACnD,CAEA,MACMC,EADYC,EAAkBL,GACRM,MAC5B,IAAIC,EAAsBZ,KAAKa,GAAeC,IAAId,KAAKO,GAAOC,SAC9D,QAA4BO,IAAxBH,EAAmC,CACrC,MAAMI,EAAWC,SAASC,yBAC1BN,EAAsBV,EAAOiB,EAASH,GACtCJ,EAAoBQ,cAAa,GACjCpB,KAAKa,GAAeQ,IAAIrB,KAAKO,GAAOC,QAASI,GAG/CU,EAAkBV,EAAqB,CAACH,IACxCc,EAAWX,OAAqBG,EAAWN,GAK7C,GAAIH,EAAiBH,GAAI,CACvB,IAAKG,EAAiBN,KAAKO,KAAWP,KAAKO,GAAOC,UAAYL,EAAEK,QAAS,CACvE,MAAMI,EAAsBZ,KAAKa,GAAeC,IAAIX,EAAEK,SACtD,QAA4BO,IAAxBH,EAAmC,CAErC,MAGMY,EAHYd,EAChBE,GAE2BD,MAE7Bc,EAAUpB,GACVkB,EAAWlB,OAAeU,EAAWS,GACrCF,EAAkBjB,EAAe,CAACmB,KAGtCxB,KAAKO,GAASJ,OAEdH,KAAKO,QAASQ,EAEhB,OAAOf,KAAKE,OAAOC"}
|