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