Spaces:
Sleeping
Sleeping
File size: 32,319 Bytes
c2b7eb3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 | import type { Action, Reducer, UnknownAction } from 'redux'
import type { Selector } from 'reselect'
import type { InjectConfig } from './combineSlices'
import type {
ActionCreatorWithoutPayload,
PayloadAction,
PayloadActionCreator,
PrepareAction,
_ActionCreatorWithPreparedPayload,
} from './createAction'
import { createAction } from './createAction'
import type {
AsyncThunk,
AsyncThunkConfig,
AsyncThunkOptions,
AsyncThunkPayloadCreator,
OverrideThunkApiConfigs,
} from './createAsyncThunk'
import { createAsyncThunk as _createAsyncThunk } from './createAsyncThunk'
import type {
ActionMatcherDescriptionCollection,
CaseReducer,
ReducerWithInitialState,
} from './createReducer'
import { createReducer } from './createReducer'
import type {
ActionReducerMapBuilder,
AsyncThunkReducers,
TypedActionCreator,
} from './mapBuilders'
import { executeReducerBuilderCallback } from './mapBuilders'
import type { Id, TypeGuard } from './tsHelpers'
import { getOrInsertComputed } from './utils'
const asyncThunkSymbol = /* @__PURE__ */ Symbol.for(
'rtk-slice-createasyncthunk',
)
// type is annotated because it's too long to infer
export const asyncThunkCreator: {
[asyncThunkSymbol]: typeof _createAsyncThunk
} = {
[asyncThunkSymbol]: _createAsyncThunk,
}
type InjectIntoConfig<NewReducerPath extends string> = InjectConfig & {
reducerPath?: NewReducerPath
}
/**
* The return value of `createSlice`
*
* @public
*/
export interface Slice<
State = any,
CaseReducers extends SliceCaseReducers<State> = SliceCaseReducers<State>,
Name extends string = string,
ReducerPath extends string = Name,
Selectors extends SliceSelectors<State> = SliceSelectors<State>,
> {
/**
* The slice name.
*/
name: Name
/**
* The slice reducer path.
*/
reducerPath: ReducerPath
/**
* The slice's reducer.
*/
reducer: Reducer<State>
/**
* Action creators for the types of actions that are handled by the slice
* reducer.
*/
actions: CaseReducerActions<CaseReducers, Name>
/**
* The individual case reducer functions that were passed in the `reducers` parameter.
* This enables reuse and testing if they were defined inline when calling `createSlice`.
*/
caseReducers: SliceDefinedCaseReducers<CaseReducers>
/**
* Provides access to the initial state value given to the slice.
* If a lazy state initializer was provided, it will be called and a fresh value returned.
*/
getInitialState: () => State
/**
* Get localised slice selectors (expects to be called with *just* the slice's state as the first parameter)
*/
getSelectors(): Id<SliceDefinedSelectors<State, Selectors, State>>
/**
* Get globalised slice selectors (`selectState` callback is expected to receive first parameter and return slice state)
*/
getSelectors<RootState>(
selectState: (rootState: RootState) => State,
): Id<SliceDefinedSelectors<State, Selectors, RootState>>
/**
* Selectors that assume the slice's state is `rootState[slice.reducerPath]` (which is usually the case)
*
* Equivalent to `slice.getSelectors((state: RootState) => state[slice.reducerPath])`.
*/
get selectors(): Id<
SliceDefinedSelectors<State, Selectors, { [K in ReducerPath]: State }>
>
/**
* Inject slice into provided reducer (return value from `combineSlices`), and return injected slice.
*/
injectInto<NewReducerPath extends string = ReducerPath>(
this: this,
injectable: {
inject: (
slice: { reducerPath: string; reducer: Reducer },
config?: InjectConfig,
) => void
},
config?: InjectIntoConfig<NewReducerPath>,
): InjectedSlice<State, CaseReducers, Name, NewReducerPath, Selectors>
/**
* Select the slice state, using the slice's current reducerPath.
*
* Will throw an error if slice is not found.
*/
selectSlice(state: { [K in ReducerPath]: State }): State
}
/**
* A slice after being called with `injectInto(reducer)`.
*
* Selectors can now be called with an `undefined` value, in which case they use the slice's initial state.
*/
type InjectedSlice<
State = any,
CaseReducers extends SliceCaseReducers<State> = SliceCaseReducers<State>,
Name extends string = string,
ReducerPath extends string = Name,
Selectors extends SliceSelectors<State> = SliceSelectors<State>,
> = Omit<
Slice<State, CaseReducers, Name, ReducerPath, Selectors>,
'getSelectors' | 'selectors'
> & {
/**
* Get localised slice selectors (expects to be called with *just* the slice's state as the first parameter)
*/
getSelectors(): Id<SliceDefinedSelectors<State, Selectors, State | undefined>>
/**
* Get globalised slice selectors (`selectState` callback is expected to receive first parameter and return slice state)
*/
getSelectors<RootState>(
selectState: (rootState: RootState) => State | undefined,
): Id<SliceDefinedSelectors<State, Selectors, RootState>>
/**
* Selectors that assume the slice's state is `rootState[slice.name]` (which is usually the case)
*
* Equivalent to `slice.getSelectors((state: RootState) => state[slice.name])`.
*/
get selectors(): Id<
SliceDefinedSelectors<
State,
Selectors,
{ [K in ReducerPath]?: State | undefined }
>
>
/**
* Select the slice state, using the slice's current reducerPath.
*
* Returns initial state if slice is not found.
*/
selectSlice(state: { [K in ReducerPath]?: State | undefined }): State
}
/**
* Options for `createSlice()`.
*
* @public
*/
export interface CreateSliceOptions<
State = any,
CR extends SliceCaseReducers<State> = SliceCaseReducers<State>,
Name extends string = string,
ReducerPath extends string = Name,
Selectors extends SliceSelectors<State> = SliceSelectors<State>,
> {
/**
* The slice's name. Used to namespace the generated action types.
*/
name: Name
/**
* The slice's reducer path. Used when injecting into a combined slice reducer.
*/
reducerPath?: ReducerPath
/**
* The initial state that should be used when the reducer is called the first time. This may also be a "lazy initializer" function, which should return an initial state value when called. This will be used whenever the reducer is called with `undefined` as its state value, and is primarily useful for cases like reading initial state from `localStorage`.
*/
initialState: State | (() => State)
/**
* A mapping from action types to action-type-specific *case reducer*
* functions. For every action type, a matching action creator will be
* generated using `createAction()`.
*/
reducers:
| ValidateSliceCaseReducers<State, CR>
| ((creators: ReducerCreators<State>) => CR)
/**
* A callback that receives a *builder* object to define
* case reducers via calls to `builder.addCase(actionCreatorOrType, reducer)`.
*
*
* @example
```ts
import { createAction, createSlice, Action } from '@reduxjs/toolkit'
const incrementBy = createAction<number>('incrementBy')
const decrement = createAction('decrement')
interface RejectedAction extends Action {
error: Error
}
function isRejectedAction(action: Action): action is RejectedAction {
return action.type.endsWith('rejected')
}
createSlice({
name: 'counter',
initialState: 0,
reducers: {},
extraReducers: builder => {
builder
.addCase(incrementBy, (state, action) => {
// action is inferred correctly here if using TS
})
// You can chain calls, or have separate `builder.addCase()` lines each time
.addCase(decrement, (state, action) => {})
// You can match a range of action types
.addMatcher(
isRejectedAction,
// `action` will be inferred as a RejectedAction due to isRejectedAction being defined as a type guard
(state, action) => {}
)
// and provide a default case if no other handlers matched
.addDefaultCase((state, action) => {})
}
})
```
*/
extraReducers?: (builder: ActionReducerMapBuilder<State>) => void
/**
* A map of selectors that receive the slice's state and any additional arguments, and return a result.
*/
selectors?: Selectors
}
export enum ReducerType {
reducer = 'reducer',
reducerWithPrepare = 'reducerWithPrepare',
asyncThunk = 'asyncThunk',
}
type ReducerDefinition<T extends ReducerType = ReducerType> = {
_reducerDefinitionType: T
}
export type CaseReducerDefinition<
S = any,
A extends Action = UnknownAction,
> = CaseReducer<S, A> & ReducerDefinition<ReducerType.reducer>
/**
* A CaseReducer with a `prepare` method.
*
* @public
*/
export type CaseReducerWithPrepare<State, Action extends PayloadAction> = {
reducer: CaseReducer<State, Action>
prepare: PrepareAction<Action['payload']>
}
export interface CaseReducerWithPrepareDefinition<
State,
Action extends PayloadAction,
> extends CaseReducerWithPrepare<State, Action>,
ReducerDefinition<ReducerType.reducerWithPrepare> {}
type AsyncThunkSliceReducerConfig<
State,
ThunkArg extends any,
Returned = unknown,
ThunkApiConfig extends AsyncThunkConfig = {},
> = AsyncThunkReducers<State, ThunkArg, Returned, ThunkApiConfig> & {
options?: AsyncThunkOptions<ThunkArg, ThunkApiConfig>
}
type AsyncThunkSliceReducerDefinition<
State,
ThunkArg extends any,
Returned = unknown,
ThunkApiConfig extends AsyncThunkConfig = {},
> = AsyncThunkSliceReducerConfig<State, ThunkArg, Returned, ThunkApiConfig> &
ReducerDefinition<ReducerType.asyncThunk> & {
payloadCreator: AsyncThunkPayloadCreator<Returned, ThunkArg, ThunkApiConfig>
}
/**
* Providing these as part of the config would cause circular types, so we disallow passing them
*/
type PreventCircular<ThunkApiConfig> = {
[K in keyof ThunkApiConfig]: K extends 'state' | 'dispatch'
? never
: ThunkApiConfig[K]
}
interface AsyncThunkCreator<
State,
CurriedThunkApiConfig extends
PreventCircular<AsyncThunkConfig> = PreventCircular<AsyncThunkConfig>,
> {
<Returned, ThunkArg = void>(
payloadCreator: AsyncThunkPayloadCreator<
Returned,
ThunkArg,
CurriedThunkApiConfig
>,
config?: AsyncThunkSliceReducerConfig<
State,
ThunkArg,
Returned,
CurriedThunkApiConfig
>,
): AsyncThunkSliceReducerDefinition<
State,
ThunkArg,
Returned,
CurriedThunkApiConfig
>
<
Returned,
ThunkArg,
ThunkApiConfig extends PreventCircular<AsyncThunkConfig> = {},
>(
payloadCreator: AsyncThunkPayloadCreator<
Returned,
ThunkArg,
ThunkApiConfig
>,
config?: AsyncThunkSliceReducerConfig<
State,
ThunkArg,
Returned,
ThunkApiConfig
>,
): AsyncThunkSliceReducerDefinition<State, ThunkArg, Returned, ThunkApiConfig>
withTypes<
ThunkApiConfig extends PreventCircular<AsyncThunkConfig>,
>(): AsyncThunkCreator<
State,
OverrideThunkApiConfigs<CurriedThunkApiConfig, ThunkApiConfig>
>
}
export interface ReducerCreators<State> {
reducer(
caseReducer: CaseReducer<State, PayloadAction>,
): CaseReducerDefinition<State, PayloadAction>
reducer<Payload>(
caseReducer: CaseReducer<State, PayloadAction<Payload>>,
): CaseReducerDefinition<State, PayloadAction<Payload>>
asyncThunk: AsyncThunkCreator<State>
preparedReducer<Prepare extends PrepareAction<any>>(
prepare: Prepare,
reducer: CaseReducer<
State,
ReturnType<_ActionCreatorWithPreparedPayload<Prepare>>
>,
): {
_reducerDefinitionType: ReducerType.reducerWithPrepare
prepare: Prepare
reducer: CaseReducer<
State,
ReturnType<_ActionCreatorWithPreparedPayload<Prepare>>
>
}
}
/**
* The type describing a slice's `reducers` option.
*
* @public
*/
export type SliceCaseReducers<State> =
| Record<string, ReducerDefinition>
| Record<
string,
| CaseReducer<State, PayloadAction<any>>
| CaseReducerWithPrepare<State, PayloadAction<any, string, any, any>>
>
/**
* The type describing a slice's `selectors` option.
*/
export type SliceSelectors<State> = {
[K: string]: (sliceState: State, ...args: any[]) => any
}
type SliceActionType<
SliceName extends string,
ActionName extends keyof any,
> = ActionName extends string | number ? `${SliceName}/${ActionName}` : string
/**
* Derives the slice's `actions` property from the `reducers` options
*
* @public
*/
export type CaseReducerActions<
CaseReducers extends SliceCaseReducers<any>,
SliceName extends string,
> = {
[Type in keyof CaseReducers]: CaseReducers[Type] extends infer Definition
? Definition extends { prepare: any }
? ActionCreatorForCaseReducerWithPrepare<
Definition,
SliceActionType<SliceName, Type>
>
: Definition extends AsyncThunkSliceReducerDefinition<
any,
infer ThunkArg,
infer Returned,
infer ThunkApiConfig
>
? AsyncThunk<Returned, ThunkArg, ThunkApiConfig>
: Definition extends { reducer: any }
? ActionCreatorForCaseReducer<
Definition['reducer'],
SliceActionType<SliceName, Type>
>
: ActionCreatorForCaseReducer<
Definition,
SliceActionType<SliceName, Type>
>
: never
}
/**
* Get a `PayloadActionCreator` type for a passed `CaseReducerWithPrepare`
*
* @internal
*/
type ActionCreatorForCaseReducerWithPrepare<
CR extends { prepare: any },
Type extends string,
> = _ActionCreatorWithPreparedPayload<CR['prepare'], Type>
/**
* Get a `PayloadActionCreator` type for a passed `CaseReducer`
*
* @internal
*/
type ActionCreatorForCaseReducer<CR, Type extends string> = CR extends (
state: any,
action: infer Action,
) => any
? Action extends { payload: infer P }
? PayloadActionCreator<P, Type>
: ActionCreatorWithoutPayload<Type>
: ActionCreatorWithoutPayload<Type>
/**
* Extracts the CaseReducers out of a `reducers` object, even if they are
* tested into a `CaseReducerWithPrepare`.
*
* @internal
*/
type SliceDefinedCaseReducers<CaseReducers extends SliceCaseReducers<any>> = {
[Type in keyof CaseReducers]: CaseReducers[Type] extends infer Definition
? Definition extends AsyncThunkSliceReducerDefinition<any, any, any>
? Id<
Pick<
Required<Definition>,
'fulfilled' | 'rejected' | 'pending' | 'settled'
>
>
: Definition extends {
reducer: infer Reducer
}
? Reducer
: Definition
: never
}
type RemappedSelector<S extends Selector, NewState> =
S extends Selector<any, infer R, infer P>
? Selector<NewState, R, P> & { unwrapped: S }
: never
/**
* Extracts the final selector type from the `selectors` object.
*
* Removes the `string` index signature from the default value.
*/
type SliceDefinedSelectors<
State,
Selectors extends SliceSelectors<State>,
RootState,
> = {
[K in keyof Selectors as string extends K ? never : K]: RemappedSelector<
Selectors[K],
RootState
>
}
/**
* Used on a SliceCaseReducers object.
* Ensures that if a CaseReducer is a `CaseReducerWithPrepare`, that
* the `reducer` and the `prepare` function use the same type of `payload`.
*
* Might do additional such checks in the future.
*
* This type is only ever useful if you want to write your own wrapper around
* `createSlice`. Please don't use it otherwise!
*
* @public
*/
export type ValidateSliceCaseReducers<
S,
ACR extends SliceCaseReducers<S>,
> = ACR & {
[T in keyof ACR]: ACR[T] extends {
reducer(s: S, action?: infer A): any
}
? {
prepare(...a: never[]): Omit<A, 'type'>
}
: {}
}
function getType(slice: string, actionKey: string): string {
return `${slice}/${actionKey}`
}
interface BuildCreateSliceConfig {
creators?: {
asyncThunk?: typeof asyncThunkCreator
}
}
export function buildCreateSlice({ creators }: BuildCreateSliceConfig = {}) {
const cAT = creators?.asyncThunk?.[asyncThunkSymbol]
return function createSlice<
State,
CaseReducers extends SliceCaseReducers<State>,
Name extends string,
Selectors extends SliceSelectors<State>,
ReducerPath extends string = Name,
>(
options: CreateSliceOptions<
State,
CaseReducers,
Name,
ReducerPath,
Selectors
>,
): Slice<State, CaseReducers, Name, ReducerPath, Selectors> {
const { name, reducerPath = name as unknown as ReducerPath } = options
if (!name) {
throw new Error('`name` is a required option for createSlice')
}
if (
typeof process !== 'undefined' &&
process.env.NODE_ENV === 'development'
) {
if (options.initialState === undefined) {
console.error(
'You must provide an `initialState` value that is not `undefined`. You may have misspelled `initialState`',
)
}
}
const reducers =
(typeof options.reducers === 'function'
? options.reducers(buildReducerCreators<State>())
: options.reducers) || {}
const reducerNames = Object.keys(reducers)
const context: ReducerHandlingContext<State> = {
sliceCaseReducersByName: {},
sliceCaseReducersByType: {},
actionCreators: {},
sliceMatchers: [],
}
const contextMethods: ReducerHandlingContextMethods<State> = {
addCase(
typeOrActionCreator: string | TypedActionCreator<any>,
reducer: CaseReducer<State>,
) {
const type =
typeof typeOrActionCreator === 'string'
? typeOrActionCreator
: typeOrActionCreator.type
if (!type) {
throw new Error(
'`context.addCase` cannot be called with an empty action type',
)
}
if (type in context.sliceCaseReducersByType) {
throw new Error(
'`context.addCase` cannot be called with two reducers for the same action type: ' +
type,
)
}
context.sliceCaseReducersByType[type] = reducer
return contextMethods
},
addMatcher(matcher, reducer) {
context.sliceMatchers.push({ matcher, reducer })
return contextMethods
},
exposeAction(name, actionCreator) {
context.actionCreators[name] = actionCreator
return contextMethods
},
exposeCaseReducer(name, reducer) {
context.sliceCaseReducersByName[name] = reducer
return contextMethods
},
}
reducerNames.forEach((reducerName) => {
const reducerDefinition = reducers[reducerName]
const reducerDetails: ReducerDetails = {
reducerName,
type: getType(name, reducerName),
createNotation: typeof options.reducers === 'function',
}
if (isAsyncThunkSliceReducerDefinition<State>(reducerDefinition)) {
handleThunkCaseReducerDefinition(
reducerDetails,
reducerDefinition,
contextMethods,
cAT,
)
} else {
handleNormalReducerDefinition<State>(
reducerDetails,
reducerDefinition as any,
contextMethods,
)
}
})
function buildReducer() {
if (process.env.NODE_ENV !== 'production') {
if (typeof options.extraReducers === 'object') {
throw new Error(
"The object notation for `createSlice.extraReducers` has been removed. Please use the 'builder callback' notation instead: https://redux-toolkit.js.org/api/createSlice",
)
}
}
const [
extraReducers = {},
actionMatchers = [],
defaultCaseReducer = undefined,
] =
typeof options.extraReducers === 'function'
? executeReducerBuilderCallback(options.extraReducers)
: [options.extraReducers]
const finalCaseReducers = {
...extraReducers,
...context.sliceCaseReducersByType,
}
return createReducer(options.initialState, (builder) => {
for (let key in finalCaseReducers) {
builder.addCase(key, finalCaseReducers[key] as CaseReducer<any>)
}
for (let sM of context.sliceMatchers) {
builder.addMatcher(sM.matcher, sM.reducer)
}
for (let m of actionMatchers) {
builder.addMatcher(m.matcher, m.reducer)
}
if (defaultCaseReducer) {
builder.addDefaultCase(defaultCaseReducer)
}
})
}
const selectSelf = (state: State) => state
const injectedSelectorCache = new Map<
boolean,
WeakMap<
(rootState: any) => State | undefined,
Record<string, (rootState: any) => any>
>
>()
const injectedStateCache = new WeakMap<(rootState: any) => State, State>()
let _reducer: ReducerWithInitialState<State>
function reducer(state: State | undefined, action: UnknownAction) {
if (!_reducer) _reducer = buildReducer()
return _reducer(state, action)
}
function getInitialState() {
if (!_reducer) _reducer = buildReducer()
return _reducer.getInitialState()
}
function makeSelectorProps<CurrentReducerPath extends string = ReducerPath>(
reducerPath: CurrentReducerPath,
injected = false,
): Pick<
Slice<State, CaseReducers, Name, CurrentReducerPath, Selectors>,
'getSelectors' | 'selectors' | 'selectSlice' | 'reducerPath'
> {
function selectSlice(state: { [K in CurrentReducerPath]: State }) {
let sliceState = state[reducerPath]
if (typeof sliceState === 'undefined') {
if (injected) {
sliceState = getOrInsertComputed(
injectedStateCache,
selectSlice,
getInitialState,
)
} else if (process.env.NODE_ENV !== 'production') {
throw new Error(
'selectSlice returned undefined for an uninjected slice reducer',
)
}
}
return sliceState
}
function getSelectors(
selectState: (rootState: any) => State = selectSelf,
) {
const selectorCache = getOrInsertComputed(
injectedSelectorCache,
injected,
() => new WeakMap(),
)
return getOrInsertComputed(selectorCache, selectState, () => {
const map: Record<string, Selector<any, any>> = {}
for (const [name, selector] of Object.entries(
options.selectors ?? {},
)) {
map[name] = wrapSelector(
selector,
selectState,
() =>
getOrInsertComputed(
injectedStateCache,
selectState,
getInitialState,
),
injected,
)
}
return map
}) as any
}
return {
reducerPath,
getSelectors,
get selectors() {
return getSelectors(selectSlice)
},
selectSlice,
}
}
const slice: Slice<State, CaseReducers, Name, ReducerPath, Selectors> = {
name,
reducer,
actions: context.actionCreators as any,
caseReducers: context.sliceCaseReducersByName as any,
getInitialState,
...makeSelectorProps(reducerPath),
injectInto(injectable, { reducerPath: pathOpt, ...config } = {}) {
const newReducerPath = pathOpt ?? reducerPath
injectable.inject({ reducerPath: newReducerPath, reducer }, config)
return {
...slice,
...makeSelectorProps(newReducerPath, true),
} as any
},
}
return slice
}
}
function wrapSelector<State, NewState, S extends Selector<State>>(
selector: S,
selectState: Selector<NewState, State>,
getInitialState: () => State,
injected?: boolean,
) {
function wrapper(rootState: NewState, ...args: any[]) {
let sliceState = selectState(rootState)
if (typeof sliceState === 'undefined') {
if (injected) {
sliceState = getInitialState()
} else if (process.env.NODE_ENV !== 'production') {
throw new Error(
'selectState returned undefined for an uninjected slice reducer',
)
}
}
return selector(sliceState, ...args)
}
wrapper.unwrapped = selector
return wrapper as RemappedSelector<S, NewState>
}
/**
* A function that accepts an initial state, an object full of reducer
* functions, and a "slice name", and automatically generates
* action creators and action types that correspond to the
* reducers and state.
*
* @public
*/
export const createSlice = /* @__PURE__ */ buildCreateSlice()
interface ReducerHandlingContext<State> {
sliceCaseReducersByName: Record<
string,
| CaseReducer<State, any>
| Pick<
AsyncThunkSliceReducerDefinition<State, any, any, any>,
'fulfilled' | 'rejected' | 'pending' | 'settled'
>
>
sliceCaseReducersByType: Record<string, CaseReducer<State, any>>
sliceMatchers: ActionMatcherDescriptionCollection<State>
actionCreators: Record<string, Function>
}
interface ReducerHandlingContextMethods<State> {
/**
* Adds a case reducer to handle a single action type.
* @param actionCreator - Either a plain action type string, or an action creator generated by [`createAction`](./createAction) that can be used to determine the action type.
* @param reducer - The actual case reducer function.
*/
addCase<ActionCreator extends TypedActionCreator<string>>(
actionCreator: ActionCreator,
reducer: CaseReducer<State, ReturnType<ActionCreator>>,
): ReducerHandlingContextMethods<State>
/**
* Adds a case reducer to handle a single action type.
* @param actionCreator - Either a plain action type string, or an action creator generated by [`createAction`](./createAction) that can be used to determine the action type.
* @param reducer - The actual case reducer function.
*/
addCase<Type extends string, A extends Action<Type>>(
type: Type,
reducer: CaseReducer<State, A>,
): ReducerHandlingContextMethods<State>
/**
* Allows you to match incoming actions against your own filter function instead of only the `action.type` property.
* @remarks
* If multiple matcher reducers match, all of them will be executed in the order
* they were defined in - even if a case reducer already matched.
* All calls to `builder.addMatcher` must come after any calls to `builder.addCase` and before any calls to `builder.addDefaultCase`.
* @param matcher - A matcher function. In TypeScript, this should be a [type predicate](https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates)
* function
* @param reducer - The actual case reducer function.
*
*/
addMatcher<A>(
matcher: TypeGuard<A>,
reducer: CaseReducer<State, A extends Action ? A : A & Action>,
): ReducerHandlingContextMethods<State>
/**
* Add an action to be exposed under the final `slice.actions` key.
* @param name The key to be exposed as.
* @param actionCreator The action to expose.
* @example
* context.exposeAction("addPost", createAction<Post>("addPost"));
*
* export const { addPost } = slice.actions
*
* dispatch(addPost(post))
*/
exposeAction(
name: string,
actionCreator: Function,
): ReducerHandlingContextMethods<State>
/**
* Add a case reducer to be exposed under the final `slice.caseReducers` key.
* @param name The key to be exposed as.
* @param reducer The reducer to expose.
* @example
* context.exposeCaseReducer("addPost", (state, action: PayloadAction<Post>) => {
* state.push(action.payload)
* })
*
* slice.caseReducers.addPost([], addPost(post))
*/
exposeCaseReducer(
name: string,
reducer:
| CaseReducer<State, any>
| Pick<
AsyncThunkSliceReducerDefinition<State, any, any, any>,
'fulfilled' | 'rejected' | 'pending' | 'settled'
>,
): ReducerHandlingContextMethods<State>
}
interface ReducerDetails {
/** The key the reducer was defined under */
reducerName: string
/** The predefined action type, i.e. `${slice.name}/${reducerName}` */
type: string
/** Whether create. notation was used when defining reducers */
createNotation: boolean
}
function buildReducerCreators<State>(): ReducerCreators<State> {
function asyncThunk(
payloadCreator: AsyncThunkPayloadCreator<any, any>,
config: AsyncThunkSliceReducerConfig<State, any>,
): AsyncThunkSliceReducerDefinition<State, any> {
return {
_reducerDefinitionType: ReducerType.asyncThunk,
payloadCreator,
...config,
}
}
asyncThunk.withTypes = () => asyncThunk
return {
reducer(caseReducer: CaseReducer<State, any>) {
return Object.assign(
{
// hack so the wrapping function has the same name as the original
// we need to create a wrapper so the `reducerDefinitionType` is not assigned to the original
[caseReducer.name](...args: Parameters<typeof caseReducer>) {
return caseReducer(...args)
},
}[caseReducer.name],
{
_reducerDefinitionType: ReducerType.reducer,
} as const,
)
},
preparedReducer(prepare, reducer) {
return {
_reducerDefinitionType: ReducerType.reducerWithPrepare,
prepare,
reducer,
}
},
asyncThunk: asyncThunk as any,
}
}
function handleNormalReducerDefinition<State>(
{ type, reducerName, createNotation }: ReducerDetails,
maybeReducerWithPrepare:
| CaseReducer<State, { payload: any; type: string }>
| CaseReducerWithPrepare<State, PayloadAction<any, string, any, any>>,
context: ReducerHandlingContextMethods<State>,
) {
let caseReducer: CaseReducer<State, any>
let prepareCallback: PrepareAction<any> | undefined
if ('reducer' in maybeReducerWithPrepare) {
if (
createNotation &&
!isCaseReducerWithPrepareDefinition(maybeReducerWithPrepare)
) {
throw new Error(
'Please use the `create.preparedReducer` notation for prepared action creators with the `create` notation.',
)
}
caseReducer = maybeReducerWithPrepare.reducer
prepareCallback = maybeReducerWithPrepare.prepare
} else {
caseReducer = maybeReducerWithPrepare
}
context
.addCase(type, caseReducer)
.exposeCaseReducer(reducerName, caseReducer)
.exposeAction(
reducerName,
prepareCallback
? createAction(type, prepareCallback)
: createAction(type),
)
}
function isAsyncThunkSliceReducerDefinition<State>(
reducerDefinition: any,
): reducerDefinition is AsyncThunkSliceReducerDefinition<State, any, any, any> {
return reducerDefinition._reducerDefinitionType === ReducerType.asyncThunk
}
function isCaseReducerWithPrepareDefinition<State>(
reducerDefinition: any,
): reducerDefinition is CaseReducerWithPrepareDefinition<State, any> {
return (
reducerDefinition._reducerDefinitionType === ReducerType.reducerWithPrepare
)
}
function handleThunkCaseReducerDefinition<State>(
{ type, reducerName }: ReducerDetails,
reducerDefinition: AsyncThunkSliceReducerDefinition<State, any, any, any>,
context: ReducerHandlingContextMethods<State>,
cAT: typeof _createAsyncThunk | undefined,
) {
if (!cAT) {
throw new Error(
'Cannot use `create.asyncThunk` in the built-in `createSlice`. ' +
'Use `buildCreateSlice({ creators: { asyncThunk: asyncThunkCreator } })` to create a customised version of `createSlice`.',
)
}
const { payloadCreator, fulfilled, pending, rejected, settled, options } =
reducerDefinition
const thunk = cAT(type, payloadCreator, options as any)
context.exposeAction(reducerName, thunk)
if (fulfilled) {
context.addCase(thunk.fulfilled, fulfilled)
}
if (pending) {
context.addCase(thunk.pending, pending)
}
if (rejected) {
context.addCase(thunk.rejected, rejected)
}
if (settled) {
context.addMatcher(thunk.settled, settled)
}
context.exposeCaseReducer(reducerName, {
fulfilled: fulfilled || noop,
pending: pending || noop,
rejected: rejected || noop,
settled: settled || noop,
})
}
function noop() {}
|