1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-16 12:14:14 +02:00

Merge branch 'schema-normalize-memoization' into schema-normalize

This commit is contained in:
Soreine
2016-11-01 16:33:14 +01:00
4 changed files with 30 additions and 44 deletions

View File

@@ -524,14 +524,13 @@ const Node = {
*/
getFurthest(key, iterator) {
let node = this.assertDescendant(key)
let furthest = null
while (node = this.getClosest(node, iterator)) {
furthest = node
let ancestors = this.getAncestors(key)
if (!ancestors) {
throw new Error(`Could not find a descendant node with key "${key}".`)
}
return furthest
// Exclude this node itself
return ancestors.rest().find(iterator)
},
/**
@@ -1291,19 +1290,7 @@ const Node = {
} else {
return result
}
},
/**
* Validate the node against a `schema`.
*
* @param {Schema} schema
* @return {Object || Void}
*/
validate(schema) {
return schema.__validate(this)
}
}
/**
@@ -1313,10 +1300,6 @@ const Node = {
memoize(Node, [
'assertChild',
'assertDescendant',
'filterDescendants',
'filterDescendantsDeep',
'findDescendant',
'findDescendantDeep',
'getAncestors',
'getBlocks',
'getBlocksAtRange',
@@ -1328,17 +1311,16 @@ memoize(Node, [
'getChildrenBeforeIncluding',
'getChildrenBetween',
'getChildrenBetweenIncluding',
'getClosest',
'getClosestBlock',
'getClosestInline',
'getComponent',
'getDecorators',
'getDepth',
'getDescendant',
'getDescendantAtPath',
'getDescendantDecorators',
'getFirstText',
'getFragmentAtRange',
'getFurthest',
'getFurthestBlock',
'getFurthestInline',
'getHighestChild',
@@ -1362,8 +1344,7 @@ memoize(Node, [
'hasChild',
'hasDescendant',
'hasVoidParent',
'isInlineSplitAtRange',
'validate'
'isInlineSplitAtRange'
])
/**

View File

@@ -328,18 +328,6 @@ class Text extends new Record(DEFAULTS) {
return this.merge({ characters })
}
/**
* Validate the node against a `schema`.
*
* @param {Schema} schema
* @return {Object || Void}
*/
validate(schema) {
return schema.__validate(this)
}
}
/**
@@ -349,8 +337,7 @@ class Text extends new Record(DEFAULTS) {
memoize(Text.prototype, [
'getDecorations',
'getDecorators',
'getRanges',
'validate',
'getRanges'
])
/**

View File

@@ -105,11 +105,19 @@ export function normalizeNodeWith(transform, schema, node, prevNode) {
return transform
}
// For performance considerations, we will check if the transform was changed
const opCount = transform.operations.length
// Iterate over its children
transform = _normalizeChildrenWith(transform, schema, node, prevNode)
// Refresh the node reference, and normalize it
const hasChanged = transform.operations.length != opCount
if (hasChanged) {
// Refresh the node reference
node = _refreshNode(transform, node)
}
// Now normalize the node itself if it still exist
if (node) {
transform = _normalizeNodeWith(transform, schema, node)
}
@@ -160,8 +168,8 @@ export function normalizeWith(transform, schema, prevDocument) {
const { state } = transform
const { document } = state
// Schema was not rule to edit the document
if (!schema.isNormalization) {
// Schema has no normalization rules
return transform
}

View File

@@ -11,6 +11,13 @@ import { Map } from 'immutable'
const LEAF = {}
/**
* An unique value used to detect cache misses
*
* @type {Object}
*/
const NO_SET = {}
/**
* Memoize all of the `properties` on a `object`.
*
@@ -31,7 +38,10 @@ function memoize(object, properties) {
const keys = [property, ...args, LEAF]
const cache = this.__cache = this.__cache || new Map()
if (cache.hasIn(keys)) return cache.getIn(keys)
const cachedValue = cache.getIn(keys, NO_SET)
if (cachedValue !== NO_SET) {
return cachedValue
}
const value = original.apply(this, args)
this.__cache = cache.setIn(keys, value)