Added mutation queues

This commit is contained in:
2017-07-05 21:06:38 +02:00
parent 44803adc2d
commit 4a76c12803
3 changed files with 60 additions and 14 deletions

View File

@ -1,4 +1,18 @@
export class Diff {
// Reuse container for diffs to avoid memory allocation
static container: HTMLElement
// innerHTML will diff the element with the given HTML string and apply DOM mutations.
static innerHTML(aRoot: HTMLElement, html: string) {
if(!Diff.container) {
Diff.container = document.createElement("main")
}
Diff.container.innerHTML = html
Diff.childNodes(aRoot, Diff.container)
}
// childNodes diffs the child nodes of 2 given elements and applies DOM mutations.
static childNodes(aRoot: Node, bRoot: Node) {
let aChild = [...aRoot.childNodes]
let bChild = [...bRoot.childNodes]
@ -33,6 +47,7 @@ export class Diff {
let elemA = a as HTMLElement
let elemB = b as HTMLElement
// Skip iframes
if(elemA.tagName === "IFRAME") {
continue
}
@ -75,11 +90,4 @@ export class Diff {
Diff.childNodes(a, b)
}
}
static innerHTML(aRoot: HTMLElement, html: string) {
let bRoot = document.createElement("main")
bRoot.innerHTML = html
Diff.childNodes(aRoot, bRoot)
}
}