v4.1.0 #524
markerikson
started this conversation in
General
v4.1.0
#524
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
This long-overdue release updates
defaultMemoize
to accept new options for cache size > 1 and a result equality check, updatescreateSelector
to accept an options object containing options for the providedmemoize
function, makes major improvements to the TypeScript types (targeting TS 4.2+), converts the codebase to TS, improves some error messages, and addsmemoizedResultFunc
andlastResult
to the fields attached to the selector,This should be a drop-in update - the only expected backwards compatibility issues are with incorrect or very outdated TypeScript usage patterns.
Changelog
New
defaultMemoize
OptionsdefaultMemoize
has 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
defaultMemoize
to allow cache sizes > 1, as well as customize comparisons of the newly generated result value to improve cache hits.The signature for
defaultMemoize
is now:In other words, you can still pass
equalityCheck
as its one additional arg, or you may pass an object containing several possible options.If the
maxSize
value is greater than 1,defaultMemoize
will now use an LRU cache based on https://github.com/erikras/lru-memoize internally.If
resultEqualityCheck
is provided, it will be used to compare the newly-generated value fromfunc
against 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 anytodo
object creates a newtodos
array 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 likeshallowEqual
as theresultEqualityCheck
argument, and it will reuse the old IDs array instead.createSelector
OptionsPreviously, the only way to customize behavior of
createSelector
was to generate a customized version withcreateSelectorCreator
. By far the most common use case was customizing theequalityCheck
option used withdefaultMemoize
, or using a different memoizer entirely. This usually looked like:createSelectorCreator
also accepted additional positional parameters, and forwarded all of them to the providedmemoize
function, sodefaultMemoize
ultimately gets called internally asdefaultMemoize(actualFunction, shallowEqual)
.This added an annoying level of indirection to common customization use cases.
createSelector
now accepts an options object as its last argument, after the output selector. Currently, that object only includes one field:memoizeOptions
:Similar to how
createSelectorCreator
accepts additional "options args" that get forwarded to the memoization function, thememoizeOptions
field 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, becausedefaultMemoize
now 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,
memoizeOptions
may 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
memoizeOptions
should be fully inferred from the providedmemoize
function.Additionally,
defaultMemoize
now supports clearing the cache inside a memoized function (regardless of cache size). The memoized function returned fromdefaultMemoize
will now have a.clearCache()
method attached that will clear the cache.When using
createSelector
, this can be accessed usingselector.memoizedResultFunc.clearCache()
.TypeScript Improvements
The Reselect types were written several years ago and originally targeted TS 2.x versions. As a result, the typedefs requires dozens of overloads to handle varying numbers of arguments (see the legacy typedefs file for examples).
We've converted the codebase to be written in TypeScript, and as part of that process we've completely rewritten the TS typedefs to use modern TS syntax like mapped types. This drastically shrinks the size of the typedefs (from 1000 lines to about 115), and also improves the actual type inference overall. Assuming the input selectors are correctly and consistently typed, TS will now fully infer the return values of all input selectors, the arguments to the output selector, and the exact type of the memoized function.
The updated types do require use of TS 4.2+. We've attempted to keep the final public type names and usage the same, but there may also be some types breakage. We'd appreciate feedback on any meaningful breakage issues so we can make further tweaks if needed.
Given the intent of the improvements, that they're all type-only changes, the attempts to retain backwards compatibility, and TS's own versioning scheme, we're considering this to be a minor version change rather than a major.
In pre-release testing, the main issues we saw were:
state
arg. Fix: explicitly add a type tostate
<A, B, C, D>
generics from thecreateSelector()
call.The legacy types are still included, and should automatically be used if you are using TS 4.1 and earlier. Note that the legacy types do not include the definitions for the new
defaultMemoize
options - you'll need to be on TS 4.2+ to use those with TS.Additional Tweaks
We've improved the error messages thrown when invalid selectors are provided.
Generated selectors now include
selector.memoizedResultFunc
andselector.lastResult
for later access if needed.Changes
The early alphas contained code from several outstanding PRs, pulled together:
memoize
type fixes ( @micahbales )createStructuredSelector
inference ( @oatkiller )Additional work included:
defaultMemoize
to accept options (maxSize, equalityCheck, resultEqualityCheck) by @markerikson in Update createSelector anddefaultMemoize
to accept options (maxSize, equalityCheck, resultEqualityCheck) #513clearCache
method to defaultMemoize output functions by @markerikson in Add aclearCache
method to defaultMemoize output functions #519Full Changelog: v4.0.0...v4.1.0
This discussion was created from the release v4.1.0.
Beta Was this translation helpful? Give feedback.
All reactions