1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-17 20:51:20 +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) { getFurthest(key, iterator) {
let node = this.assertDescendant(key) let ancestors = this.getAncestors(key)
let furthest = null if (!ancestors) {
throw new Error(`Could not find a descendant node with key "${key}".`)
while (node = this.getClosest(node, iterator)) {
furthest = node
} }
return furthest // Exclude this node itself
return ancestors.rest().find(iterator)
}, },
/** /**
@@ -1291,19 +1290,7 @@ const Node = {
} else { } else {
return result 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, [ memoize(Node, [
'assertChild', 'assertChild',
'assertDescendant', 'assertDescendant',
'filterDescendants',
'filterDescendantsDeep',
'findDescendant',
'findDescendantDeep',
'getAncestors', 'getAncestors',
'getBlocks', 'getBlocks',
'getBlocksAtRange', 'getBlocksAtRange',
@@ -1328,17 +1311,16 @@ memoize(Node, [
'getChildrenBeforeIncluding', 'getChildrenBeforeIncluding',
'getChildrenBetween', 'getChildrenBetween',
'getChildrenBetweenIncluding', 'getChildrenBetweenIncluding',
'getClosest',
'getClosestBlock', 'getClosestBlock',
'getClosestInline', 'getClosestInline',
'getComponent', 'getComponent',
'getDecorators', 'getDecorators',
'getDepth', 'getDepth',
'getDescendant', 'getDescendant',
'getDescendantAtPath',
'getDescendantDecorators', 'getDescendantDecorators',
'getFirstText', 'getFirstText',
'getFragmentAtRange', 'getFragmentAtRange',
'getFurthest',
'getFurthestBlock', 'getFurthestBlock',
'getFurthestInline', 'getFurthestInline',
'getHighestChild', 'getHighestChild',
@@ -1362,8 +1344,7 @@ memoize(Node, [
'hasChild', 'hasChild',
'hasDescendant', 'hasDescendant',
'hasVoidParent', 'hasVoidParent',
'isInlineSplitAtRange', 'isInlineSplitAtRange'
'validate'
]) ])
/** /**

View File

@@ -328,18 +328,6 @@ class Text extends new Record(DEFAULTS) {
return this.merge({ characters }) 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, [ memoize(Text.prototype, [
'getDecorations', 'getDecorations',
'getDecorators', 'getDecorators',
'getRanges', 'getRanges'
'validate',
]) ])
/** /**

View File

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

View File

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