-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improved flow type #12
Changes from 1 commit
d219433
90d35b6
6485bb5
14e875c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
// @flow | ||
type EqualityFn = (a: any, b: any) => boolean; | ||
type EqualityFn = (a: mixed, b: mixed) => boolean; | ||
|
||
const simpleIsEqual = (a: any, b: any): boolean => a === b; | ||
const simpleIsEqual: EqualityFn = (a: mixed, b: mixed): boolean => a === b; | ||
|
||
export default function (resultFn: Function, isEqual?: EqualityFn = simpleIsEqual) { | ||
let lastThis: any; | ||
let lastArgs: Array<any> = []; | ||
let lastResult: any; | ||
export default function <ResultFn: (...Array<any>) => mixed>(resultFn: ResultFn, isEqual?: EqualityFn = simpleIsEqual): ResultFn { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @thejameskyle
Is that correct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup, that's right. While you're not enforcing inputs, you can at least be sure that you aren't handling the output incorrectly. |
||
let lastThis: mixed; | ||
let lastArgs: Array<mixed> = []; | ||
let lastResult: mixed; | ||
let calledOnce: boolean = false; | ||
|
||
const isNewArgEqualToLast = (newArg, index) => isEqual(newArg, lastArgs[index]); | ||
const isNewArgEqualToLast = (newArg: mixed, index: number): boolean => isEqual(newArg, lastArgs[index]); | ||
|
||
// breaking cache when context (this) or arguments change | ||
return function (...newArgs: Array<any>) { | ||
// breaking cache when context (this) or arguments change | ||
const result = function (...newArgs: Array<mixed>) { | ||
if (calledOnce && | ||
lastThis === this && | ||
newArgs.length === lastArgs.length && | ||
newArgs.every(isNewArgEqualToLast)) { | ||
lastThis === this && | ||
newArgs.length === lastArgs.length && | ||
newArgs.every(isNewArgEqualToLast)) { | ||
return lastResult; | ||
} | ||
|
||
|
@@ -26,4 +26,7 @@ export default function (resultFn: Function, isEqual?: EqualityFn = simpleIsEqua | |
lastResult = resultFn.apply(this, newArgs); | ||
return lastResult; | ||
}; | ||
} | ||
|
||
// telling flow to ignore the type of `result` as we know it is `ResultFn` | ||
return (result: any); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had added this also to ensure that memoizeOne will work with the upcoming flow version