v4.1.0-alpha.2 #517
markerikson
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
This alpha release updates
defaultMemoizeto accept new options for cache size > 1 and a result equality check, updatescreateSelectorto accept an options object containing options for the providedmemoizefunction, improves some error messages, and addsmemoizedResultFuncandlastResultto the fields attached to the selector.These changes enable major improvements in functionality for
createSelector, and should resolve almost all the concerns and pain points experienced by our users.Although marked as an alpha, the code should be stable and be ready to ship as 4.1.0 in the very near future pending feedback on any potential upgrade issues.
Changelog
New
defaultMemoizeOptionsdefaultMemoizehas always been fairly limited. Its signature was(func: Function, equalityCheck?: EqualityFn) => Function, and only ever had a cache size of 1. This has led to many annoyances and workarounds, typically involving callingcreateSelectorCreator()with a custom memoization function that has a larger cache size or more options for customizing comparisons.We've updated
defaultMemoizeto allow cache sizes > 1, as well as customize comparisons of the newly generated result value to improve cache hits.The signature for
defaultMemoizeis now:In other words, you can still pass
equalityCheckas its one additional arg, or you may pass an object containing several possible options.If the
maxSizevalue is greater than 1,defaultMemoizewill now use an LRU cache based on https://github.com/erikras/lru-memoize internally.If
resultEqualityCheckis provided, it will be used to compare the newly-generated value fromfuncagainst all other values in the cache, in LRU order. If a cached value is found to be equal, that value will be returned. This addresses the commontodos.map(todo => todo.id)use case, where a change to any field in anytodoobject creates a newtodosarray and thus causes the output to be recalculated, but the generated IDs array is still shallow-equal to the last result. You can now pass an equality function likeshallowEqualas theresultEqualityCheckargument, and it will reuse the old IDs array instead.createSelectorOptionsPreviously, the only way to customize behavior of
createSelectorwas to generate a customized version withcreateSelectorCreator. By far the most common use case was customizing theequalityCheckoption used withdefaultMemoize, or using a different memoizer entirely. This usually looked like:createSelectorCreatoralso accepted additional positional parameters, and forwarded all of them to the providedmemoizefunction, sodefaultMemoizeultimately gets called internally asdefaultMemoize(actualFunction, shallowEqual).This added an annoying level of indirection to common customization use cases.
createSelectornow accepts an options object as its last argument, after the output selector. Currently, that object only includes one field:memoizeOptions:Similar to how
createSelectorCreatoraccepts additional "options args" that get forwarded to the memoization function, thememoizeOptionsfield accepts an array of those "options args" as well. If provided, these override what was given tocreateSelectorCreator.That means that you can now customize memoization behavior with direct options to
createSelector. And, becausedefaultMemoizenow accepts more options, you can directly customizedefaultMemoize's behavior without usingcreateSelectorCreator.Additionally, because it's very common to only need to pass one options arg to the memoization function,
memoizeOptionsmay also be just that first options arg by itself, without any array.Example usages of this look like:
This should make it much easier to customize behavior.
All of this is fully TypeScript-typed, and the possible values for
memoizeOptionsshould be fully inferred from the providedmemoizefunction.Additional Tweaks
We've improved the error messages thrown when invalid selectors are provided.
Generated selectors now include
selector.memoizedResultFuncandselector.lastResultfor later access if needed.What's Changed
defaultMemoizeto accept options (maxSize, equalityCheck, resultEqualityCheck) by @markerikson in Update createSelector anddefaultMemoizeto accept options (maxSize, equalityCheck, resultEqualityCheck) #513New Contributors
Full Changelog: v4.1.0-alpha.1...v4.1.0-alpha.2
This discussion was created from the release v4.1.0-alpha.2.
Beta Was this translation helpful? Give feedback.
All reactions