1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-22 15:02:51 +02:00

Improve memorize interface (#1713)

* Remove takesArguments

* Remove codes of restoring func.length

* Alphabeta fix

* Remove un-necessary code

* Remove un-necessary annotation
This commit is contained in:
Jinxuan Zhu
2018-03-23 13:53:02 -04:00
committed by Ian Storm Taylor
parent 8478c0291b
commit 4422f8afca
6 changed files with 75 additions and 98 deletions

View File

@@ -214,9 +214,7 @@ Mark.prototype[MODEL_TYPES.MARK] = true
* Memoize read methods.
*/
memoize(Mark.prototype, ['getComponent'], {
takesArguments: true,
})
memoize(Mark.prototype, ['getComponent'])
/**
* Export.

View File

@@ -2023,77 +2023,62 @@ function assertKey(arg) {
* Memoize read methods.
*/
memoize(
Node.prototype,
[
'getBlocksAsArray',
'getCharactersAsArray',
'getFirstText',
'getInlinesAsArray',
'getKeysAsArray',
'getLastText',
'getMarksAsArray',
'getText',
'getTextDirection',
'getTextsAsArray',
'isLeafBlock',
'isLeafInline',
],
{
takesArguments: false,
}
)
memoize(
Node.prototype,
[
'areDescendantsSorted',
'getActiveMarksAtRangeAsArray',
'getAncestors',
'getBlocksAtRangeAsArray',
'getBlocksByTypeAsArray',
'getCharactersAtRangeAsArray',
'getChild',
'getClosestBlock',
'getClosestInline',
'getClosestVoid',
'getCommonAncestor',
'getDecorations',
'getDepth',
'getDescendant',
'getDescendantAtPath',
'getFragmentAtRange',
'getFurthestBlock',
'getFurthestInline',
'getFurthestAncestor',
'getFurthestOnlyChildAncestor',
'getInlinesAtRangeAsArray',
'getInlinesByTypeAsArray',
'getMarksAtRangeAsArray',
'getInsertMarksAtRangeAsArray',
'getMarksByTypeAsArray',
'getNextBlock',
'getNextSibling',
'getNextText',
'getNode',
'getNodeAtPath',
'getOffset',
'getOffsetAtRange',
'getParent',
'getPath',
'getPlaceholder',
'getPreviousBlock',
'getPreviousSibling',
'getPreviousText',
'getTextAtOffset',
'getTextsAtRangeAsArray',
'validate',
'getFirstInvalidDescendant',
],
{
takesArguments: true,
}
)
memoize(Node.prototype, [
'areDescendantsSorted',
'getActiveMarksAtRangeAsArray',
'getAncestors',
'getBlocksAsArray',
'getBlocksAtRangeAsArray',
'getBlocksByTypeAsArray',
'getCharactersAtRangeAsArray',
'getCharactersAsArray',
'getChild',
'getClosestBlock',
'getClosestInline',
'getClosestVoid',
'getCommonAncestor',
'getDecorations',
'getDepth',
'getDescendant',
'getDescendantAtPath',
'getFirstText',
'getFragmentAtRange',
'getFurthestBlock',
'getFurthestInline',
'getFurthestAncestor',
'getFurthestOnlyChildAncestor',
'getInlinesAsArray',
'getInlinesAtRangeAsArray',
'getInlinesByTypeAsArray',
'getMarksAsArray',
'getMarksAtRangeAsArray',
'getInsertMarksAtRangeAsArray',
'getKeysAsArray',
'getLastText',
'getMarksByTypeAsArray',
'getNextBlock',
'getNextSibling',
'getNextText',
'getNode',
'getNodeAtPath',
'getOffset',
'getOffsetAtRange',
'getParent',
'getPath',
'getPlaceholder',
'getPreviousBlock',
'getPreviousSibling',
'getPreviousText',
'getText',
'getTextAtOffset',
'getTextDirection',
'getTextsAsArray',
'getTextsAtRangeAsArray',
'isLeafBlock',
'isLeafInline',
'validate',
'getFirstInvalidDescendant',
])
/**
* Export.

View File

@@ -584,9 +584,7 @@ Schema.prototype[MODEL_TYPES.SCHEMA] = true
* Memoize read methods.
*/
memoize(Schema.prototype, ['getParentRules'], {
takesArguments: true,
})
memoize(Schema.prototype, ['getParentRules'])
/**
* Export.

View File

@@ -159,9 +159,7 @@ Stack.prototype[MODEL_TYPES.STACK] = true
* Memoize read methods.
*/
memoize(Stack.prototype, ['getPluginsWith'], {
takesArguments: true,
})
memoize(Stack.prototype, ['getPluginsWith'])
/**
* Export.

View File

@@ -523,19 +523,13 @@ memoize(Text.prototype, ['getMarks', 'getMarksAsArray'], {
takesArguments: false,
})
memoize(
Text.prototype,
[
'getDecoratedCharacters',
'getDecorations',
'getLeaves',
'getMarksAtIndex',
'validate',
],
{
takesArguments: true,
}
)
memoize(Text.prototype, [
'getDecoratedCharacters',
'getDecorations',
'getLeaves',
'getMarksAtIndex',
'validate',
])
/**
* Export.

View File

@@ -60,9 +60,7 @@ const UNSET = undefined
* @return {Record}
*/
function memoize(object, properties, options = {}) {
const { takesArguments = true } = options
function memoize(object, properties) {
for (const property of properties) {
const original = object[property]
@@ -79,12 +77,18 @@ function memoize(object, properties, options = {}) {
if (CACHE_KEY !== this.__cache_key) {
this.__cache_key = CACHE_KEY
this.__cache = new Map() // eslint-disable-line no-undef,no-restricted-globals
this.__cache_no_args = {}
}
}
if (!this.__cache) {
this.__cache = new Map() // eslint-disable-line no-undef,no-restricted-globals
}
if (!this.__cache_no_args) {
this.__cache_no_args = {}
}
const takesArguments = args.length !== 0
let cachedValue
let keys
@@ -93,7 +97,7 @@ function memoize(object, properties, options = {}) {
keys = [property, ...args]
cachedValue = getIn(this.__cache, keys)
} else {
cachedValue = this.__cache.get(property)
cachedValue = this.__cache_no_args[property]
}
// If we've got a result already, return it.
@@ -108,7 +112,7 @@ function memoize(object, properties, options = {}) {
if (takesArguments) {
this.__cache = setIn(this.__cache, keys, v)
} else {
this.__cache.set(property, v)
this.__cache_no_args[property] = v
}
return value