Skip to content
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

Merged
merged 4 commits into from
May 15, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

[options]

experimental.strict_call_arity=true
Copy link
Owner Author

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


# Provides a way to suppress flow errors in the following line.
# Example: // $FlowFixMe: This following line is borked because of reasons.
suppress_comment= \\(.\\|\n\\)*\\$FlowFixMe
29 changes: 16 additions & 13 deletions src/index.js
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 {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thejameskyle <ResultFn: (...Array<any>) => mixed> seemed to work fine. I am still a little confused as to what this is communicating. Here is my current understanding:

ResultFn is a type of function (as communicated by the syntax() => {}) that can accept any length of arguments ((...Array<any>)) and returns a mixed (which means that you must check the result type before you can do type specific operations on it).

Is that correct?

Copy link
Contributor

Choose a reason for hiding this comment

The 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;
}

Expand All @@ -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);
}
18 changes: 18 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,5 +482,23 @@ describe('memoizeOne', () => {
expect(add.callCount).to.equal(2);
});
});

describe('flow typing', () => {
it('should maintain the type of the original function', () => {
// this test will create a flow error if the typing is incorrect
type SubtractFn = (a: number, b: number) => number;
const subtract: SubtractFn = (a: number, b: number): number => a - b;
const requiresASubtractFn = (fn: SubtractFn): number => fn(2, 1);

const memoizedSubtract: SubtractFn = memoizeOne(subtract);

// will cause a flow error if `fn` is not of type `SubtractFn`
const result1 = requiresASubtractFn(memoizedSubtract);
const result2 = requiresASubtractFn(memoizeOne(subtract));

expect(result1).to.equal(1);
expect(result2).to.equal(1);
});
});
});