UNPKG

es5-lit-html

Version:
65 lines (50 loc) 2.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.templateFactory = templateFactory; exports.templateCaches = void 0; var _template = require("./template.js"); /** * @license * Copyright (c) 2017 The Polymer Project Authors. All rights reserved. * This code may only be used under the BSD style license found at * http://polymer.github.io/LICENSE.txt * The complete set of authors may be found at * http://polymer.github.io/AUTHORS.txt * The complete set of contributors may be found at * http://polymer.github.io/CONTRIBUTORS.txt * Code distributed by Google as part of the polymer project is also * subject to an additional IP rights grant found at * http://polymer.github.io/PATENTS.txt */ /** * The default TemplateFactory which caches Templates keyed on * result.type and result.strings. */ function templateFactory(result) { var templateCache = templateCaches.get(result.type); if (templateCache === undefined) { templateCache = { stringsArray: new WeakMap(), keyString: new Map() }; templateCaches.set(result.type, templateCache); } var template = templateCache.stringsArray.get(result.strings); if (template !== undefined) { return template; } // If the TemplateStringsArray is new, generate a key from the strings // This key is shared between all templates with identical content var key = result.strings.join(_template.marker); // Check if we already have a Template for this key template = templateCache.keyString.get(key); if (template === undefined) { // If we have not seen this key before, create a new Template template = new _template.Template(result, result.getTemplateElement()); // Cache the Template for this key templateCache.keyString.set(key, template); } // Cache all future queries for this TemplateStringsArray templateCache.stringsArray.set(result.strings, template); return template; } var templateCaches = new Map(); exports.templateCaches = templateCaches;