[d24f17c] | 1 | /**
|
---|
| 2 | * Represents the longest array within an array of arrays.
|
---|
| 3 | *
|
---|
| 4 | * @template ArrayOfTuples An array of arrays.
|
---|
| 5 | *
|
---|
| 6 | * @internal
|
---|
| 7 | */
|
---|
| 8 | type LongestTuple<ArrayOfTuples extends readonly unknown[][]> = ArrayOfTuples extends [infer FirstArray extends unknown[]] ? FirstArray : ArrayOfTuples extends [
|
---|
| 9 | infer FirstArray,
|
---|
| 10 | ...infer RestArrays extends unknown[][]
|
---|
| 11 | ] ? LongerOfTwo<FirstArray, LongestTuple<RestArrays>> : never;
|
---|
| 12 | /**
|
---|
| 13 | * Determines the longer of two array types.
|
---|
| 14 | *
|
---|
| 15 | * @template ArrayOne First array type.
|
---|
| 16 | * @template ArrayTwo Second array type.
|
---|
| 17 | *
|
---|
| 18 | * @internal
|
---|
| 19 | */
|
---|
| 20 | type LongerOfTwo<ArrayOne, ArrayTwo> = keyof ArrayTwo extends keyof ArrayOne ? ArrayOne : ArrayTwo;
|
---|
| 21 | /**
|
---|
| 22 | * Extracts the element at a specific index in an array.
|
---|
| 23 | *
|
---|
| 24 | * @template ArrayType The array type.
|
---|
| 25 | * @template Index The index type.
|
---|
| 26 | *
|
---|
| 27 | * @internal
|
---|
| 28 | */
|
---|
| 29 | type ElementAt<ArrayType extends unknown[], Index extends PropertyKey> = Index extends keyof ArrayType ? ArrayType[Index] : unknown;
|
---|
| 30 | /**
|
---|
| 31 | * Maps each array in an array of arrays to its element at a given index.
|
---|
| 32 | *
|
---|
| 33 | * @template ArrayOfTuples An array of arrays.
|
---|
| 34 | * @template Index The index to extract from each array.
|
---|
| 35 | *
|
---|
| 36 | * @internal
|
---|
| 37 | */
|
---|
| 38 | type ElementsAtGivenIndex<ArrayOfTuples extends readonly unknown[][], Index extends PropertyKey> = {
|
---|
| 39 | [ArrayIndex in keyof ArrayOfTuples]: ElementAt<ArrayOfTuples[ArrayIndex], Index>;
|
---|
| 40 | };
|
---|
| 41 | /**
|
---|
| 42 | * Computes the intersection of all types in a tuple.
|
---|
| 43 | *
|
---|
| 44 | * @template Tuple A tuple of types.
|
---|
| 45 | *
|
---|
| 46 | * @internal
|
---|
| 47 | */
|
---|
| 48 | type Intersect<Tuple extends readonly unknown[]> = Tuple extends [] ? unknown : Tuple extends [infer Head, ...infer Tail] ? Head & Intersect<Tail> : Tuple[number];
|
---|
| 49 | /**
|
---|
| 50 | * Merges a tuple of arrays into a single tuple, intersecting types at each index.
|
---|
| 51 | *
|
---|
| 52 | * @template ArrayOfTuples An array of tuples.
|
---|
| 53 | * @template LongestArray The longest array in ArrayOfTuples.
|
---|
| 54 | *
|
---|
| 55 | * @internal
|
---|
| 56 | */
|
---|
| 57 | type MergeTuples<ArrayOfTuples extends readonly unknown[][], LongestArray extends unknown[] = LongestTuple<ArrayOfTuples>> = {
|
---|
| 58 | [Index in keyof LongestArray]: Intersect<ElementsAtGivenIndex<ArrayOfTuples, Index>>;
|
---|
| 59 | };
|
---|
| 60 | /**
|
---|
| 61 | * Extracts the parameter types from a tuple of functions.
|
---|
| 62 | *
|
---|
| 63 | * @template FunctionsArray An array of function types.
|
---|
| 64 | *
|
---|
| 65 | * @internal
|
---|
| 66 | */
|
---|
| 67 | type ExtractParameters<FunctionsArray extends readonly AnyFunction[]> = {
|
---|
| 68 | [Index in keyof FunctionsArray]: Parameters<FunctionsArray[Index]>;
|
---|
| 69 | };
|
---|
| 70 | /**
|
---|
| 71 | * Merges the parameters of a tuple of functions into a single tuple.
|
---|
| 72 | *
|
---|
| 73 | * @template FunctionsArray An array of function types.
|
---|
| 74 | *
|
---|
| 75 | * @internal
|
---|
| 76 | */
|
---|
| 77 | type MergeParameters<FunctionsArray extends readonly AnyFunction[]> = '0' extends keyof FunctionsArray ? MergeTuples<ExtractParameters<FunctionsArray>> : Parameters<FunctionsArray[number]>;
|
---|
| 78 |
|
---|
| 79 | /**
|
---|
| 80 | * Configuration options for a memoization function utilizing `WeakMap` for
|
---|
| 81 | * its caching mechanism.
|
---|
| 82 | *
|
---|
| 83 | * @template Result - The type of the return value of the memoized function.
|
---|
| 84 | *
|
---|
| 85 | * @since 5.0.0
|
---|
| 86 | * @public
|
---|
| 87 | */
|
---|
| 88 | interface WeakMapMemoizeOptions<Result = any> {
|
---|
| 89 | /**
|
---|
| 90 | * If provided, used to compare a newly generated output value against previous values in the cache.
|
---|
| 91 | * If a match is found, the old value is returned. This addresses the common
|
---|
| 92 | * ```ts
|
---|
| 93 | * todos.map(todo => todo.id)
|
---|
| 94 | * ```
|
---|
| 95 | * use case, where an update to another field in the original data causes a recalculation
|
---|
| 96 | * due to changed references, but the output is still effectively the same.
|
---|
| 97 | *
|
---|
| 98 | * @since 5.0.0
|
---|
| 99 | */
|
---|
| 100 | resultEqualityCheck?: EqualityFn<Result>;
|
---|
| 101 | }
|
---|
| 102 | /**
|
---|
| 103 | * Creates a tree of `WeakMap`-based cache nodes based on the identity of the
|
---|
| 104 | * arguments it's been called with (in this case, the extracted values from your input selectors).
|
---|
| 105 | * This allows `weakMapMemoize` to have an effectively infinite cache size.
|
---|
| 106 | * Cache results will be kept in memory as long as references to the arguments still exist,
|
---|
| 107 | * and then cleared out as the arguments are garbage-collected.
|
---|
| 108 | *
|
---|
| 109 | * __Design Tradeoffs for `weakMapMemoize`:__
|
---|
| 110 | * - Pros:
|
---|
| 111 | * - It has an effectively infinite cache size, but you have no control over
|
---|
| 112 | * how long values are kept in cache as it's based on garbage collection and `WeakMap`s.
|
---|
| 113 | * - Cons:
|
---|
| 114 | * - There's currently no way to alter the argument comparisons.
|
---|
| 115 | * They're based on strict reference equality.
|
---|
| 116 | * - It's roughly the same speed as `lruMemoize`, although likely a fraction slower.
|
---|
| 117 | *
|
---|
| 118 | * __Use Cases for `weakMapMemoize`:__
|
---|
| 119 | * - This memoizer is likely best used for cases where you need to call the
|
---|
| 120 | * same selector instance with many different arguments, such as a single
|
---|
| 121 | * selector instance that is used in a list item component and called with
|
---|
| 122 | * item IDs like:
|
---|
| 123 | * ```ts
|
---|
| 124 | * useSelector(state => selectSomeData(state, props.category))
|
---|
| 125 | * ```
|
---|
| 126 | * @param func - The function to be memoized.
|
---|
| 127 | * @returns A memoized function with a `.clearCache()` method attached.
|
---|
| 128 | *
|
---|
| 129 | * @example
|
---|
| 130 | * <caption>Using `createSelector`</caption>
|
---|
| 131 | * ```ts
|
---|
| 132 | * import { createSelector, weakMapMemoize } from 'reselect'
|
---|
| 133 | *
|
---|
| 134 | * interface RootState {
|
---|
| 135 | * items: { id: number; category: string; name: string }[]
|
---|
| 136 | * }
|
---|
| 137 | *
|
---|
| 138 | * const selectItemsByCategory = createSelector(
|
---|
| 139 | * [
|
---|
| 140 | * (state: RootState) => state.items,
|
---|
| 141 | * (state: RootState, category: string) => category
|
---|
| 142 | * ],
|
---|
| 143 | * (items, category) => items.filter(item => item.category === category),
|
---|
| 144 | * {
|
---|
| 145 | * memoize: weakMapMemoize,
|
---|
| 146 | * argsMemoize: weakMapMemoize
|
---|
| 147 | * }
|
---|
| 148 | * )
|
---|
| 149 | * ```
|
---|
| 150 | *
|
---|
| 151 | * @example
|
---|
| 152 | * <caption>Using `createSelectorCreator`</caption>
|
---|
| 153 | * ```ts
|
---|
| 154 | * import { createSelectorCreator, weakMapMemoize } from 'reselect'
|
---|
| 155 | *
|
---|
| 156 | * const createSelectorWeakMap = createSelectorCreator({ memoize: weakMapMemoize, argsMemoize: weakMapMemoize })
|
---|
| 157 | *
|
---|
| 158 | * const selectItemsByCategory = createSelectorWeakMap(
|
---|
| 159 | * [
|
---|
| 160 | * (state: RootState) => state.items,
|
---|
| 161 | * (state: RootState, category: string) => category
|
---|
| 162 | * ],
|
---|
| 163 | * (items, category) => items.filter(item => item.category === category)
|
---|
| 164 | * )
|
---|
| 165 | * ```
|
---|
| 166 | *
|
---|
| 167 | * @template Func - The type of the function that is memoized.
|
---|
| 168 | *
|
---|
| 169 | * @see {@link https://reselect.js.org/api/weakMapMemoize `weakMapMemoize`}
|
---|
| 170 | *
|
---|
| 171 | * @since 5.0.0
|
---|
| 172 | * @public
|
---|
| 173 | * @experimental
|
---|
| 174 | */
|
---|
| 175 | declare function weakMapMemoize<Func extends AnyFunction>(func: Func, options?: WeakMapMemoizeOptions<ReturnType<Func>>): Func & {
|
---|
| 176 | clearCache: () => void;
|
---|
| 177 | resultsCount: () => number;
|
---|
| 178 | resetResultsCount: () => void;
|
---|
| 179 | };
|
---|
| 180 |
|
---|
| 181 | /**
|
---|
| 182 | * A standard selector function.
|
---|
| 183 | * @template State - The first value, often a Redux root state object.
|
---|
| 184 | * @template Result - The final result returned by the selector.
|
---|
| 185 | * @template Params - All additional arguments passed into the selector.
|
---|
| 186 | *
|
---|
| 187 | * @public
|
---|
| 188 | */
|
---|
| 189 | type Selector<State = any, Result = unknown, Params extends readonly any[] = any[]> = Distribute<
|
---|
| 190 | /**
|
---|
| 191 | * A function that takes a state and returns data that is based on that state.
|
---|
| 192 | *
|
---|
| 193 | * @param state - The first argument, often a Redux root state object.
|
---|
| 194 | * @param params - All additional arguments passed into the selector.
|
---|
| 195 | * @returns A derived value from the state.
|
---|
| 196 | */
|
---|
| 197 | (state: State, ...params: FallbackIfNever<Params, []>) => Result>;
|
---|
| 198 | /**
|
---|
| 199 | * An array of input selectors.
|
---|
| 200 | *
|
---|
| 201 | * @public
|
---|
| 202 | */
|
---|
| 203 | type SelectorArray<State = any> = readonly Selector<State>[];
|
---|
| 204 | /**
|
---|
| 205 | * Extracts an array of all return types from all input selectors.
|
---|
| 206 | *
|
---|
| 207 | * @public
|
---|
| 208 | */
|
---|
| 209 | type SelectorResultArray<Selectors extends SelectorArray> = ExtractReturnType<Selectors>;
|
---|
| 210 | /**
|
---|
| 211 | * The options object used inside `createSelector` and `createSelectorCreator`.
|
---|
| 212 | *
|
---|
| 213 | * @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`).
|
---|
| 214 | * @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
|
---|
| 215 | * @template OverrideMemoizeFunction - The type of the optional `memoize` function that could be passed into the options object inside `createSelector` to override the original `memoize` function that was initially passed into `createSelectorCreator`.
|
---|
| 216 | * @template OverrideArgsMemoizeFunction - The type of the optional `argsMemoize` function that could be passed into the options object inside `createSelector` to override the original `argsMemoize` function that was initially passed into `createSelectorCreator`. If none was initially provided, `weakMapMemoize` will be used.
|
---|
| 217 | *
|
---|
| 218 | * @public
|
---|
| 219 | */
|
---|
| 220 | interface CreateSelectorOptions<MemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize, ArgsMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize, OverrideMemoizeFunction extends UnknownMemoizer = never, OverrideArgsMemoizeFunction extends UnknownMemoizer = never> {
|
---|
| 221 | /**
|
---|
| 222 | * Reselect performs additional checks in development mode to help identify
|
---|
| 223 | * and warn about potential issues in selector behavior. This option
|
---|
| 224 | * allows you to customize the behavior of these checks per selector.
|
---|
| 225 | *
|
---|
| 226 | * @see {@link https://reselect.js.org/api/development-only-stability-checks Development-Only Stability Checks}
|
---|
| 227 | *
|
---|
| 228 | * @since 5.0.0
|
---|
| 229 | */
|
---|
| 230 | devModeChecks?: Partial<DevModeChecks>;
|
---|
| 231 | /**
|
---|
| 232 | * The memoize function that is used to memoize the {@linkcode OutputSelectorFields.resultFunc resultFunc}
|
---|
| 233 | * inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`).
|
---|
| 234 | *
|
---|
| 235 | * When passed directly into `createSelector`, it overrides the `memoize` function initially passed into `createSelectorCreator`.
|
---|
| 236 | *
|
---|
| 237 | * @example
|
---|
| 238 | * ```ts
|
---|
| 239 | * import { createSelector, weakMapMemoize } from 'reselect'
|
---|
| 240 | *
|
---|
| 241 | * const selectItemsByCategory = createSelector(
|
---|
| 242 | * [
|
---|
| 243 | * (state: RootState) => state.items,
|
---|
| 244 | * (state: RootState, category: string) => category
|
---|
| 245 | * ],
|
---|
| 246 | * (items, category) => items.filter(item => item.category === category),
|
---|
| 247 | * { memoize: weakMapMemoize }
|
---|
| 248 | * )
|
---|
| 249 | * ```
|
---|
| 250 | *
|
---|
| 251 | * @since 5.0.0
|
---|
| 252 | */
|
---|
| 253 | memoize?: FallbackIfNever<OverrideMemoizeFunction, MemoizeFunction>;
|
---|
| 254 | /**
|
---|
| 255 | * The optional memoize function that is used to memoize the arguments
|
---|
| 256 | * passed into the output selector generated by `createSelector`
|
---|
| 257 | * (e.g., `lruMemoize` or `weakMapMemoize`).
|
---|
| 258 | *
|
---|
| 259 | * When passed directly into `createSelector`, it overrides the
|
---|
| 260 | * `argsMemoize` function initially passed into `createSelectorCreator`.
|
---|
| 261 | * If none was initially provided, `weakMapMemoize` will be used.
|
---|
| 262 | *
|
---|
| 263 | * @example
|
---|
| 264 | * ```ts
|
---|
| 265 | * import { createSelector, weakMapMemoize } from 'reselect'
|
---|
| 266 | *
|
---|
| 267 | * const selectItemsByCategory = createSelector(
|
---|
| 268 | * [
|
---|
| 269 | * (state: RootState) => state.items,
|
---|
| 270 | * (state: RootState, category: string) => category
|
---|
| 271 | * ],
|
---|
| 272 | * (items, category) => items.filter(item => item.category === category),
|
---|
| 273 | * { argsMemoize: weakMapMemoize }
|
---|
| 274 | * )
|
---|
| 275 | * ```
|
---|
| 276 | *
|
---|
| 277 | * @default weakMapMemoize
|
---|
| 278 | *
|
---|
| 279 | * @since 5.0.0
|
---|
| 280 | */
|
---|
| 281 | argsMemoize?: FallbackIfNever<OverrideArgsMemoizeFunction, ArgsMemoizeFunction>;
|
---|
| 282 | /**
|
---|
| 283 | * Optional configuration options for the {@linkcode CreateSelectorOptions.memoize memoize} function.
|
---|
| 284 | * These options are passed to the {@linkcode CreateSelectorOptions.memoize memoize} function as the second argument.
|
---|
| 285 | *
|
---|
| 286 | * @since 5.0.0
|
---|
| 287 | */
|
---|
| 288 | memoizeOptions?: OverrideMemoizeOptions<MemoizeFunction, OverrideMemoizeFunction>;
|
---|
| 289 | /**
|
---|
| 290 | * Optional configuration options for the {@linkcode CreateSelectorOptions.argsMemoize argsMemoize} function.
|
---|
| 291 | * These options are passed to the {@linkcode CreateSelectorOptions.argsMemoize argsMemoize} function as the second argument.
|
---|
| 292 | *
|
---|
| 293 | * @since 5.0.0
|
---|
| 294 | */
|
---|
| 295 | argsMemoizeOptions?: OverrideMemoizeOptions<ArgsMemoizeFunction, OverrideArgsMemoizeFunction>;
|
---|
| 296 | }
|
---|
| 297 | /**
|
---|
| 298 | * The additional fields attached to the output selector generated by `createSelector`.
|
---|
| 299 | *
|
---|
| 300 | * **Note**: Although {@linkcode CreateSelectorOptions.memoize memoize}
|
---|
| 301 | * and {@linkcode CreateSelectorOptions.argsMemoize argsMemoize} are included in the attached fields,
|
---|
| 302 | * the fields themselves are independent of the type of
|
---|
| 303 | * {@linkcode CreateSelectorOptions.memoize memoize} and {@linkcode CreateSelectorOptions.argsMemoize argsMemoize} functions.
|
---|
| 304 | * Meaning this type is not going to generate additional fields based on what functions we use to memoize our selectors.
|
---|
| 305 | *
|
---|
| 306 | * _This type is not to be confused with {@linkcode ExtractMemoizerFields ExtractMemoizerFields}._
|
---|
| 307 | *
|
---|
| 308 | * @template InputSelectors - The type of the input selectors.
|
---|
| 309 | * @template Result - The type of the result returned by the `resultFunc`.
|
---|
| 310 | * @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`).
|
---|
| 311 | * @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
|
---|
| 312 | *
|
---|
| 313 | * @public
|
---|
| 314 | */
|
---|
| 315 | type OutputSelectorFields<InputSelectors extends SelectorArray = SelectorArray, Result = unknown, MemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize, ArgsMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize> = {
|
---|
| 316 | /**
|
---|
| 317 | * The final function passed to `createSelector`. Otherwise known as the `combiner`.
|
---|
| 318 | */
|
---|
| 319 | resultFunc: Combiner<InputSelectors, Result>;
|
---|
| 320 | /**
|
---|
| 321 | * The memoized version of {@linkcode OutputSelectorFields.resultFunc resultFunc}.
|
---|
| 322 | */
|
---|
| 323 | memoizedResultFunc: Combiner<InputSelectors, Result> & ExtractMemoizerFields<MemoizeFunction>;
|
---|
| 324 | /**
|
---|
| 325 | * @Returns The last result calculated by {@linkcode OutputSelectorFields.memoizedResultFunc memoizedResultFunc}.
|
---|
| 326 | */
|
---|
| 327 | lastResult: () => Result;
|
---|
| 328 | /**
|
---|
| 329 | * The array of the input selectors used by `createSelector` to compose the
|
---|
| 330 | * combiner ({@linkcode OutputSelectorFields.memoizedResultFunc memoizedResultFunc}).
|
---|
| 331 | */
|
---|
| 332 | dependencies: InputSelectors;
|
---|
| 333 | /**
|
---|
| 334 | * Counts the number of times {@linkcode OutputSelectorFields.memoizedResultFunc memoizedResultFunc} has been recalculated.
|
---|
| 335 | */
|
---|
| 336 | recomputations: () => number;
|
---|
| 337 | /**
|
---|
| 338 | * Resets the count of {@linkcode OutputSelectorFields.recomputations recomputations} count to 0.
|
---|
| 339 | */
|
---|
| 340 | resetRecomputations: () => void;
|
---|
| 341 | /**
|
---|
| 342 | * Counts the number of times the input selectors ({@linkcode OutputSelectorFields.dependencies dependencies})
|
---|
| 343 | * have been recalculated. This is distinct from {@linkcode OutputSelectorFields.recomputations recomputations},
|
---|
| 344 | * which tracks the recalculations of the result function.
|
---|
| 345 | *
|
---|
| 346 | * @since 5.0.0
|
---|
| 347 | */
|
---|
| 348 | dependencyRecomputations: () => number;
|
---|
| 349 | /**
|
---|
| 350 | * Resets the count {@linkcode OutputSelectorFields.dependencyRecomputations dependencyRecomputations}
|
---|
| 351 | * for the input selectors ({@linkcode OutputSelectorFields.dependencies dependencies})
|
---|
| 352 | * of a memoized selector.
|
---|
| 353 | *
|
---|
| 354 | * @since 5.0.0
|
---|
| 355 | */
|
---|
| 356 | resetDependencyRecomputations: () => void;
|
---|
| 357 | } & Simplify<Required<Pick<CreateSelectorOptions<MemoizeFunction, ArgsMemoizeFunction>, 'argsMemoize' | 'memoize'>>>;
|
---|
| 358 | /**
|
---|
| 359 | * Represents the actual selectors generated by `createSelector`.
|
---|
| 360 | *
|
---|
| 361 | * @template InputSelectors - The type of the input selectors.
|
---|
| 362 | * @template Result - The type of the result returned by the `resultFunc`.
|
---|
| 363 | * @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`).
|
---|
| 364 | * @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
|
---|
| 365 | *
|
---|
| 366 | * @public
|
---|
| 367 | */
|
---|
| 368 | type OutputSelector<InputSelectors extends SelectorArray = SelectorArray, Result = unknown, MemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize, ArgsMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize> = Selector<GetStateFromSelectors<InputSelectors>, Result, GetParamsFromSelectors<InputSelectors>> & ExtractMemoizerFields<ArgsMemoizeFunction> & OutputSelectorFields<InputSelectors, Result, MemoizeFunction, ArgsMemoizeFunction>;
|
---|
| 369 | /**
|
---|
| 370 | * A function that takes input selectors' return values as arguments and returns a result. Otherwise known as `resultFunc`.
|
---|
| 371 | *
|
---|
| 372 | * @template InputSelectors - An array of input selectors.
|
---|
| 373 | * @template Result - Result returned by `resultFunc`.
|
---|
| 374 | *
|
---|
| 375 | * @public
|
---|
| 376 | */
|
---|
| 377 | type Combiner<InputSelectors extends SelectorArray, Result> = Distribute<
|
---|
| 378 | /**
|
---|
| 379 | * A function that takes input selectors' return values as arguments and returns a result. Otherwise known as `resultFunc`.
|
---|
| 380 | *
|
---|
| 381 | * @param resultFuncArgs - Return values of input selectors.
|
---|
| 382 | * @returns The return value of {@linkcode OutputSelectorFields.resultFunc resultFunc}.
|
---|
| 383 | */
|
---|
| 384 | (...resultFuncArgs: SelectorResultArray<InputSelectors>) => Result>;
|
---|
| 385 | /**
|
---|
| 386 | * A standard function returning true if two values are considered equal.
|
---|
| 387 | *
|
---|
| 388 | * @public
|
---|
| 389 | */
|
---|
| 390 | type EqualityFn<T = any> = (a: T, b: T) => boolean;
|
---|
| 391 | /**
|
---|
| 392 | * The frequency of development mode checks.
|
---|
| 393 | *
|
---|
| 394 | * @since 5.0.0
|
---|
| 395 | * @public
|
---|
| 396 | */
|
---|
| 397 | type DevModeCheckFrequency = 'always' | 'once' | 'never';
|
---|
| 398 | /**
|
---|
| 399 | * Represents the configuration for development mode checks.
|
---|
| 400 | *
|
---|
| 401 | * @since 5.0.0
|
---|
| 402 | * @public
|
---|
| 403 | */
|
---|
| 404 | interface DevModeChecks {
|
---|
| 405 | /**
|
---|
| 406 | * Overrides the global input stability check for the selector.
|
---|
| 407 | * - `once` - Run only the first time the selector is called.
|
---|
| 408 | * - `always` - Run every time the selector is called.
|
---|
| 409 | * - `never` - Never run the input stability check.
|
---|
| 410 | *
|
---|
| 411 | * @default 'once'
|
---|
| 412 | *
|
---|
| 413 | * @see {@link https://reselect.js.org/api/development-only-stability-checks Development-Only Stability Checks}
|
---|
| 414 | * @see {@link https://reselect.js.org/api/development-only-stability-checks#inputstabilitycheck `inputStabilityCheck`}
|
---|
| 415 | * @see {@link https://reselect.js.org/api/development-only-stability-checks#2-per-selector-by-passing-an-inputstabilitycheck-option-directly-to- per-selector-configuration}
|
---|
| 416 | *
|
---|
| 417 | * @since 5.0.0
|
---|
| 418 | */
|
---|
| 419 | inputStabilityCheck: DevModeCheckFrequency;
|
---|
| 420 | /**
|
---|
| 421 | * Overrides the global identity function check for the selector.
|
---|
| 422 | * - `once` - Run only the first time the selector is called.
|
---|
| 423 | * - `always` - Run every time the selector is called.
|
---|
| 424 | * - `never` - Never run the identity function check.
|
---|
| 425 | *
|
---|
| 426 | * @default 'once'
|
---|
| 427 | *
|
---|
| 428 | * @see {@link https://reselect.js.org/api/development-only-stability-checks Development-Only Stability Checks}
|
---|
| 429 | * @see {@link https://reselect.js.org/api/development-only-stability-checks#identityfunctioncheck `identityFunctionCheck`}
|
---|
| 430 | * @see {@link https://reselect.js.org/api/development-only-stability-checks#2-per-selector-by-passing-an-identityfunctioncheck-option-directly-to- per-selector-configuration}
|
---|
| 431 | *
|
---|
| 432 | * @since 5.0.0
|
---|
| 433 | */
|
---|
| 434 | identityFunctionCheck: DevModeCheckFrequency;
|
---|
| 435 | }
|
---|
| 436 | /**
|
---|
| 437 | * Represents execution information for development mode checks.
|
---|
| 438 | *
|
---|
| 439 | * @public
|
---|
| 440 | * @since 5.0.0
|
---|
| 441 | */
|
---|
| 442 | type DevModeChecksExecutionInfo = {
|
---|
| 443 | [K in keyof DevModeChecks]: {
|
---|
| 444 | /**
|
---|
| 445 | * A boolean indicating whether the check should be executed.
|
---|
| 446 | */
|
---|
| 447 | shouldRun: boolean;
|
---|
| 448 | /**
|
---|
| 449 | * The function to execute for the check.
|
---|
| 450 | */
|
---|
| 451 | run: AnyFunction;
|
---|
| 452 | };
|
---|
| 453 | };
|
---|
| 454 | /**
|
---|
| 455 | * Determines the combined single "State" type (first arg) from all input selectors.
|
---|
| 456 | *
|
---|
| 457 | * @public
|
---|
| 458 | */
|
---|
| 459 | type GetStateFromSelectors<Selectors extends SelectorArray> = MergeParameters<Selectors>[0];
|
---|
| 460 | /**
|
---|
| 461 | * Determines the combined "Params" type (all remaining args) from all input selectors.
|
---|
| 462 | *
|
---|
| 463 | * @public
|
---|
| 464 | */
|
---|
| 465 | type GetParamsFromSelectors<Selectors extends SelectorArray> = ArrayTail<MergeParameters<Selectors>>;
|
---|
| 466 | /**
|
---|
| 467 | * Any Memoizer function. A memoizer is a function that accepts another function and returns it.
|
---|
| 468 | *
|
---|
| 469 | * @template FunctionType - The type of the function that is memoized.
|
---|
| 470 | *
|
---|
| 471 | * @public
|
---|
| 472 | */
|
---|
| 473 | type UnknownMemoizer<FunctionType extends UnknownFunction = UnknownFunction> = (func: FunctionType, ...options: any[]) => FunctionType;
|
---|
| 474 | /**
|
---|
| 475 | * Extracts the options type for a memoization function based on its parameters.
|
---|
| 476 | * The first parameter of the function is expected to be the function to be memoized,
|
---|
| 477 | * followed by options for the memoization process.
|
---|
| 478 | *
|
---|
| 479 | * @template MemoizeFunction - The type of the memoize function to be checked.
|
---|
| 480 | *
|
---|
| 481 | * @public
|
---|
| 482 | */
|
---|
| 483 | type MemoizeOptionsFromParameters<MemoizeFunction extends UnknownMemoizer> = (NonFunctionType<DropFirstParameter<MemoizeFunction>[0]> | FunctionType<DropFirstParameter<MemoizeFunction>[0]>) | (NonFunctionType<DropFirstParameter<MemoizeFunction>[number]> | FunctionType<DropFirstParameter<MemoizeFunction>[number]>)[];
|
---|
| 484 | /**
|
---|
| 485 | * Derive the type of memoize options object based on whether the memoize function itself was overridden.
|
---|
| 486 | *
|
---|
| 487 | * _This type can be used for both `memoizeOptions` and `argsMemoizeOptions`._
|
---|
| 488 | *
|
---|
| 489 | * @template MemoizeFunction - The type of the `memoize` or `argsMemoize` function initially passed into `createSelectorCreator`.
|
---|
| 490 | * @template OverrideMemoizeFunction - The type of the optional `memoize` or `argsMemoize` function passed directly into `createSelector` which then overrides the original `memoize` or `argsMemoize` function passed into `createSelectorCreator`.
|
---|
| 491 | *
|
---|
| 492 | * @public
|
---|
| 493 | */
|
---|
| 494 | type OverrideMemoizeOptions<MemoizeFunction extends UnknownMemoizer, OverrideMemoizeFunction extends UnknownMemoizer = never> = IfNever<OverrideMemoizeFunction, Simplify<MemoizeOptionsFromParameters<MemoizeFunction>>, Simplify<MemoizeOptionsFromParameters<OverrideMemoizeFunction>>>;
|
---|
| 495 | /**
|
---|
| 496 | * Extracts the additional properties or methods that a memoize function attaches to
|
---|
| 497 | * the function it memoizes (e.g., `clearCache`).
|
---|
| 498 | *
|
---|
| 499 | * @template MemoizeFunction - The type of the memoize function to be checked.
|
---|
| 500 | *
|
---|
| 501 | * @public
|
---|
| 502 | */
|
---|
| 503 | type ExtractMemoizerFields<MemoizeFunction extends UnknownMemoizer> = Simplify<OmitIndexSignature<ReturnType<MemoizeFunction>>>;
|
---|
| 504 | /**
|
---|
| 505 | * Represents the additional properties attached to a function memoized by `reselect`.
|
---|
| 506 | *
|
---|
| 507 | * `lruMemoize`, `weakMapMemoize` and `autotrackMemoize` all return these properties.
|
---|
| 508 | *
|
---|
| 509 | * @see {@linkcode ExtractMemoizerFields ExtractMemoizerFields}
|
---|
| 510 | *
|
---|
| 511 | * @public
|
---|
| 512 | */
|
---|
| 513 | type DefaultMemoizeFields = {
|
---|
| 514 | /**
|
---|
| 515 | * Clears the memoization cache associated with a memoized function.
|
---|
| 516 | * This method is typically used to reset the state of the cache, allowing
|
---|
| 517 | * for the garbage collection of previously memoized results and ensuring
|
---|
| 518 | * that future calls to the function recompute the results.
|
---|
| 519 | */
|
---|
| 520 | clearCache: () => void;
|
---|
| 521 | resultsCount: () => number;
|
---|
| 522 | resetResultsCount: () => void;
|
---|
| 523 | };
|
---|
| 524 | /**
|
---|
| 525 | * Any function with any arguments.
|
---|
| 526 | *
|
---|
| 527 | * @internal
|
---|
| 528 | */
|
---|
| 529 | type AnyFunction = (...args: any[]) => any;
|
---|
| 530 | /**
|
---|
| 531 | * Any function with unknown arguments.
|
---|
| 532 | *
|
---|
| 533 | * @internal
|
---|
| 534 | */
|
---|
| 535 | type UnknownFunction = (...args: unknown[]) => unknown;
|
---|
| 536 | /**
|
---|
| 537 | * When a generic type parameter is using its default value of `never`, fallback to a different type.
|
---|
| 538 | *
|
---|
| 539 | * @template T - Type to be checked.
|
---|
| 540 | * @template FallbackTo - Type to fallback to if `T` resolves to `never`.
|
---|
| 541 | *
|
---|
| 542 | * @internal
|
---|
| 543 | */
|
---|
| 544 | type FallbackIfNever<T, FallbackTo> = IfNever<T, FallbackTo, T>;
|
---|
| 545 | /**
|
---|
| 546 | * Extracts the non-function part of a type.
|
---|
| 547 | *
|
---|
| 548 | * @template T - The input type to be refined by excluding function types and index signatures.
|
---|
| 549 | *
|
---|
| 550 | * @internal
|
---|
| 551 | */
|
---|
| 552 | type NonFunctionType<T> = Simplify<OmitIndexSignature<Exclude<T, AnyFunction>>>;
|
---|
| 553 | /**
|
---|
| 554 | * Extracts the function part of a type.
|
---|
| 555 | *
|
---|
| 556 | * @template T - The input type to be refined by extracting function types.
|
---|
| 557 | *
|
---|
| 558 | * @internal
|
---|
| 559 | */
|
---|
| 560 | type FunctionType<T> = Extract<T, AnyFunction>;
|
---|
| 561 | /**
|
---|
| 562 | * Extracts the return type from all functions as a tuple.
|
---|
| 563 | *
|
---|
| 564 | * @internal
|
---|
| 565 | */
|
---|
| 566 | type ExtractReturnType<FunctionsArray extends readonly AnyFunction[]> = {
|
---|
| 567 | [Index in keyof FunctionsArray]: FunctionsArray[Index] extends FunctionsArray[number] ? FallbackIfUnknown<ReturnType<FunctionsArray[Index]>, any> : never;
|
---|
| 568 | };
|
---|
| 569 | /**
|
---|
| 570 | * Utility type to infer the type of "all params of a function except the first",
|
---|
| 571 | * so we can determine what arguments a memoize function accepts.
|
---|
| 572 | *
|
---|
| 573 | * @internal
|
---|
| 574 | */
|
---|
| 575 | type DropFirstParameter<Func extends AnyFunction> = Func extends (firstArg: any, ...restArgs: infer Rest) => any ? Rest : never;
|
---|
| 576 | /**
|
---|
| 577 | * Distributes over a type. It is used mostly to expand a function type
|
---|
| 578 | * in hover previews while preserving their original JSDoc information.
|
---|
| 579 | *
|
---|
| 580 | * If preserving JSDoc information is not a concern, you can use {@linkcode ExpandFunction ExpandFunction}.
|
---|
| 581 | *
|
---|
| 582 | * @template T The type to be distributed.
|
---|
| 583 | *
|
---|
| 584 | * @internal
|
---|
| 585 | */
|
---|
| 586 | type Distribute<T> = T extends T ? T : never;
|
---|
| 587 | /**
|
---|
| 588 | * Extracts the type of an array or tuple minus the first element.
|
---|
| 589 | *
|
---|
| 590 | * @internal
|
---|
| 591 | */
|
---|
| 592 | type ArrayTail<ArrayType> = ArrayType extends readonly [
|
---|
| 593 | unknown,
|
---|
| 594 | ...infer Tail
|
---|
| 595 | ] ? Tail : [];
|
---|
| 596 | /**
|
---|
| 597 | * An alias for type `{}`. Represents any value that is not `null` or `undefined`.
|
---|
| 598 | * It is mostly used for semantic purposes to help distinguish between an
|
---|
| 599 | * empty object type and `{}` as they are not the same.
|
---|
| 600 | *
|
---|
| 601 | * @internal
|
---|
| 602 | */
|
---|
| 603 | type AnyNonNullishValue = NonNullable<unknown>;
|
---|
| 604 | /**
|
---|
| 605 | * Same as {@linkcode AnyNonNullishValue AnyNonNullishValue} but aliased
|
---|
| 606 | * for semantic purposes. It is intended to be used in scenarios where
|
---|
| 607 | * a recursive type definition needs to be interrupted to ensure type safety
|
---|
| 608 | * and to avoid excessively deep recursion that could lead to performance issues.
|
---|
| 609 | *
|
---|
| 610 | * @internal
|
---|
| 611 | */
|
---|
| 612 | type InterruptRecursion = AnyNonNullishValue;
|
---|
| 613 | /**
|
---|
| 614 | * An if-else-like type that resolves depending on whether the given type is `never`.
|
---|
| 615 | * This is mainly used to conditionally resolve the type of a `memoizeOptions` object based on whether `memoize` is provided or not.
|
---|
| 616 | * @see {@link https://github.com/sindresorhus/type-fest/blob/main/source/if-never.d.ts Source}
|
---|
| 617 | *
|
---|
| 618 | * @internal
|
---|
| 619 | */
|
---|
| 620 | type IfNever<T, TypeIfNever, TypeIfNotNever> = [T] extends [never] ? TypeIfNever : TypeIfNotNever;
|
---|
| 621 | /**
|
---|
| 622 | * Omit any index signatures from the given object type, leaving only explicitly defined properties.
|
---|
| 623 | * This is mainly used to remove explicit `any`s from the return type of some memoizers (e.g, `microMemoize`).
|
---|
| 624 | *
|
---|
| 625 | * __Disclaimer:__ When used on an intersection of a function and an object,
|
---|
| 626 | * the function is erased.
|
---|
| 627 | *
|
---|
| 628 | * @see {@link https://github.com/sindresorhus/type-fest/blob/main/source/omit-index-signature.d.ts Source}
|
---|
| 629 | *
|
---|
| 630 | * @internal
|
---|
| 631 | */
|
---|
| 632 | type OmitIndexSignature<ObjectType> = {
|
---|
| 633 | [KeyType in keyof ObjectType as {} extends Record<KeyType, unknown> ? never : KeyType]: ObjectType[KeyType];
|
---|
| 634 | };
|
---|
| 635 | /**
|
---|
| 636 | * The infamous "convert a union type to an intersection type" hack
|
---|
| 637 | * @see {@link https://github.com/sindresorhus/type-fest/blob/main/source/union-to-intersection.d.ts Source}
|
---|
| 638 | * @see {@link https://github.com/microsoft/TypeScript/issues/29594 Reference}
|
---|
| 639 | *
|
---|
| 640 | * @internal
|
---|
| 641 | */
|
---|
| 642 | type UnionToIntersection<Union> = (Union extends unknown ? (distributedUnion: Union) => void : never) extends (mergedIntersection: infer Intersection) => void ? // The `& Union` is to allow indexing by the resulting type
|
---|
| 643 | Intersection & Union : never;
|
---|
| 644 | /**
|
---|
| 645 | * Code to convert a union of values into a tuple.
|
---|
| 646 | * @see {@link https://stackoverflow.com/a/55128956/62937 Source}
|
---|
| 647 | *
|
---|
| 648 | * @internal
|
---|
| 649 | */
|
---|
| 650 | type Push<T extends any[], V> = [...T, V];
|
---|
| 651 | /**
|
---|
| 652 | * @see {@link https://stackoverflow.com/a/55128956/62937 Source}
|
---|
| 653 | *
|
---|
| 654 | * @internal
|
---|
| 655 | */
|
---|
| 656 | type LastOf<T> = UnionToIntersection<T extends any ? () => T : never> extends () => infer R ? R : never;
|
---|
| 657 | /**
|
---|
| 658 | * TS4.1+
|
---|
| 659 | * @see {@link https://stackoverflow.com/a/55128956/62937 Source}
|
---|
| 660 | *
|
---|
| 661 | * @internal
|
---|
| 662 | */
|
---|
| 663 | type TuplifyUnion<T, L = LastOf<T>, N = [T] extends [never] ? true : false> = true extends N ? [] : Push<TuplifyUnion<Exclude<T, L>>, L>;
|
---|
| 664 | /**
|
---|
| 665 | * Converts "the values of an object" into a tuple, like a type-level `Object.values()`
|
---|
| 666 | * @see {@link https://stackoverflow.com/a/68695508/62937 Source}
|
---|
| 667 | *
|
---|
| 668 | * @internal
|
---|
| 669 | */
|
---|
| 670 | type ObjectValuesToTuple<T, KS extends any[] = TuplifyUnion<keyof T>, R extends any[] = []> = KS extends [infer K, ...infer KT] ? ObjectValuesToTuple<T, KT, [...R, T[K & keyof T]]> : R;
|
---|
| 671 | /**
|
---|
| 672 | * Create a type that makes the given keys required.
|
---|
| 673 | * The remaining keys are kept as is.
|
---|
| 674 | *
|
---|
| 675 | * @see {@link https://github.com/sindresorhus/type-fest/blob/main/source/set-required.d.ts Source}
|
---|
| 676 | *
|
---|
| 677 | * @internal
|
---|
| 678 | */
|
---|
| 679 | type SetRequired<BaseType, Keys extends keyof BaseType> = Omit<BaseType, Keys> & Required<Pick<BaseType, Keys>>;
|
---|
| 680 | /**
|
---|
| 681 | * An if-else-like type that resolves depending on whether the given type is `unknown`.
|
---|
| 682 | * @see {@link https://github.com/sindresorhus/type-fest/blob/main/source/if-unknown.d.ts Source}
|
---|
| 683 | *
|
---|
| 684 | * @internal
|
---|
| 685 | */
|
---|
| 686 | type IfUnknown<T, TypeIfUnknown, TypeIfNotUnknown> = unknown extends T ? [T] extends [null] ? TypeIfNotUnknown : TypeIfUnknown : TypeIfNotUnknown;
|
---|
| 687 | /**
|
---|
| 688 | * When a type is resolves to `unknown`, fallback to a different type.
|
---|
| 689 | *
|
---|
| 690 | * @template T - Type to be checked.
|
---|
| 691 | * @template FallbackTo - Type to fallback to if `T` resolves to `unknown`.
|
---|
| 692 | *
|
---|
| 693 | * @internal
|
---|
| 694 | */
|
---|
| 695 | type FallbackIfUnknown<T, FallbackTo> = IfUnknown<T, FallbackTo, T>;
|
---|
| 696 | /**
|
---|
| 697 | * Useful to flatten the type output to improve type hints shown in editors.
|
---|
| 698 | * And also to transform an interface into a type to aide with assignability.
|
---|
| 699 | * @see {@link https://github.com/sindresorhus/type-fest/blob/main/source/simplify.d.ts Source}
|
---|
| 700 | *
|
---|
| 701 | * @internal
|
---|
| 702 | */
|
---|
| 703 | type Simplify<T> = T extends AnyFunction ? T : {
|
---|
| 704 | [KeyType in keyof T]: T[KeyType];
|
---|
| 705 | } & AnyNonNullishValue;
|
---|
| 706 |
|
---|
| 707 | /**
|
---|
| 708 | * Uses an "auto-tracking" approach inspired by the work of the Ember Glimmer team.
|
---|
| 709 | * It uses a Proxy to wrap arguments and track accesses to nested fields
|
---|
| 710 | * in your selector on first read. Later, when the selector is called with
|
---|
| 711 | * new arguments, it identifies which accessed fields have changed and
|
---|
| 712 | * only recalculates the result if one or more of those accessed fields have changed.
|
---|
| 713 | * This allows it to be more precise than the shallow equality checks in `lruMemoize`.
|
---|
| 714 | *
|
---|
| 715 | * __Design Tradeoffs for `autotrackMemoize`:__
|
---|
| 716 | * - Pros:
|
---|
| 717 | * - It is likely to avoid excess calculations and recalculate fewer times than `lruMemoize` will,
|
---|
| 718 | * which may also result in fewer component re-renders.
|
---|
| 719 | * - Cons:
|
---|
| 720 | * - It only has a cache size of 1.
|
---|
| 721 | * - It is slower than `lruMemoize`, because it has to do more work. (How much slower is dependent on the number of accessed fields in a selector, number of calls, frequency of input changes, etc)
|
---|
| 722 | * - It can have some unexpected behavior. Because it tracks nested field accesses,
|
---|
| 723 | * cases where you don't access a field will not recalculate properly.
|
---|
| 724 | * For example, a badly-written selector like:
|
---|
| 725 | * ```ts
|
---|
| 726 | * createSelector([state => state.todos], todos => todos)
|
---|
| 727 | * ```
|
---|
| 728 | * that just immediately returns the extracted value will never update, because it doesn't see any field accesses to check.
|
---|
| 729 | *
|
---|
| 730 | * __Use Cases for `autotrackMemoize`:__
|
---|
| 731 | * - It is likely best used for cases where you need to access specific nested fields
|
---|
| 732 | * in data, and avoid recalculating if other fields in the same data objects are immutably updated.
|
---|
| 733 | *
|
---|
| 734 | * @param func - The function to be memoized.
|
---|
| 735 | * @returns A memoized function with a `.clearCache()` method attached.
|
---|
| 736 | *
|
---|
| 737 | * @example
|
---|
| 738 | * <caption>Using `createSelector`</caption>
|
---|
| 739 | * ```ts
|
---|
| 740 | * import { unstable_autotrackMemoize as autotrackMemoize, createSelector } from 'reselect'
|
---|
| 741 | *
|
---|
| 742 | * const selectTodoIds = createSelector(
|
---|
| 743 | * [(state: RootState) => state.todos],
|
---|
| 744 | * (todos) => todos.map(todo => todo.id),
|
---|
| 745 | * { memoize: autotrackMemoize }
|
---|
| 746 | * )
|
---|
| 747 | * ```
|
---|
| 748 | *
|
---|
| 749 | * @example
|
---|
| 750 | * <caption>Using `createSelectorCreator`</caption>
|
---|
| 751 | * ```ts
|
---|
| 752 | * import { unstable_autotrackMemoize as autotrackMemoize, createSelectorCreator } from 'reselect'
|
---|
| 753 | *
|
---|
| 754 | * const createSelectorAutotrack = createSelectorCreator({ memoize: autotrackMemoize })
|
---|
| 755 | *
|
---|
| 756 | * const selectTodoIds = createSelectorAutotrack(
|
---|
| 757 | * [(state: RootState) => state.todos],
|
---|
| 758 | * (todos) => todos.map(todo => todo.id)
|
---|
| 759 | * )
|
---|
| 760 | * ```
|
---|
| 761 | *
|
---|
| 762 | * @template Func - The type of the function that is memoized.
|
---|
| 763 | *
|
---|
| 764 | * @see {@link https://reselect.js.org/api/unstable_autotrackMemoize autotrackMemoize}
|
---|
| 765 | *
|
---|
| 766 | * @since 5.0.0
|
---|
| 767 | * @public
|
---|
| 768 | * @experimental
|
---|
| 769 | */
|
---|
| 770 | declare function autotrackMemoize<Func extends AnyFunction>(func: Func): Func & {
|
---|
| 771 | clearCache: () => void;
|
---|
| 772 | resultsCount: () => number;
|
---|
| 773 | resetResultsCount: () => void;
|
---|
| 774 | };
|
---|
| 775 |
|
---|
| 776 | /**
|
---|
| 777 | * An instance of `createSelector`, customized with a given memoize implementation.
|
---|
| 778 | *
|
---|
| 779 | * @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`).
|
---|
| 780 | * @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
|
---|
| 781 | * @template StateType - The type of state that the selectors created with this selector creator will operate on.
|
---|
| 782 | *
|
---|
| 783 | * @public
|
---|
| 784 | */
|
---|
| 785 | interface CreateSelectorFunction<MemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize, ArgsMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize, StateType = any> {
|
---|
| 786 | /**
|
---|
| 787 | * Creates a memoized selector function.
|
---|
| 788 | *
|
---|
| 789 | * @param createSelectorArgs - An arbitrary number of input selectors as separate inline arguments and a `combiner` function.
|
---|
| 790 | * @returns A memoized output selector.
|
---|
| 791 | *
|
---|
| 792 | * @template InputSelectors - The type of the input selectors as an array.
|
---|
| 793 | * @template Result - The return type of the `combiner` as well as the output selector.
|
---|
| 794 | * @template OverrideMemoizeFunction - The type of the optional `memoize` function that could be passed into the options object to override the original `memoize` function that was initially passed into `createSelectorCreator`.
|
---|
| 795 | * @template OverrideArgsMemoizeFunction - The type of the optional `argsMemoize` function that could be passed into the options object to override the original `argsMemoize` function that was initially passed into `createSelectorCreator`.
|
---|
| 796 | *
|
---|
| 797 | * @see {@link https://reselect.js.org/api/createselector `createSelector`}
|
---|
| 798 | */
|
---|
| 799 | <InputSelectors extends SelectorArray<StateType>, Result>(...createSelectorArgs: [
|
---|
| 800 | ...inputSelectors: InputSelectors,
|
---|
| 801 | combiner: Combiner<InputSelectors, Result>
|
---|
| 802 | ]): OutputSelector<InputSelectors, Result, MemoizeFunction, ArgsMemoizeFunction> & InterruptRecursion;
|
---|
| 803 | /**
|
---|
| 804 | * Creates a memoized selector function.
|
---|
| 805 | *
|
---|
| 806 | * @param createSelectorArgs - An arbitrary number of input selectors as separate inline arguments, a `combiner` function and an `options` object.
|
---|
| 807 | * @returns A memoized output selector.
|
---|
| 808 | *
|
---|
| 809 | * @template InputSelectors - The type of the input selectors as an array.
|
---|
| 810 | * @template Result - The return type of the `combiner` as well as the output selector.
|
---|
| 811 | * @template OverrideMemoizeFunction - The type of the optional `memoize` function that could be passed into the options object to override the original `memoize` function that was initially passed into `createSelectorCreator`.
|
---|
| 812 | * @template OverrideArgsMemoizeFunction - The type of the optional `argsMemoize` function that could be passed into the options object to override the original `argsMemoize` function that was initially passed into `createSelectorCreator`.
|
---|
| 813 | *
|
---|
| 814 | * @see {@link https://reselect.js.org/api/createselector `createSelector`}
|
---|
| 815 | */
|
---|
| 816 | <InputSelectors extends SelectorArray<StateType>, Result, OverrideMemoizeFunction extends UnknownMemoizer = MemoizeFunction, OverrideArgsMemoizeFunction extends UnknownMemoizer = ArgsMemoizeFunction>(...createSelectorArgs: [
|
---|
| 817 | ...inputSelectors: InputSelectors,
|
---|
| 818 | combiner: Combiner<InputSelectors, Result>,
|
---|
| 819 | createSelectorOptions: Simplify<CreateSelectorOptions<MemoizeFunction, ArgsMemoizeFunction, OverrideMemoizeFunction, OverrideArgsMemoizeFunction>>
|
---|
| 820 | ]): OutputSelector<InputSelectors, Result, OverrideMemoizeFunction, OverrideArgsMemoizeFunction> & InterruptRecursion;
|
---|
| 821 | /**
|
---|
| 822 | * Creates a memoized selector function.
|
---|
| 823 | *
|
---|
| 824 | * @param inputSelectors - An array of input selectors.
|
---|
| 825 | * @param combiner - A function that Combines the input selectors and returns an output selector. Otherwise known as the result function.
|
---|
| 826 | * @param createSelectorOptions - An optional options object that allows for further customization per selector.
|
---|
| 827 | * @returns A memoized output selector.
|
---|
| 828 | *
|
---|
| 829 | * @template InputSelectors - The type of the input selectors array.
|
---|
| 830 | * @template Result - The return type of the `combiner` as well as the output selector.
|
---|
| 831 | * @template OverrideMemoizeFunction - The type of the optional `memoize` function that could be passed into the options object to override the original `memoize` function that was initially passed into `createSelectorCreator`.
|
---|
| 832 | * @template OverrideArgsMemoizeFunction - The type of the optional `argsMemoize` function that could be passed into the options object to override the original `argsMemoize` function that was initially passed into `createSelectorCreator`.
|
---|
| 833 | *
|
---|
| 834 | * @see {@link https://reselect.js.org/api/createselector `createSelector`}
|
---|
| 835 | */
|
---|
| 836 | <InputSelectors extends SelectorArray<StateType>, Result, OverrideMemoizeFunction extends UnknownMemoizer = MemoizeFunction, OverrideArgsMemoizeFunction extends UnknownMemoizer = ArgsMemoizeFunction>(inputSelectors: [...InputSelectors], combiner: Combiner<InputSelectors, Result>, createSelectorOptions?: Simplify<CreateSelectorOptions<MemoizeFunction, ArgsMemoizeFunction, OverrideMemoizeFunction, OverrideArgsMemoizeFunction>>): OutputSelector<InputSelectors, Result, OverrideMemoizeFunction, OverrideArgsMemoizeFunction> & InterruptRecursion;
|
---|
| 837 | /**
|
---|
| 838 | * Creates a "pre-typed" version of {@linkcode createSelector createSelector}
|
---|
| 839 | * where the `state` type is predefined.
|
---|
| 840 | *
|
---|
| 841 | * This allows you to set the `state` type once, eliminating the need to
|
---|
| 842 | * specify it with every {@linkcode createSelector createSelector} call.
|
---|
| 843 | *
|
---|
| 844 | * @returns A pre-typed `createSelector` with the state type already defined.
|
---|
| 845 | *
|
---|
| 846 | * @example
|
---|
| 847 | * ```ts
|
---|
| 848 | * import { createSelector } from 'reselect'
|
---|
| 849 | *
|
---|
| 850 | * export interface RootState {
|
---|
| 851 | * todos: { id: number; completed: boolean }[]
|
---|
| 852 | * alerts: { id: number; read: boolean }[]
|
---|
| 853 | * }
|
---|
| 854 | *
|
---|
| 855 | * export const createAppSelector = createSelector.withTypes<RootState>()
|
---|
| 856 | *
|
---|
| 857 | * const selectTodoIds = createAppSelector(
|
---|
| 858 | * [
|
---|
| 859 | * // Type of `state` is set to `RootState`, no need to manually set the type
|
---|
| 860 | * state => state.todos
|
---|
| 861 | * ],
|
---|
| 862 | * todos => todos.map(({ id }) => id)
|
---|
| 863 | * )
|
---|
| 864 | * ```
|
---|
| 865 | * @template OverrideStateType - The specific type of state used by all selectors created with this selector creator.
|
---|
| 866 | *
|
---|
| 867 | * @see {@link https://reselect.js.org/api/createselector#defining-a-pre-typed-createselector `createSelector.withTypes`}
|
---|
| 868 | *
|
---|
| 869 | * @since 5.1.0
|
---|
| 870 | */
|
---|
| 871 | withTypes: <OverrideStateType extends StateType>() => CreateSelectorFunction<MemoizeFunction, ArgsMemoizeFunction, OverrideStateType>;
|
---|
| 872 | }
|
---|
| 873 | /**
|
---|
| 874 | * Creates a selector creator function with the specified memoization function
|
---|
| 875 | * and options for customizing memoization behavior.
|
---|
| 876 | *
|
---|
| 877 | * @param options - An options object containing the `memoize` function responsible for memoizing the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`). It also provides additional options for customizing memoization. While the `memoize` property is mandatory, the rest are optional.
|
---|
| 878 | * @returns A customized `createSelector` function.
|
---|
| 879 | *
|
---|
| 880 | * @example
|
---|
| 881 | * ```ts
|
---|
| 882 | * const customCreateSelector = createSelectorCreator({
|
---|
| 883 | * memoize: customMemoize, // Function to be used to memoize `resultFunc`
|
---|
| 884 | * memoizeOptions: [memoizeOption1, memoizeOption2], // Options passed to `customMemoize` as the second argument onwards
|
---|
| 885 | * argsMemoize: customArgsMemoize, // Function to be used to memoize the selector's arguments
|
---|
| 886 | * argsMemoizeOptions: [argsMemoizeOption1, argsMemoizeOption2] // Options passed to `customArgsMemoize` as the second argument onwards
|
---|
| 887 | * })
|
---|
| 888 | *
|
---|
| 889 | * const customSelector = customCreateSelector(
|
---|
| 890 | * [inputSelector1, inputSelector2],
|
---|
| 891 | * resultFunc // `resultFunc` will be passed as the first argument to `customMemoize`
|
---|
| 892 | * )
|
---|
| 893 | *
|
---|
| 894 | * customSelector(
|
---|
| 895 | * ...selectorArgs // Will be memoized by `customArgsMemoize`
|
---|
| 896 | * )
|
---|
| 897 | * ```
|
---|
| 898 | *
|
---|
| 899 | * @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`).
|
---|
| 900 | * @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
|
---|
| 901 | *
|
---|
| 902 | * @see {@link https://reselect.js.org/api/createSelectorCreator#using-options-since-500 `createSelectorCreator`}
|
---|
| 903 | *
|
---|
| 904 | * @since 5.0.0
|
---|
| 905 | * @public
|
---|
| 906 | */
|
---|
| 907 | declare function createSelectorCreator<MemoizeFunction extends UnknownMemoizer, ArgsMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize>(options: Simplify<SetRequired<CreateSelectorOptions<typeof weakMapMemoize, typeof weakMapMemoize, MemoizeFunction, ArgsMemoizeFunction>, 'memoize'>>): CreateSelectorFunction<MemoizeFunction, ArgsMemoizeFunction>;
|
---|
| 908 | /**
|
---|
| 909 | * Creates a selector creator function with the specified memoization function
|
---|
| 910 | * and options for customizing memoization behavior.
|
---|
| 911 | *
|
---|
| 912 | * @param memoize - The `memoize` function responsible for memoizing the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`).
|
---|
| 913 | * @param memoizeOptionsFromArgs - Optional configuration options for the memoization function. These options are then passed to the memoize function as the second argument onwards.
|
---|
| 914 | * @returns A customized `createSelector` function.
|
---|
| 915 | *
|
---|
| 916 | * @example
|
---|
| 917 | * ```ts
|
---|
| 918 | * const customCreateSelector = createSelectorCreator(customMemoize, // Function to be used to memoize `resultFunc`
|
---|
| 919 | * option1, // Will be passed as second argument to `customMemoize`
|
---|
| 920 | * option2, // Will be passed as third argument to `customMemoize`
|
---|
| 921 | * option3 // Will be passed as fourth argument to `customMemoize`
|
---|
| 922 | * )
|
---|
| 923 | *
|
---|
| 924 | * const customSelector = customCreateSelector(
|
---|
| 925 | * [inputSelector1, inputSelector2],
|
---|
| 926 | * resultFunc // `resultFunc` will be passed as the first argument to `customMemoize`
|
---|
| 927 | * )
|
---|
| 928 | * ```
|
---|
| 929 | *
|
---|
| 930 | * @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`).
|
---|
| 931 | *
|
---|
| 932 | * @see {@link https://reselect.js.org/api/createSelectorCreator#using-memoize-and-memoizeoptions `createSelectorCreator`}
|
---|
| 933 | *
|
---|
| 934 | * @public
|
---|
| 935 | */
|
---|
| 936 | declare function createSelectorCreator<MemoizeFunction extends UnknownMemoizer>(memoize: MemoizeFunction, ...memoizeOptionsFromArgs: DropFirstParameter<MemoizeFunction>): CreateSelectorFunction<MemoizeFunction>;
|
---|
| 937 | /**
|
---|
| 938 | * Accepts one or more "input selectors" (either as separate arguments or a single array),
|
---|
| 939 | * a single "result function" / "combiner", and an optional options object, and
|
---|
| 940 | * generates a memoized selector function.
|
---|
| 941 | *
|
---|
| 942 | * @see {@link https://reselect.js.org/api/createSelector `createSelector`}
|
---|
| 943 | *
|
---|
| 944 | * @public
|
---|
| 945 | */
|
---|
| 946 | declare const createSelector: CreateSelectorFunction<typeof weakMapMemoize, typeof weakMapMemoize, any>;
|
---|
| 947 |
|
---|
| 948 | /**
|
---|
| 949 | * Represents a mapping of selectors to their return types.
|
---|
| 950 | *
|
---|
| 951 | * @template TObject - An object type where each property is a selector function.
|
---|
| 952 | *
|
---|
| 953 | * @public
|
---|
| 954 | */
|
---|
| 955 | type SelectorResultsMap<TObject extends SelectorsObject> = {
|
---|
| 956 | [Key in keyof TObject]: ReturnType<TObject[Key]>;
|
---|
| 957 | };
|
---|
| 958 | /**
|
---|
| 959 | * Represents a mapping of selectors for each key in a given root state.
|
---|
| 960 | *
|
---|
| 961 | * This type is a utility that takes a root state object type and
|
---|
| 962 | * generates a corresponding set of selectors. Each selector is associated
|
---|
| 963 | * with a key in the root state, allowing for the selection
|
---|
| 964 | * of specific parts of the state.
|
---|
| 965 | *
|
---|
| 966 | * @template RootState - The type of the root state object.
|
---|
| 967 | *
|
---|
| 968 | * @since 5.0.0
|
---|
| 969 | * @public
|
---|
| 970 | */
|
---|
| 971 | type RootStateSelectors<RootState = any> = {
|
---|
| 972 | [Key in keyof RootState]: Selector<RootState, RootState[Key], []>;
|
---|
| 973 | };
|
---|
| 974 | /**
|
---|
| 975 | * @deprecated Please use {@linkcode StructuredSelectorCreator.withTypes createStructuredSelector.withTypes<RootState>()} instead. This type will be removed in the future.
|
---|
| 976 | * @template RootState - The type of the root state object.
|
---|
| 977 | *
|
---|
| 978 | * @since 5.0.0
|
---|
| 979 | * @public
|
---|
| 980 | */
|
---|
| 981 | type TypedStructuredSelectorCreator<RootState = any> =
|
---|
| 982 | /**
|
---|
| 983 | * A convenience function that simplifies returning an object
|
---|
| 984 | * made up of selector results.
|
---|
| 985 | *
|
---|
| 986 | * @param inputSelectorsObject - A key value pair consisting of input selectors.
|
---|
| 987 | * @param selectorCreator - A custom selector creator function. It defaults to `createSelector`.
|
---|
| 988 | * @returns A memoized structured selector.
|
---|
| 989 | *
|
---|
| 990 | * @example
|
---|
| 991 | * <caption>Modern Use Case</caption>
|
---|
| 992 | * ```ts
|
---|
| 993 | * import { createSelector, createStructuredSelector } from 'reselect'
|
---|
| 994 | *
|
---|
| 995 | * interface RootState {
|
---|
| 996 | * todos: {
|
---|
| 997 | * id: number
|
---|
| 998 | * completed: boolean
|
---|
| 999 | * title: string
|
---|
| 1000 | * description: string
|
---|
| 1001 | * }[]
|
---|
| 1002 | * alerts: { id: number; read: boolean }[]
|
---|
| 1003 | * }
|
---|
| 1004 | *
|
---|
| 1005 | * // This:
|
---|
| 1006 | * const structuredSelector = createStructuredSelector(
|
---|
| 1007 | * {
|
---|
| 1008 | * todos: (state: RootState) => state.todos,
|
---|
| 1009 | * alerts: (state: RootState) => state.alerts,
|
---|
| 1010 | * todoById: (state: RootState, id: number) => state.todos[id]
|
---|
| 1011 | * },
|
---|
| 1012 | * createSelector
|
---|
| 1013 | * )
|
---|
| 1014 | *
|
---|
| 1015 | * // Is essentially the same as this:
|
---|
| 1016 | * const selector = createSelector(
|
---|
| 1017 | * [
|
---|
| 1018 | * (state: RootState) => state.todos,
|
---|
| 1019 | * (state: RootState) => state.alerts,
|
---|
| 1020 | * (state: RootState, id: number) => state.todos[id]
|
---|
| 1021 | * ],
|
---|
| 1022 | * (todos, alerts, todoById) => {
|
---|
| 1023 | * return {
|
---|
| 1024 | * todos,
|
---|
| 1025 | * alerts,
|
---|
| 1026 | * todoById
|
---|
| 1027 | * }
|
---|
| 1028 | * }
|
---|
| 1029 | * )
|
---|
| 1030 | * ```
|
---|
| 1031 | *
|
---|
| 1032 | * @example
|
---|
| 1033 | * <caption>In your component:</caption>
|
---|
| 1034 | * ```tsx
|
---|
| 1035 | * import type { RootState } from 'createStructuredSelector/modernUseCase'
|
---|
| 1036 | * import { structuredSelector } from 'createStructuredSelector/modernUseCase'
|
---|
| 1037 | * import type { FC } from 'react'
|
---|
| 1038 | * import { useSelector } from 'react-redux'
|
---|
| 1039 | *
|
---|
| 1040 | * interface Props {
|
---|
| 1041 | * id: number
|
---|
| 1042 | * }
|
---|
| 1043 | *
|
---|
| 1044 | * const MyComponent: FC<Props> = ({ id }) => {
|
---|
| 1045 | * const { todos, alerts, todoById } = useSelector((state: RootState) =>
|
---|
| 1046 | * structuredSelector(state, id)
|
---|
| 1047 | * )
|
---|
| 1048 | *
|
---|
| 1049 | * return (
|
---|
| 1050 | * <div>
|
---|
| 1051 | * Next to do is:
|
---|
| 1052 | * <h2>{todoById.title}</h2>
|
---|
| 1053 | * <p>Description: {todoById.description}</p>
|
---|
| 1054 | * <ul>
|
---|
| 1055 | * <h3>All other to dos:</h3>
|
---|
| 1056 | * {todos.map(todo => (
|
---|
| 1057 | * <li key={todo.id}>{todo.title}</li>
|
---|
| 1058 | * ))}
|
---|
| 1059 | * </ul>
|
---|
| 1060 | * </div>
|
---|
| 1061 | * )
|
---|
| 1062 | * }
|
---|
| 1063 | * ```
|
---|
| 1064 | *
|
---|
| 1065 | * @example
|
---|
| 1066 | * <caption>Simple Use Case</caption>
|
---|
| 1067 | * ```ts
|
---|
| 1068 | * const selectA = state => state.a
|
---|
| 1069 | * const selectB = state => state.b
|
---|
| 1070 | *
|
---|
| 1071 | * // The result function in the following selector
|
---|
| 1072 | * // is simply building an object from the input selectors
|
---|
| 1073 | * const structuredSelector = createSelector(selectA, selectB, (a, b) => ({
|
---|
| 1074 | * a,
|
---|
| 1075 | * b
|
---|
| 1076 | * }))
|
---|
| 1077 | *
|
---|
| 1078 | * const result = structuredSelector({ a: 1, b: 2 }) // will produce { x: 1, y: 2 }
|
---|
| 1079 | * ```
|
---|
| 1080 | *
|
---|
| 1081 | * @template InputSelectorsObject - The shape of the input selectors object.
|
---|
| 1082 | * @template MemoizeFunction - The type of the memoize function that is used to create the structured selector. It defaults to `weakMapMemoize`.
|
---|
| 1083 | * @template ArgsMemoizeFunction - The type of the of the memoize function that is used to memoize the arguments passed into the generated structured selector. It defaults to `weakMapMemoize`.
|
---|
| 1084 | *
|
---|
| 1085 | * @see {@link https://reselect.js.org/api/createStructuredSelector `createStructuredSelector`}
|
---|
| 1086 | */
|
---|
| 1087 | <InputSelectorsObject extends RootStateSelectors<RootState> = RootStateSelectors<RootState>, MemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize, ArgsMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize>(inputSelectorsObject: InputSelectorsObject, selectorCreator?: CreateSelectorFunction<MemoizeFunction, ArgsMemoizeFunction>) => OutputSelector<ObjectValuesToTuple<InputSelectorsObject>, Simplify<SelectorResultsMap<InputSelectorsObject>>, MemoizeFunction, ArgsMemoizeFunction> & InterruptRecursion;
|
---|
| 1088 | /**
|
---|
| 1089 | * Represents an object where each property is a selector function.
|
---|
| 1090 | *
|
---|
| 1091 | * @template StateType - The type of state that all the selectors operate on.
|
---|
| 1092 | *
|
---|
| 1093 | * @public
|
---|
| 1094 | */
|
---|
| 1095 | type SelectorsObject<StateType = any> = Record<string, Selector<StateType>>;
|
---|
| 1096 | /**
|
---|
| 1097 | * It provides a way to create structured selectors.
|
---|
| 1098 | * The structured selector can take multiple input selectors
|
---|
| 1099 | * and map their output to an object with specific keys.
|
---|
| 1100 | *
|
---|
| 1101 | * @template StateType - The type of state that the structured selectors created with this structured selector creator will operate on.
|
---|
| 1102 | *
|
---|
| 1103 | * @see {@link https://reselect.js.org/api/createStructuredSelector `createStructuredSelector`}
|
---|
| 1104 | *
|
---|
| 1105 | * @public
|
---|
| 1106 | */
|
---|
| 1107 | interface StructuredSelectorCreator<StateType = any> {
|
---|
| 1108 | /**
|
---|
| 1109 | * A convenience function that simplifies returning an object
|
---|
| 1110 | * made up of selector results.
|
---|
| 1111 | *
|
---|
| 1112 | * @param inputSelectorsObject - A key value pair consisting of input selectors.
|
---|
| 1113 | * @param selectorCreator - A custom selector creator function. It defaults to `createSelector`.
|
---|
| 1114 | * @returns A memoized structured selector.
|
---|
| 1115 | *
|
---|
| 1116 | * @example
|
---|
| 1117 | * <caption>Modern Use Case</caption>
|
---|
| 1118 | * ```ts
|
---|
| 1119 | * import { createSelector, createStructuredSelector } from 'reselect'
|
---|
| 1120 | *
|
---|
| 1121 | * interface RootState {
|
---|
| 1122 | * todos: {
|
---|
| 1123 | * id: number
|
---|
| 1124 | * completed: boolean
|
---|
| 1125 | * title: string
|
---|
| 1126 | * description: string
|
---|
| 1127 | * }[]
|
---|
| 1128 | * alerts: { id: number; read: boolean }[]
|
---|
| 1129 | * }
|
---|
| 1130 | *
|
---|
| 1131 | * // This:
|
---|
| 1132 | * const structuredSelector = createStructuredSelector(
|
---|
| 1133 | * {
|
---|
| 1134 | * todos: (state: RootState) => state.todos,
|
---|
| 1135 | * alerts: (state: RootState) => state.alerts,
|
---|
| 1136 | * todoById: (state: RootState, id: number) => state.todos[id]
|
---|
| 1137 | * },
|
---|
| 1138 | * createSelector
|
---|
| 1139 | * )
|
---|
| 1140 | *
|
---|
| 1141 | * // Is essentially the same as this:
|
---|
| 1142 | * const selector = createSelector(
|
---|
| 1143 | * [
|
---|
| 1144 | * (state: RootState) => state.todos,
|
---|
| 1145 | * (state: RootState) => state.alerts,
|
---|
| 1146 | * (state: RootState, id: number) => state.todos[id]
|
---|
| 1147 | * ],
|
---|
| 1148 | * (todos, alerts, todoById) => {
|
---|
| 1149 | * return {
|
---|
| 1150 | * todos,
|
---|
| 1151 | * alerts,
|
---|
| 1152 | * todoById
|
---|
| 1153 | * }
|
---|
| 1154 | * }
|
---|
| 1155 | * )
|
---|
| 1156 | * ```
|
---|
| 1157 | *
|
---|
| 1158 | * @example
|
---|
| 1159 | * <caption>In your component:</caption>
|
---|
| 1160 | * ```tsx
|
---|
| 1161 | * import type { RootState } from 'createStructuredSelector/modernUseCase'
|
---|
| 1162 | * import { structuredSelector } from 'createStructuredSelector/modernUseCase'
|
---|
| 1163 | * import type { FC } from 'react'
|
---|
| 1164 | * import { useSelector } from 'react-redux'
|
---|
| 1165 | *
|
---|
| 1166 | * interface Props {
|
---|
| 1167 | * id: number
|
---|
| 1168 | * }
|
---|
| 1169 | *
|
---|
| 1170 | * const MyComponent: FC<Props> = ({ id }) => {
|
---|
| 1171 | * const { todos, alerts, todoById } = useSelector((state: RootState) =>
|
---|
| 1172 | * structuredSelector(state, id)
|
---|
| 1173 | * )
|
---|
| 1174 | *
|
---|
| 1175 | * return (
|
---|
| 1176 | * <div>
|
---|
| 1177 | * Next to do is:
|
---|
| 1178 | * <h2>{todoById.title}</h2>
|
---|
| 1179 | * <p>Description: {todoById.description}</p>
|
---|
| 1180 | * <ul>
|
---|
| 1181 | * <h3>All other to dos:</h3>
|
---|
| 1182 | * {todos.map(todo => (
|
---|
| 1183 | * <li key={todo.id}>{todo.title}</li>
|
---|
| 1184 | * ))}
|
---|
| 1185 | * </ul>
|
---|
| 1186 | * </div>
|
---|
| 1187 | * )
|
---|
| 1188 | * }
|
---|
| 1189 | * ```
|
---|
| 1190 | *
|
---|
| 1191 | * @example
|
---|
| 1192 | * <caption>Simple Use Case</caption>
|
---|
| 1193 | * ```ts
|
---|
| 1194 | * const selectA = state => state.a
|
---|
| 1195 | * const selectB = state => state.b
|
---|
| 1196 | *
|
---|
| 1197 | * // The result function in the following selector
|
---|
| 1198 | * // is simply building an object from the input selectors
|
---|
| 1199 | * const structuredSelector = createSelector(selectA, selectB, (a, b) => ({
|
---|
| 1200 | * a,
|
---|
| 1201 | * b
|
---|
| 1202 | * }))
|
---|
| 1203 | *
|
---|
| 1204 | * const result = structuredSelector({ a: 1, b: 2 }) // will produce { x: 1, y: 2 }
|
---|
| 1205 | * ```
|
---|
| 1206 | *
|
---|
| 1207 | * @template InputSelectorsObject - The shape of the input selectors object.
|
---|
| 1208 | * @template MemoizeFunction - The type of the memoize function that is used to create the structured selector. It defaults to `weakMapMemoize`.
|
---|
| 1209 | * @template ArgsMemoizeFunction - The type of the of the memoize function that is used to memoize the arguments passed into the generated structured selector. It defaults to `weakMapMemoize`.
|
---|
| 1210 | *
|
---|
| 1211 | * @see {@link https://reselect.js.org/api/createStructuredSelector `createStructuredSelector`}
|
---|
| 1212 | */
|
---|
| 1213 | <InputSelectorsObject extends SelectorsObject<StateType>, MemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize, ArgsMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize>(inputSelectorsObject: InputSelectorsObject, selectorCreator?: CreateSelectorFunction<MemoizeFunction, ArgsMemoizeFunction>): OutputSelector<ObjectValuesToTuple<InputSelectorsObject>, Simplify<SelectorResultsMap<InputSelectorsObject>>, MemoizeFunction, ArgsMemoizeFunction> & InterruptRecursion;
|
---|
| 1214 | /**
|
---|
| 1215 | * Creates a "pre-typed" version of
|
---|
| 1216 | * {@linkcode createStructuredSelector createStructuredSelector}
|
---|
| 1217 | * where the `state` type is predefined.
|
---|
| 1218 | *
|
---|
| 1219 | * This allows you to set the `state` type once, eliminating the need to
|
---|
| 1220 | * specify it with every
|
---|
| 1221 | * {@linkcode createStructuredSelector createStructuredSelector} call.
|
---|
| 1222 | *
|
---|
| 1223 | * @returns A pre-typed `createStructuredSelector` with the state type already defined.
|
---|
| 1224 | *
|
---|
| 1225 | * @example
|
---|
| 1226 | * ```ts
|
---|
| 1227 | * import { createStructuredSelector } from 'reselect'
|
---|
| 1228 | *
|
---|
| 1229 | * export interface RootState {
|
---|
| 1230 | * todos: { id: number; completed: boolean }[]
|
---|
| 1231 | * alerts: { id: number; read: boolean }[]
|
---|
| 1232 | * }
|
---|
| 1233 | *
|
---|
| 1234 | * export const createStructuredAppSelector =
|
---|
| 1235 | * createStructuredSelector.withTypes<RootState>()
|
---|
| 1236 | *
|
---|
| 1237 | * const structuredAppSelector = createStructuredAppSelector({
|
---|
| 1238 | * // Type of `state` is set to `RootState`, no need to manually set the type
|
---|
| 1239 | * todos: state => state.todos,
|
---|
| 1240 | * alerts: state => state.alerts,
|
---|
| 1241 | * todoById: (state, id: number) => state.todos[id]
|
---|
| 1242 | * })
|
---|
| 1243 | *
|
---|
| 1244 | * ```
|
---|
| 1245 | * @template OverrideStateType - The specific type of state used by all structured selectors created with this structured selector creator.
|
---|
| 1246 | *
|
---|
| 1247 | * @see {@link https://reselect.js.org/api/createstructuredselector#defining-a-pre-typed-createstructuredselector `createSelector.withTypes`}
|
---|
| 1248 | *
|
---|
| 1249 | * @since 5.1.0
|
---|
| 1250 | */
|
---|
| 1251 | withTypes: <OverrideStateType extends StateType>() => StructuredSelectorCreator<OverrideStateType>;
|
---|
| 1252 | }
|
---|
| 1253 | /**
|
---|
| 1254 | * A convenience function that simplifies returning an object
|
---|
| 1255 | * made up of selector results.
|
---|
| 1256 | *
|
---|
| 1257 | * @param inputSelectorsObject - A key value pair consisting of input selectors.
|
---|
| 1258 | * @param selectorCreator - A custom selector creator function. It defaults to `createSelector`.
|
---|
| 1259 | * @returns A memoized structured selector.
|
---|
| 1260 | *
|
---|
| 1261 | * @example
|
---|
| 1262 | * <caption>Modern Use Case</caption>
|
---|
| 1263 | * ```ts
|
---|
| 1264 | * import { createSelector, createStructuredSelector } from 'reselect'
|
---|
| 1265 | *
|
---|
| 1266 | * interface RootState {
|
---|
| 1267 | * todos: {
|
---|
| 1268 | * id: number
|
---|
| 1269 | * completed: boolean
|
---|
| 1270 | * title: string
|
---|
| 1271 | * description: string
|
---|
| 1272 | * }[]
|
---|
| 1273 | * alerts: { id: number; read: boolean }[]
|
---|
| 1274 | * }
|
---|
| 1275 | *
|
---|
| 1276 | * // This:
|
---|
| 1277 | * const structuredSelector = createStructuredSelector(
|
---|
| 1278 | * {
|
---|
| 1279 | * todos: (state: RootState) => state.todos,
|
---|
| 1280 | * alerts: (state: RootState) => state.alerts,
|
---|
| 1281 | * todoById: (state: RootState, id: number) => state.todos[id]
|
---|
| 1282 | * },
|
---|
| 1283 | * createSelector
|
---|
| 1284 | * )
|
---|
| 1285 | *
|
---|
| 1286 | * // Is essentially the same as this:
|
---|
| 1287 | * const selector = createSelector(
|
---|
| 1288 | * [
|
---|
| 1289 | * (state: RootState) => state.todos,
|
---|
| 1290 | * (state: RootState) => state.alerts,
|
---|
| 1291 | * (state: RootState, id: number) => state.todos[id]
|
---|
| 1292 | * ],
|
---|
| 1293 | * (todos, alerts, todoById) => {
|
---|
| 1294 | * return {
|
---|
| 1295 | * todos,
|
---|
| 1296 | * alerts,
|
---|
| 1297 | * todoById
|
---|
| 1298 | * }
|
---|
| 1299 | * }
|
---|
| 1300 | * )
|
---|
| 1301 | * ```
|
---|
| 1302 | *
|
---|
| 1303 | * @see {@link https://reselect.js.org/api/createStructuredSelector `createStructuredSelector`}
|
---|
| 1304 | *
|
---|
| 1305 | * @public
|
---|
| 1306 | */
|
---|
| 1307 | declare const createStructuredSelector: StructuredSelectorCreator;
|
---|
| 1308 |
|
---|
| 1309 | /**
|
---|
| 1310 | * Overrides the development mode checks settings for all selectors.
|
---|
| 1311 | *
|
---|
| 1312 | * Reselect performs additional checks in development mode to help identify and
|
---|
| 1313 | * warn about potential issues in selector behavior. This function allows you to
|
---|
| 1314 | * customize the behavior of these checks across all selectors in your application.
|
---|
| 1315 | *
|
---|
| 1316 | * **Note**: This setting can still be overridden per selector inside `createSelector`'s `options` object.
|
---|
| 1317 | * See {@link https://github.com/reduxjs/reselect#2-per-selector-by-passing-an-identityfunctioncheck-option-directly-to-createselector per-selector-configuration}
|
---|
| 1318 | * and {@linkcode CreateSelectorOptions.identityFunctionCheck identityFunctionCheck} for more details.
|
---|
| 1319 | *
|
---|
| 1320 | * _The development mode checks do not run in production builds._
|
---|
| 1321 | *
|
---|
| 1322 | * @param devModeChecks - An object specifying the desired settings for development mode checks. You can provide partial overrides. Unspecified settings will retain their current values.
|
---|
| 1323 | *
|
---|
| 1324 | * @example
|
---|
| 1325 | * ```ts
|
---|
| 1326 | * import { setGlobalDevModeChecks } from 'reselect'
|
---|
| 1327 | * import { DevModeChecks } from '../types'
|
---|
| 1328 | *
|
---|
| 1329 | * // Run only the first time the selector is called. (default)
|
---|
| 1330 | * setGlobalDevModeChecks({ inputStabilityCheck: 'once' })
|
---|
| 1331 | *
|
---|
| 1332 | * // Run every time the selector is called.
|
---|
| 1333 | * setGlobalDevModeChecks({ inputStabilityCheck: 'always' })
|
---|
| 1334 | *
|
---|
| 1335 | * // Never run the input stability check.
|
---|
| 1336 | * setGlobalDevModeChecks({ inputStabilityCheck: 'never' })
|
---|
| 1337 | *
|
---|
| 1338 | * // Run only the first time the selector is called. (default)
|
---|
| 1339 | * setGlobalDevModeChecks({ identityFunctionCheck: 'once' })
|
---|
| 1340 | *
|
---|
| 1341 | * // Run every time the selector is called.
|
---|
| 1342 | * setGlobalDevModeChecks({ identityFunctionCheck: 'always' })
|
---|
| 1343 | *
|
---|
| 1344 | * // Never run the identity function check.
|
---|
| 1345 | * setGlobalDevModeChecks({ identityFunctionCheck: 'never' })
|
---|
| 1346 | * ```
|
---|
| 1347 | * @see {@link https://reselect.js.org/api/development-only-stability-checks Development-Only Stability Checks}
|
---|
| 1348 | * @see {@link https://reselect.js.org/api/development-only-stability-checks#1-globally-through-setglobaldevmodechecks global-configuration}
|
---|
| 1349 | *
|
---|
| 1350 | * @since 5.0.0
|
---|
| 1351 | * @public
|
---|
| 1352 | */
|
---|
| 1353 | declare const setGlobalDevModeChecks: (devModeChecks: Partial<DevModeChecks>) => void;
|
---|
| 1354 |
|
---|
| 1355 | /**
|
---|
| 1356 | * Runs a simple reference equality check.
|
---|
| 1357 | * What {@linkcode lruMemoize lruMemoize} uses by default.
|
---|
| 1358 | *
|
---|
| 1359 | * **Note**: This function was previously known as `defaultEqualityCheck`.
|
---|
| 1360 | *
|
---|
| 1361 | * @public
|
---|
| 1362 | */
|
---|
| 1363 | declare const referenceEqualityCheck: EqualityFn;
|
---|
| 1364 | /**
|
---|
| 1365 | * Options for configuring the behavior of a function memoized with
|
---|
| 1366 | * LRU (Least Recently Used) caching.
|
---|
| 1367 | *
|
---|
| 1368 | * @template Result - The type of the return value of the memoized function.
|
---|
| 1369 | *
|
---|
| 1370 | * @public
|
---|
| 1371 | */
|
---|
| 1372 | interface LruMemoizeOptions<Result = any> {
|
---|
| 1373 | /**
|
---|
| 1374 | * Function used to compare the individual arguments of the
|
---|
| 1375 | * provided calculation function.
|
---|
| 1376 | *
|
---|
| 1377 | * @default referenceEqualityCheck
|
---|
| 1378 | */
|
---|
| 1379 | equalityCheck?: EqualityFn;
|
---|
| 1380 | /**
|
---|
| 1381 | * If provided, used to compare a newly generated output value against
|
---|
| 1382 | * previous values in the cache. If a match is found,
|
---|
| 1383 | * the old value is returned. This addresses the common
|
---|
| 1384 | * ```ts
|
---|
| 1385 | * todos.map(todo => todo.id)
|
---|
| 1386 | * ```
|
---|
| 1387 | * use case, where an update to another field in the original data causes
|
---|
| 1388 | * a recalculation due to changed references, but the output is still
|
---|
| 1389 | * effectively the same.
|
---|
| 1390 | *
|
---|
| 1391 | * @since 4.1.0
|
---|
| 1392 | */
|
---|
| 1393 | resultEqualityCheck?: EqualityFn<Result>;
|
---|
| 1394 | /**
|
---|
| 1395 | * The maximum size of the cache used by the selector.
|
---|
| 1396 | * A size greater than 1 means the selector will use an
|
---|
| 1397 | * LRU (Least Recently Used) cache, allowing for the caching of multiple
|
---|
| 1398 | * results based on different sets of arguments.
|
---|
| 1399 | *
|
---|
| 1400 | * @default 1
|
---|
| 1401 | */
|
---|
| 1402 | maxSize?: number;
|
---|
| 1403 | }
|
---|
| 1404 | /**
|
---|
| 1405 | * Creates a memoized version of a function with an optional
|
---|
| 1406 | * LRU (Least Recently Used) cache. The memoized function uses a cache to
|
---|
| 1407 | * store computed values. Depending on the `maxSize` option, it will use
|
---|
| 1408 | * either a singleton cache (for a single entry) or an
|
---|
| 1409 | * LRU cache (for multiple entries).
|
---|
| 1410 | *
|
---|
| 1411 | * **Note**: This function was previously known as `defaultMemoize`.
|
---|
| 1412 | *
|
---|
| 1413 | * @param func - The function to be memoized.
|
---|
| 1414 | * @param equalityCheckOrOptions - Either an equality check function or an options object.
|
---|
| 1415 | * @returns A memoized function with a `.clearCache()` method attached.
|
---|
| 1416 | *
|
---|
| 1417 | * @template Func - The type of the function that is memoized.
|
---|
| 1418 | *
|
---|
| 1419 | * @see {@link https://reselect.js.org/api/lruMemoize `lruMemoize`}
|
---|
| 1420 | *
|
---|
| 1421 | * @public
|
---|
| 1422 | */
|
---|
| 1423 | declare function lruMemoize<Func extends AnyFunction>(func: Func, equalityCheckOrOptions?: EqualityFn | LruMemoizeOptions<ReturnType<Func>>): Func & {
|
---|
| 1424 | clearCache: () => void;
|
---|
| 1425 | resultsCount: () => number;
|
---|
| 1426 | resetResultsCount: () => void;
|
---|
| 1427 | };
|
---|
| 1428 |
|
---|
| 1429 | export { Combiner, CreateSelectorFunction, CreateSelectorOptions, DefaultMemoizeFields, DevModeCheckFrequency, DevModeChecks, DevModeChecksExecutionInfo, EqualityFn, ExtractMemoizerFields, GetParamsFromSelectors, GetStateFromSelectors, LruMemoizeOptions, MemoizeOptionsFromParameters, OutputSelector, OutputSelectorFields, OverrideMemoizeOptions, RootStateSelectors, Selector, SelectorArray, SelectorResultArray, SelectorResultsMap, SelectorsObject, StructuredSelectorCreator, TypedStructuredSelectorCreator, UnknownMemoizer, WeakMapMemoizeOptions, createSelector, createSelectorCreator, createStructuredSelector, lruMemoize, referenceEqualityCheck, setGlobalDevModeChecks, autotrackMemoize as unstable_autotrackMemoize, weakMapMemoize };
|
---|