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

Better documentation for resolvers? #53

Closed
seanohollaren opened this issue Mar 7, 2016 · 3 comments
Closed

Better documentation for resolvers? #53

seanohollaren opened this issue Mar 7, 2016 · 3 comments
Assignees

Comments

@seanohollaren
Copy link

I'm a huge fan of the built-in stats and cache management tools that memoizee offers, but right now I'm totally puzzled by how I'm supposed to use the resolver functionality.

Lodash and Underscore resolvers are very straight forward. For those, you provide a function which returns the key used for the cache map.

Like this:
const memoizedFunction = _.memoize(nonMemoizedFunction, (anObject, aBool) => anObject.id + aBool.toString() );

Memoizee's resolver functionality, however, is a lot less clear.

To me, it's not apparent from the documentation how to craft a resolver function and it seems that memoizee focuses on argument type as opposed to allowing for arbitrary key construction.

Is it possible to memoize based on a given object's property instead of attempting to serialize the entire object? What about combining that with a second parameter?

For example, given foo(anObject, aBool), I'd like my memoization key to be anObject.id + bool.

Given how important resolvers are to a memoization library functioning as intended, I think the documentation would really benefit from being beefed up with useful examples as opposed to trivial ones.

@medikoo medikoo self-assigned this Apr 21, 2016
@medikoo
Copy link
Owner

medikoo commented Apr 21, 2016

@seanohollaren Firstly, sorry for replying that late, Github notifications failed for me, and it's today I noticed that issue.

resolvers are purely about normalization of arguments, before they're provided into caching mechanism and underlying function. This unifies handling of some cases, e.g. if at some argument you expect a number, the default caching mechanism will treat '2' and 2 differently (as by language they're different values), however if you indicate a Number resolver for that argument, then it's not the case, as '2' will be turned to 2 before it reaches caching handling.

Now concerning normalization of cache id, there's not documented option normalizer (sorry for that, I'll try to address that shortly, and keep this issue open until that's the case).

Through that option you provide a function that's meant to resolve cache id (string) for given call. So in case you're describing it can be:

var memoizedFunction = memoizee(function (anObject, aBool) {
  ...
}, { normalizer: function (args) {
  var anObject = args[0], aBool = args[1];
  return anObject.id + aBool.toString();
});

@medikoo
Copy link
Owner

medikoo commented Jul 4, 2016

Fixed by 75422fd

@medikoo medikoo closed this as completed Jul 4, 2016
@seanohollaren
Copy link
Author

Looks great, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants