Skip to content

Commit

Permalink
pure more of the things
Browse files Browse the repository at this point in the history
  • Loading branch information
EskiMojo14 committed Feb 5, 2024
1 parent f559617 commit 162e482
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 68 deletions.
136 changes: 69 additions & 67 deletions packages/toolkit/src/createSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -840,32 +840,33 @@ function getType(slice: string, actionKey: string): string {
return `${slice}/${actionKey}`
}

export const reducerCreator: ReducerCreator<ReducerType.reducer> = {
type: ReducerType.reducer,
create(caseReducer: CaseReducer<any, 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,
)
},
handle({ type, reducerName }, reducer, context) {
context
.addCase(type, reducer as any)
.exposeCaseReducer(reducerName, reducer)
.exposeAction(reducerName, createAction(type))
},
}
export const reducerCreator: ReducerCreator<ReducerType.reducer> =
/*#__PURE__*/ {
type: ReducerType.reducer,
create(caseReducer: CaseReducer<any, 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,
)
},
handle({ type, reducerName }, reducer, context) {
context
.addCase(type, reducer as any)
.exposeCaseReducer(reducerName, reducer)
.exposeAction(reducerName, createAction(type))
},
}

export const preparedReducerCreator: ReducerCreator<ReducerType.reducerWithPrepare> =
{
/*#__PURE__*/ {
type: ReducerType.reducerWithPrepare,
create(prepare, reducer) {
return {
Expand All @@ -882,53 +883,54 @@ export const preparedReducerCreator: ReducerCreator<ReducerType.reducerWithPrepa
},
}

export const asyncThunkCreator: ReducerCreator<ReducerType.asyncThunk> = {
type: ReducerType.asyncThunk,
create: /* @__PURE__ */ (() => {
function asyncThunk(
payloadCreator: AsyncThunkPayloadCreator<any, any>,
config: AsyncThunkSliceReducerConfig<any, any>,
): AsyncThunkSliceReducerDefinition<any, any> {
return {
_reducerDefinitionType: ReducerType.asyncThunk,
payloadCreator,
...config,
export const asyncThunkCreator: ReducerCreator<ReducerType.asyncThunk> =
/*#__PURE__*/ {
type: ReducerType.asyncThunk,
create: /* @__PURE__ */ (() => {
function asyncThunk(
payloadCreator: AsyncThunkPayloadCreator<any, any>,
config: AsyncThunkSliceReducerConfig<any, any>,
): AsyncThunkSliceReducerDefinition<any, any> {
return {
_reducerDefinitionType: ReducerType.asyncThunk,
payloadCreator,
...config,
}
}
asyncThunk.withTypes = () => asyncThunk
return asyncThunk as AsyncThunkCreator<any>
})(),
handle({ type, reducerName }, definition, context) {
const { payloadCreator, fulfilled, pending, rejected, settled, options } =
definition
const thunk = createAsyncThunk(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)
}
}
asyncThunk.withTypes = () => asyncThunk
return asyncThunk as AsyncThunkCreator<any>
})(),
handle({ type, reducerName }, definition, context) {
const { payloadCreator, fulfilled, pending, rejected, settled, options } =
definition
const thunk = createAsyncThunk(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,
})
},
}
context.exposeCaseReducer(reducerName, {
fulfilled: fulfilled || noop,
pending: pending || noop,
rejected: rejected || noop,
settled: settled || noop,
})
},
}

function noop() {}

const isCreatorCallback = <
const isCreatorCallback = /*#__PURE__*/ <
State,
CreatorMap extends Record<string, RegisteredReducerType>,
>(
Expand Down Expand Up @@ -1298,4 +1300,4 @@ function wrapSelector<State, NewState, S extends Selector<State>>(
*
* @public
*/
export const createSlice = buildCreateSlice()
export const createSlice = /*#__PURE__*/ buildCreateSlice()
2 changes: 1 addition & 1 deletion packages/toolkit/src/entities/slice_creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {
} from './models'
import { capitalize } from './utils'

export const entityMethodsCreatorType = Symbol()
export const entityMethodsCreatorType = /*#__PURE__*/ Symbol()

type DefaultPlural<Single extends string> = Single extends ''
? ''
Expand Down

0 comments on commit 162e482

Please sign in to comment.