1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-21 06:31:28 +02:00

Improve performances of "getPreviousSibling" and "getNextSibling" with memoization

This commit is contained in:
Samy Pesse
2016-11-02 23:28:03 +01:00
parent 91f481ba8d
commit ca67684bea

View File

@@ -670,12 +670,16 @@ const Node = {
*/ */
getNextSibling(key) { getNextSibling(key) {
const node = this.assertDescendant(key) key = Normalize.key(key)
return this
.getParent(node) const parent = this.getParent(key)
.nodes const after = parent.nodes
.skipUntil(child => child == node) .skipUntil(child => child.key == key)
.get(1)
if (after.size == 0) {
throw new Error(`Could not find a child node with key "${key}".`)
}
return after.get(1)
}, },
/** /**
@@ -822,12 +826,16 @@ const Node = {
*/ */
getPreviousSibling(key) { getPreviousSibling(key) {
const node = this.assertDescendant(key) key = Normalize.key(key)
return this const parent = this.getParent(key)
.getParent(node) const before = parent.nodes
.nodes .takeUntil(child => child.key == key)
.takeUntil(child => child == node)
.last() if (before.size == parent.nodes.size) {
throw new Error(`Could not find a child node with key "${key}".`)
}
return before.last()
}, },
/** /**