Improved SVG icon performance

This commit is contained in:
Eduard Urbach 2019-08-31 20:29:03 +09:00
parent 3ef41b1cdd
commit 619fe67cc5
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0

View File

@ -1,7 +1,7 @@
import Diff from "scripts/Diff" import Diff from "scripts/Diff"
export default class SVGIcon extends HTMLElement { export default class SVGIcon extends HTMLElement {
static cache = new Map<string, Promise<string>>() static cache = new Map<string, Promise<Node>>()
static get observedAttributes() { static get observedAttributes() {
return ["name"] return ["name"]
@ -17,8 +17,18 @@ export default class SVGIcon extends HTMLElement {
let cache = SVGIcon.cache.get(this.name) let cache = SVGIcon.cache.get(this.name)
if(cache) { if(cache) {
let text = await cache let svg = await cache
Diff.mutations.queue(() => this.innerHTML = text)
Diff.mutations.queue(() => {
// Remove all existing child nodes
while(this.firstChild) {
this.removeChild(this.firstChild)
}
// Append a clone of the cached SVG node
this.appendChild(svg.cloneNode(true))
})
return return
} }
@ -33,8 +43,18 @@ export default class SVGIcon extends HTMLElement {
} }
let text = await response.text() let text = await response.text()
Diff.mutations.queue(() => this.innerHTML = text)
resolve(text) Diff.mutations.queue(() => {
this.innerHTML = text
let svg = this.firstChild
if(!svg) {
console.warn("Invalid SVG icon:", svg)
return
}
resolve(svg)
})
})) }))
} }