Repository navigation
Fix: use unique value for NOT_FOUND - #709
Conversation
✅ Deploy Preview for reselect-docs canceled.
|
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
|
Seems like a good idea. Though I think in this particular scenario a test('cache miss identifier does not collide with state values', () => {
const state = ['NOT_FOUND', 'FOUND']
type State = typeof state
const createSelector = createSelectorCreator({
memoize: lruMemoize,
argsMemoize: lruMemoize
}).withTypes<State>()
const selector = createSelector(
[(state, id: number) => state[id]],
state => state,
{
argsMemoizeOptions: { maxSize: 10 },
memoizeOptions: { maxSize: 10 }
}
)
const firstResult = selector(state, 0)
expect(selector(state, 1)).toBe(selector(state, 1))
const secondResult = selector(state, 0)
expect(secondResult).toBe('NOT_FOUND')
expect(firstResult).toBe(secondResult)
expect(selector.recomputations()).toBe(2)
})It would have failed previously as |
|
I have added the suggested modifications. |
|
Ping, but feel free to close the PR if you're not going to merge it. |
|
@romgrk Maintainers are a little busy at the moment, I'm sure they will look at it as soon as they get a chance. In the meantime, if you don't mind, could you update the code with the provided suggestions? |
Co-authored-by: Arya Emami <[email protected]>
|
Thanks! |
This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [reselect](https://togithub.com/reduxjs/reselect) | dependencies | patch | [`5.1.0` -> `5.1.1`](https://renovatebot.com/diffs/npm/reselect/5.1.0/5.1.1) | --- ### Release Notes <details> <summary>reduxjs/reselect (reselect)</summary> ### [`v5.1.1`](https://togithub.com/reduxjs/reselect/releases/tag/v5.1.1) [Compare Source](https://togithub.com/reduxjs/reselect/compare/v5.1.0...v5.1.1) This **patch release** fixes behavior of `resultEqualityCheck` in `weakMapMemoize`, fixes the case of `lruMemoize` being given a `maxSize` less than 1, and tweaks the internal implementation of `lruMemoize`. (We've also updated our general build tooling.) ##### Changelog ##### Bug fixes Previously, providing the `resultEqualityCheck` option to `weakMapMemoize` resulted in it being called with empty objects as part of the initialization / dev check process. That could be an issue if your comparison function expected different values. We've updated the logic to avoid that, as well as improving a couple other perf aspects. Previously, passing a `maxSize` < 1 to `lruMemoize` would result in it creating a larger cache. That's now fixed. `lruMemoize` now uses a symbol for its `NOT_FOUND` value instead of a string. ##### What's Changed - Ensure `lruMemoize` correctly memoizes when `maxSize` is set to a number less than 1 by [@​aryaemami59](https://togithub.com/aryaemami59) in [https://github.com/reduxjs/reselect/pull/698](https://togithub.com/reduxjs/reselect/pull/698) - Fix `resultEqualityCheck` behavior in `weakMapMemoize` by [@​aryaemami59](https://togithub.com/aryaemami59) in [https://github.com/reduxjs/reselect/pull/699](https://togithub.com/reduxjs/reselect/pull/699) - Update TypeScript to 5.4 by [@​aryaemami59](https://togithub.com/aryaemami59) in [https://github.com/reduxjs/reselect/pull/708](https://togithub.com/reduxjs/reselect/pull/708) - Upgrade to Yarn 4 by [@​aryaemami59](https://togithub.com/aryaemami59) in [https://github.com/reduxjs/reselect/pull/705](https://togithub.com/reduxjs/reselect/pull/705) - Fix: use unique value for NOT_FOUND by [@​romgrk](https://togithub.com/romgrk) in [https://github.com/reduxjs/reselect/pull/709](https://togithub.com/reduxjs/reselect/pull/709) **Full Changelog**: reduxjs/reselect@v5.1.0...v5.1.1 </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://togithub.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4xNzAuMCIsInVwZGF0ZWRJblZlciI6IjM3LjE3MC4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9--> Co-authored-by: descope[bot] <descope[bot]@users.noreply.github.com>
I'm not sure what's the reasoning for using a string constant for the not-found value, but an object feels both safer (real uniqueness, the string
'NOT_FOUND'could make the logic fail) and more performant (nostrcmp()if both values are strings) which is handy when the selectors are called very often.I've used a private class instance for the empty value because you seem to use the value type
NOT_FOUND_TYPE, otherwise I would have gone with aSymbol('not-found')or an empty object, but any of these would be fine I think.