-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Detect opportunity to insert new vnode earlier for more efficient DOM manipulation #1106
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,8 @@ import { | |
DestroyHook, | ||
UpdateHook, | ||
Key, | ||
fragment | ||
fragment, | ||
DOMAPI | ||
} from "../../src/index"; | ||
|
||
const hasSvgClassList = "classList" in SVGElement.prototype; | ||
|
@@ -837,6 +838,44 @@ describe("snabbdom", function () { | |
assert.equal(elm.children[1].innerHTML, "symbol"); | ||
assert.equal(elm.children[2].innerHTML, "symbol"); | ||
}); | ||
it("simultaneous addition in beginning and removal in end", function () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test also passed before this PR. I'm adding it for symmetry with the test below and to make sure that the non-inefficent case reported in #1099 is now tested. |
||
let insertCount = 0; | ||
const domApi: DOMAPI = { | ||
...htmlDomApi, | ||
insertBefore: (a, b, c) => { | ||
insertCount++; | ||
htmlDomApi.insertBefore(a, b, c); | ||
} | ||
}; | ||
const patch = init([], domApi); | ||
const vnode1 = h("span", [2, 3, 4, 5].map(spanNum)); | ||
const vnode2 = h("span", [1, 2, 3, 4].map(spanNum)); | ||
elm = patch(vnode0, vnode1).elm; | ||
assert.deepEqual(map(inner, elm.children), ["2", "3", "4", "5"]); | ||
elm = patch(vnode1, vnode2).elm; | ||
assert.deepEqual(map(inner, elm.children), ["1", "2", "3", "4"]); | ||
assert.strictEqual(insertCount, 1); | ||
}); | ||
it("simultaneous removal in beginning and addition in end", function () { | ||
let insertCount = 0; | ||
const domApi: DOMAPI = { | ||
...htmlDomApi, | ||
insertBefore: (a, b, c) => { | ||
insertCount++; | ||
htmlDomApi.insertBefore(a, b, c); | ||
} | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change has no effect on the resulting DOM, only on the process by which we get there. Hence the test wraps the |
||
const patch = init([], domApi); | ||
const vnode1 = h("span", [1, 2, 3, 4].map(spanNum)); | ||
const vnode2 = h("span", [2, 3, 4, 5].map(spanNum)); | ||
elm = patch(vnode0, vnode1).elm; | ||
assert.equal(elm.children.length, 4); | ||
assert.deepEqual(map(inner, elm.children), ["1", "2", "3", "4"]); | ||
elm = patch(vnode1, vnode2).elm; | ||
assert.equal(elm.children.length, 4); | ||
assert.deepEqual(map(inner, elm.children), ["2", "3", "4", "5"]); | ||
assert.strictEqual(insertCount, 1); | ||
}); | ||
}); | ||
it("reverses elements", function () { | ||
const vnode1 = h("span", [1, 2, 3, 4, 5, 6, 7, 8].map(spanNum)); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is unrelated to the PR but I just happened to improve this type as well.