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

Allow passing in memoize functions directly to createSelector. #626

Merged
merged 58 commits into from
Oct 28, 2023

Commits on Oct 28, 2023

  1. fixed one of the overloads

    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    1b8791c View commit details
    Browse the repository at this point in the history
  2. createSelector now accepts a memoizeMethod option that allows the use…

    … to override the initial memoize function passed in to createSelectorCreator
    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    32c7693 View commit details
    Browse the repository at this point in the history
  3. Add type tests for memoizeMethod

    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    d066563 View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    2d08a6e View commit details
    Browse the repository at this point in the history
  5. Remove generic F from CreateSelectorFunction and `createSelectorC…

    …reator` in order to simplify the types.
    
    I ran the all the type tests without it and it seems it can be removed without sacrificing any sort of type safety on any level.
    When calling `createSelectorCreator`, it doesn't need to know the type of the function it's memoizer at some point will memoize, it can be inferred later.
    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    d46ce05 View commit details
    Browse the repository at this point in the history
  6. Unify createSelectorCreator function signatures and removed overloa…

    …ds in order to keep the types simple.
    
    I also removed `MemoizeOptions` from the `CreateSelectorFunction` interface and `createSelectorCreator` since its type can be derived directly from `MemoizeFunction`.
    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    97a6880 View commit details
    Browse the repository at this point in the history
  7. Add separate options for memoize and argsMemoize in createSelectorCre…

    …ator
    
    I tried to get the types for  `createSelectorCreator` to work with the new memoizeOptions type parameter without using overloads, but TS was still allowing passing in a second argument when the first argument is the memoizeOptions object.
    So I had to resort to using overloads. Might try again later to see if it's possible to make it work with only generics and conditionals.
    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    6bdf561 View commit details
    Browse the repository at this point in the history
  8. Add minimal JSDoc for setInputStabilityCheckEnabled and `getDepende…

    …ncies`
    
    Will try to add more inline documentation and JSDocs to types and functions.
    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    89e5bb1 View commit details
    Browse the repository at this point in the history
  9. Configuration menu
    Copy the full SHA
    6083103 View commit details
    Browse the repository at this point in the history
  10. CreateSelectorOptions now takes MemoizeFunction instead of MemoizeOpt…

    …ions
    
    I tested different variations of  CreateSelectorOptions and CreateSelectorFunction.
    From what I've learned so far, it seems like CreateSelectorFunction can do fine with just one generic which is MemoizeFunction.
    It can derive the rest of the types as needed.
     CreateSelectorOptions now uses the MemoizeFunction generic type in addition to OverrideMemoizeFunction and  OverrideArgsMemoizeFunction in order to create the type for the memoizeOptions object.
     Will investigate to see if CreateSelectorFunction can work with a `State` and `MemoizeFunction` generic type parameter.
    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    3311e78 View commit details
    Browse the repository at this point in the history
  11. Rename UnknownFunction to AnyFunction

    UnknownFunction kind of sounds like it's a function that has parameters of type unknown.
    So for the sake of semantic consistency I will create another type called UnknownFunction which will have parameters of type unknown.
    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    4c098a4 View commit details
    Browse the repository at this point in the history
  12. Create more utility types

    I also created UnknownFunction which has parameters of type unknown.
    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    481add2 View commit details
    Browse the repository at this point in the history
  13. Configuration menu
    Copy the full SHA
    6b040f8 View commit details
    Browse the repository at this point in the history
  14. Add JSDoc to CreateSelectorOptions

    Will add more JSDoc to all types and function signatures.
    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    b77c8b1 View commit details
    Browse the repository at this point in the history
  15. Remove Keys generic type parameter

    The `Keys` generic type parameter was causing type mismatches for selectors created using other third party memoize methods such as `microMemoize` and `memoizeOne`.
    The types did not have the output selector fields attached to the selectors.
    Once I removed Keys, the type mismatch was resolved.
    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    b7eb24e View commit details
    Browse the repository at this point in the history
  16. Fix TS issues for the first overload of createSelectorCreator

    The type parameter for the first overload of `createSelectorCreator` looks quite messy, but it works.
    I will try and simplify it later to make it more readable.
    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    eac3213 View commit details
    Browse the repository at this point in the history
  17. Configuration menu
    Copy the full SHA
    990b7b0 View commit details
    Browse the repository at this point in the history
  18. Configuration menu
    Copy the full SHA
    1058905 View commit details
    Browse the repository at this point in the history
  19. Fix type inference issues with CreateSelectorOptions.

    There were some issues with `CreateSelectorOptions` not inferring the type of `memoizeOptions` or `argsMemoizeOptions` all the way and it would sometimes give implicit any errors on the `equalityCheck` and `resultEqualityCheck` functions.
    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    a17fdf8 View commit details
    Browse the repository at this point in the history
  20. Fix CreateSelectorFunction TS type inference issues.

    Sometimes `CreateSelectorFunction` would not infer the type of the extra fields that memoizers return like `clearCache` for `defaultMemoize` or `isMemoized`. I added another generic type parameter called  `ArgsMemoizeFunction` which is exactly what it sounds like, it is the type of `argsMemoize`.
    
    It was necessary to add this type parameter since it can directly impact the return type of the selector, specifically the extra memoizer fields that selectors return. e.g: clearCache, cache, isMemoized, etc.
    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    db52127 View commit details
    Browse the repository at this point in the history
  21. Configuration menu
    Copy the full SHA
    588bd16 View commit details
    Browse the repository at this point in the history
  22. Configuration menu
    Copy the full SHA
    3d80872 View commit details
    Browse the repository at this point in the history
  23. Fix typo

    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    887bf54 View commit details
    Browse the repository at this point in the history
  24. Fix typo

    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    9b9de8f View commit details
    Browse the repository at this point in the history
  25. Configuration menu
    Copy the full SHA
    6113874 View commit details
    Browse the repository at this point in the history
  26. Unify memoizeOptions type parameter in createSelectorCreator with…

    … `CreateSelectorOptions`
    
    This was done to maintain a single source of truth in case the types were to change later.
    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    c30a741 View commit details
    Browse the repository at this point in the history
  27. Clean up CreateSelectorOptions

    - Added `IfNever` utility type to help conditionally resolve the type of a `memoizeOptions` object based on whether `memoize` is provided or not.
    - Added `Fallback` utility type to help detect whether a memoize function is to be overridden inside the options argument of `createSelector`.
    - Added `OverrideMemoizeOptions` utility type to help derive the type of `memoizeOptions` and `argsMemoizeOptions` based on whether `memoize` and `argsMemoize` were overridden.
    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    3189764 View commit details
    Browse the repository at this point in the history
  28. Configuration menu
    Copy the full SHA
    96022e3 View commit details
    Browse the repository at this point in the history
  29. Configuration menu
    Copy the full SHA
    2ce44c2 View commit details
    Browse the repository at this point in the history
  30. Configuration menu
    Copy the full SHA
    8fd2eae View commit details
    Browse the repository at this point in the history
  31. Rename argsMemoize type test file

    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    e566c8a View commit details
    Browse the repository at this point in the history
  32. Configuration menu
    Copy the full SHA
    57405b2 View commit details
    Browse the repository at this point in the history
  33. Add utils.ts file

    - Add `assertIsFunction` which is basically just a small part of code inside `createSelectorCreator` extracted to make the function body smaller and easier to read, and to add a small layer of type safety and reusability at the same time.
    - Add `ensureIsArray` which is another small part of code inside `createSelectorCreator`. But since it is a pattern that we repeat, it does not hurt to turn it into a function that we can reuse.
    - Move `getDependencies` to utils.ts and change its `Error` to `TypeError` in order to be more explicit.
    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    9cb9b9c View commit details
    Browse the repository at this point in the history
  34. Move createSelectorCreator and createStructuredSelector into thei…

    …r own files
    
    This was done because the new overload signature for `createSelectorCreator` was added and the index.ts file was getting a little too crowded.
    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    0601daf View commit details
    Browse the repository at this point in the history
  35. Configuration menu
    Copy the full SHA
    f4f63f5 View commit details
    Browse the repository at this point in the history
  36. Rewrite, change and rename DropFirst to DropFirstParameter

    Change `DropFirst` from a type that drops the first item in an array to a type that drops the first parameter in a function, and rename it to `DropFirstParameter`.
    This change was mostly due to the fact that the type was essentially a duplicate of the internal utility type `Tail`.
    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    c2dd369 View commit details
    Browse the repository at this point in the history
  37. Add Combiner utility type

    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    236f7bc View commit details
    Browse the repository at this point in the history
  38. Rewrite OutputSelectorFields

    Instead of taking a `Combiner` and a `Keys` generic type parameter, it now takes 4 generic parameters:
    
    - `Result`: the return value of `resultFunc`.
    - `Selectors`: the input selectors. This enables us to infer the type of `dependencies` as well.
    - `MemoizeFunction`: the memoize function used to infer the correct type for `memoizedResultFunc` and the new `memoize` output selector field.
    - `ArgsMemoizeFunction`: the memoize function used for the new `argsMemoize` output selector field.
    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    e4dfbb8 View commit details
    Browse the repository at this point in the history
  39. Rewrite and simplify OutputSelector

    It now takes the exact same generic type parameters as `OutputSelectorFields` which in turn simplifies the return type of `CreateSelectorFunction`.
    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    b1cc274 View commit details
    Browse the repository at this point in the history
  40. Rewrite OutputParametricSelector

    Remove the `Combiner` and `Keys` generic type parameters from `OutputParametricSelector` as they are no longer needed.
    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    a43ad79 View commit details
    Browse the repository at this point in the history
  41. Move StabilityCheck into types.ts

    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    e3fbc56 View commit details
    Browse the repository at this point in the history
  42. Move CreateSelectorOptions into types.ts

    This was done because now there are other types such as `OutputSelectorFields` that need to use it.
    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    5552281 View commit details
    Browse the repository at this point in the history
  43. Configuration menu
    Copy the full SHA
    bebf23e View commit details
    Browse the repository at this point in the history
  44. Make minor type tweaks

    - Rename `Fallback` to `FallbackIfNever`.
    - Add the source for `IfNever` and `OmitIndexSignature` and move them to External/Copied Utility Types section.
    - Rename `MemoizeOptsFromParams` to `MemoizeOptionsFromParameters`.
    - Update the description for `AnyFunction` to be more explicit.
    - Remove union with `never` in the `Params` generic type parameter of `Selector` since a union of `any[]` and `never` is just `any[]`.
    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    383923b View commit details
    Browse the repository at this point in the history
  45. Configuration menu
    Copy the full SHA
    37c7cb9 View commit details
    Browse the repository at this point in the history
  46. Import and Use AnyFunction to replace its definition in order to mi…

    …nimize repetition.
    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    002c2b8 View commit details
    Browse the repository at this point in the history
  47. Configuration menu
    Copy the full SHA
    fa31fcd View commit details
    Browse the repository at this point in the history
  48. Configuration menu
    Copy the full SHA
    ccfb005 View commit details
    Browse the repository at this point in the history
  49. Fix export issues

    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    8b89487 View commit details
    Browse the repository at this point in the history
  50. Simplify Selector type

    The previous conditional type can be slightly simplified since the condition itself only applies to `Params`
    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    3b9778e View commit details
    Browse the repository at this point in the history
  51. Swap generic slots for Selectors and Result inside OutputSelector

    It would make more sense if `Selectors` came before `Result` since that actually corresponds to the order of execution inside `createSelector`.
    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    e0b9310 View commit details
    Browse the repository at this point in the history
  52. Create runStabilityCheck utility function

    *`runStabilityCheck` is the extracted logic of running stability checks inside `createSelectorCreator` made into its own standalone function.
    It made sense to segregate this piece of logic from the body of `createSelectorCreator` as it has its own purpose somewhat independently from the rest of the function.
    
    *`collectInputSelectorResults` is another piece of logic extracted from the body of `createSelectorCreator`. It only focuses on creating an array from the return values of input selectors.
    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    87f8910 View commit details
    Browse the repository at this point in the history
  53. Add additional JSDocs

    - Made the following changes to enhance readability:
    
    * Add JSDocs to some of the types.
    
    * Rename some variables.
    
    * Rename some types.
    
    * Rename some generic type parameters.
    
    * Rename some parameter names.
    
    * Tweak some existing JSDocs.
    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    2229d3c View commit details
    Browse the repository at this point in the history
  54. Configuration menu
    Copy the full SHA
    01a64f6 View commit details
    Browse the repository at this point in the history
  55. Configuration menu
    Copy the full SHA
    0184db8 View commit details
    Browse the repository at this point in the history
  56. Configuration menu
    Copy the full SHA
    ad0d840 View commit details
    Browse the repository at this point in the history
  57. Remove structuredClone from unit tests.

    Substituted `deepClone` since Node 16 does not support `structuredClone`.
    aryaemami59 authored and markerikson committed Oct 28, 2023
    Configuration menu
    Copy the full SHA
    9f133f9 View commit details
    Browse the repository at this point in the history
  58. Configuration menu
    Copy the full SHA
    9d7290f View commit details
    Browse the repository at this point in the history