2
This commit is contained in:
41
normal-admin/node_modules/memoize-one/src/are-inputs-equal.ts
generated
vendored
Normal file
41
normal-admin/node_modules/memoize-one/src/are-inputs-equal.ts
generated
vendored
Normal file
@@ -0,0 +1,41 @@
|
||||
// Number.isNaN as it is not supported in IE11 so conditionally using ponyfill
|
||||
// Using Number.isNaN where possible as it is ~10% faster
|
||||
|
||||
const safeIsNaN =
|
||||
Number.isNaN ||
|
||||
function ponyfill(value: unknown): boolean {
|
||||
// // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN#polyfill
|
||||
// NaN is the only value in JavaScript which is not equal to itself.
|
||||
return typeof value === 'number' && value !== value;
|
||||
};
|
||||
|
||||
function isEqual(first: unknown, second: unknown): boolean {
|
||||
if (first === second) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Special case for NaN (NaN !== NaN)
|
||||
if (safeIsNaN(first) && safeIsNaN(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;
|
||||
}
|
||||
11
normal-admin/node_modules/memoize-one/src/memoize-one.js.flow
generated
vendored
Normal file
11
normal-admin/node_modules/memoize-one/src/memoize-one.js.flow
generated
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
// @flow
|
||||
|
||||
// These types are not as powerful as the TypeScript types, but they get the job done
|
||||
|
||||
export type EqualityFn = (newArgs: mixed[], lastArgs: mixed[]) => boolean;
|
||||
|
||||
// default export
|
||||
declare export default function memoizeOne<ResultFn: (...any[]) => mixed>(
|
||||
fn: ResultFn,
|
||||
isEqual?: EqualityFn,
|
||||
): ResultFn & { clear: () => void };
|
||||
56
normal-admin/node_modules/memoize-one/src/memoize-one.ts
generated
vendored
Normal file
56
normal-admin/node_modules/memoize-one/src/memoize-one.ts
generated
vendored
Normal file
@@ -0,0 +1,56 @@
|
||||
import areInputsEqual from './are-inputs-equal';
|
||||
|
||||
export type EqualityFn<TFunc extends (...args: any[]) => any> = (
|
||||
newArgs: Parameters<TFunc>,
|
||||
lastArgs: Parameters<TFunc>,
|
||||
) => boolean;
|
||||
|
||||
export type MemoizedFn<TFunc extends (this: any, ...args: any[]) => any> = {
|
||||
clear: () => void;
|
||||
(this: ThisParameterType<TFunc>, ...args: Parameters<TFunc>): ReturnType<TFunc>;
|
||||
};
|
||||
|
||||
// internal type
|
||||
type 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;
|
||||
Reference in New Issue
Block a user