1/** 2@license 3Copyright (c) 2017 The Polymer Project Authors. All rights reserved. 4This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt 5The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt 6The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt 7Code distributed by Google as part of the polymer project is also 8subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt 9*/ 10'use strict'; 11 12export default class StyleCache { 13 constructor(typeMax = 100) { 14 // map element name -> [{properties, styleElement, scopeSelector}] 15 this.cache = {}; 16 /** @type {number} */ 17 this.typeMax = typeMax; 18 } 19 20 _validate(cacheEntry, properties, ownPropertyNames) { 21 for (let idx = 0; idx < ownPropertyNames.length; idx++) { 22 let pn = ownPropertyNames[idx]; 23 if (cacheEntry.properties[pn] !== properties[pn]) { 24 return false; 25 } 26 } 27 return true; 28 } 29 30 store(tagname, properties, styleElement, scopeSelector) { 31 let list = this.cache[tagname] || []; 32 list.push({properties, styleElement, scopeSelector}); 33 if (list.length > this.typeMax) { 34 list.shift(); 35 } 36 this.cache[tagname] = list; 37 } 38 39 fetch(tagname, properties, ownPropertyNames) { 40 let list = this.cache[tagname]; 41 if (!list) { 42 return; 43 } 44 // reverse list for most-recent lookups 45 for (let idx = list.length - 1; idx >= 0; idx--) { 46 let entry = list[idx]; 47 if (this._validate(entry, properties, ownPropertyNames)) { 48 return entry; 49 } 50 } 51 } 52} 53