-
-
Notifications
You must be signed in to change notification settings - Fork 54
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
Warn when cacheKey
is unset and function.length > 1
?
#90
Comments
What if the parameter is there but does not change, so does not affect memoization? I don't think we can reliably detect incorrect usage here. And |
I think you're talking about, say, a function that accepts a What the caller does with the function is irrelevant. Since memoization wraps the function, it should follow the function’s logic, not the caller’s. e.g. export const normal = fetch;
export const memoized = mem(fetch); If a function accepts a parameter, why would it "not change"? If that exists, it sounds like an exception.
Agreed, but it sounds like a good warning and it's easy to silence: console.warn(`
Function accepts 2 parameters, but only one is considered during mem.
Use \`cacheKey: ([x]) => x\` to silence this message or refer to the docs:
https://github.com/sindresorhus/mem#caching-strategy
`) mem(async (host, path) => fetch(host + path), {
+ cacheKey: ([x]) => x
}) |
For example, an API with a parameter where you always pass in the same value as you only use it that way. Yes, the correct way would be to set a custom cache key, but since the parameter is practically static, it feels moot. |
I'm ok with |
Side quest: detecting functions with optional parameters/rest.
await memQuantumSum(12, 45, 71) I think however this requires a whole JS parser though 😰 so, no. |
Wait a minute. TypeScript to the rescue? This is right up its alley and better than a runtime warning. |
I think
mem
should callconsole.warn
in this case:Correct usage:
The text was updated successfully, but these errors were encountered: