You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
exporttypeEqualityFn=(newArgs: unknown[],lastArgs: unknown[])=>boolean;functionareInputsEqual(newInputs: unknown[],lastInputs: unknown[],): boolean{// no checks needed if the inputs length has changedif(newInputs.length!==lastInputs.length){returnfalse;}// Using for loop for speed. It generally performs better than array.every// https://github.com/alexreardon/memoize-one/pull/59for(leti=0;i<newInputs.length;i++){// using shallow equality checkif(newInputs[i]!==lastInputs[i]){returnfalse;}}returntrue;}exportdefaultfunctionmemoizeOne<ResultFnextends(this: unknown, ...newArgs: unknown[])=>ReturnType<ResultFn>>(this: unknown,resultFn: ResultFn,isEqual: EqualityFn=areInputsEqual,): ResultFn{letlastThis: unknown;letlastArgs: unknown[]=[];letlastResult: ReturnType<ResultFn>;
let calledOnce: boolean = false;
// breaking cache when context (this) or arguments change
function memoized(this: unknown, ...newArgs: unknown[]): ReturnType<ResultFn>{if(calledOnce&&lastThis===this&&isEqual(newArgs,lastArgs)){returnlastResult;}
// 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(memoizedasResultFn);}
Here is what I am thinking so far:
playground
The text was updated successfully, but these errors were encountered: