diff options
Diffstat (limited to 'bin/wiki/ImportarDesdeURL/node_modules/memoize-one/src/index.js')
-rw-r--r-- | bin/wiki/ImportarDesdeURL/node_modules/memoize-one/src/index.js | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/bin/wiki/ImportarDesdeURL/node_modules/memoize-one/src/index.js b/bin/wiki/ImportarDesdeURL/node_modules/memoize-one/src/index.js new file mode 100644 index 00000000..44eae7a6 --- /dev/null +++ b/bin/wiki/ImportarDesdeURL/node_modules/memoize-one/src/index.js @@ -0,0 +1,38 @@ +// @flow +import areInputsEqual from './are-inputs-equal'; + +export type EqualityFn = (newArgs: mixed[], lastArgs: mixed[]) => boolean; + +// <ResultFn: (...any[]) => mixed> +// The purpose of this typing is to ensure that the returned memoized +// function has the same type as the provided function (`resultFn`). +// ResultFn: Generic type (which is the same as the resultFn). +// (...any[]): Accepts any length of arguments - and they are not checked +// mixed: The result can be anything but needs to be checked before usage +export default function memoizeOne<ResultFn: (...any[]) => mixed>( + resultFn: ResultFn, + isEqual?: EqualityFn = areInputsEqual, +): ResultFn { + let lastThis: mixed; + let lastArgs: mixed[] = []; + let lastResult: mixed; + let calledOnce: boolean = false; + + // breaking cache when context (this) or arguments change + const result = function memoized(...newArgs: mixed[]) { + if (calledOnce && lastThis === this && isEqual(newArgs, lastArgs)) { + return lastResult; + } + + // Throwing during an assignment aborts the assignment: https://codepen.io/alexreardon/pen/RYKoaz + // Doing the lastResult assignment first so that if it throws + // nothing will be overwritten + lastResult = resultFn.apply(this, newArgs); + calledOnce = true; + lastThis = this; + lastArgs = newArgs; + return lastResult; + }; + + return (result: any); +} |