-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
112 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Original author: Alex Reardon | ||
* License: MIT License | ||
* Repo: https://github.com/alexreardon/memoize-one | ||
*/ | ||
|
||
function isEqual(first: unknown, second: unknown): boolean { | ||
if (first === second) { | ||
return true; | ||
} | ||
|
||
// Special case for NaN (NaN !== NaN) | ||
if (Number.isNaN(first) && Number.isNaN(second)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
export default function areInputsEqual( | ||
newInputs: readonly unknown[], | ||
lastInputs: readonly unknown[], | ||
): boolean { | ||
// no checks needed if the inputs length has changed | ||
if (newInputs.length !== lastInputs.length) { | ||
return false; | ||
} | ||
// Using for loop for speed. It generally performs better than array.every | ||
// https://github.com/alexreardon/memoize-one/pull/59 | ||
for (let i = 0; i < newInputs.length; i++) { | ||
if (!isEqual(newInputs[i], lastInputs[i])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* Original author: Alex Reardon | ||
* License: MIT License | ||
* Repo: https://github.com/alexreardon/memoize-one | ||
* Description: A memoization library which only remembers the latest invocation. | ||
*/ | ||
|
||
import areInputsEqual from './are-inputs-equal'; | ||
|
||
export type EqualityFn<TFunc extends (...args: any[]) => any> = ( | ||
newArgs: Parameters<TFunc>, | ||
lastArgs: Parameters<TFunc>, | ||
) => boolean; | ||
|
||
export interface MemoizedFn<TFunc extends (this: any, ...args: any[]) => any> { | ||
clear: () => void; | ||
( | ||
this: ThisParameterType<TFunc>, | ||
...args: Parameters<TFunc> | ||
): ReturnType<TFunc>; | ||
} | ||
|
||
// internal type | ||
interface Cache<TFunc extends (this: any, ...args: any[]) => any> { | ||
lastThis: ThisParameterType<TFunc>; | ||
lastArgs: Parameters<TFunc>; | ||
lastResult: ReturnType<TFunc>; | ||
} | ||
|
||
function memoizeOne<TFunc extends (this: any, ...newArgs: any[]) => any>( | ||
resultFn: TFunc, | ||
isEqual: EqualityFn<TFunc> = areInputsEqual, | ||
): MemoizedFn<TFunc> { | ||
let cache: Cache<TFunc> | null = null; | ||
|
||
// breaking cache when context (this) or arguments change | ||
function memoized( | ||
this: ThisParameterType<TFunc>, | ||
...newArgs: Parameters<TFunc> | ||
): ReturnType<TFunc> { | ||
if (cache && cache.lastThis === this && isEqual(newArgs, cache.lastArgs)) { | ||
return cache.lastResult; | ||
} | ||
|
||
// Throwing during an assignment aborts the assignment: https://codepen.io/alexreardon/pen/RYKoaz | ||
// Doing the lastResult assignment first so that if it throws | ||
// the cache will not be overwritten | ||
const lastResult = resultFn.apply(this, newArgs); | ||
cache = { | ||
lastResult, | ||
lastArgs: newArgs, | ||
lastThis: this, | ||
}; | ||
|
||
return lastResult; | ||
} | ||
|
||
// Adding the ability to clear the cache of a memoized function | ||
memoized.clear = function clear() { | ||
cache = null; | ||
}; | ||
|
||
return memoized; | ||
} | ||
|
||
export default memoizeOne; |
2 changes: 1 addition & 1 deletion
2
src/state/auto-scroller/fluid-scroller/get-best-scrollable-droppable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters