Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 10 additions & 70 deletions packages/toolkit/src/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import type {
} from './createAsyncThunk'

/** @public */
export type ActionMatchingAnyOf<Matchers extends [...Matcher<any>[]]> =
export type ActionMatchingAnyOf<Matchers extends Matcher<any>[]> =
ActionFromMatcher<Matchers[number]>

/** @public */
export type ActionMatchingAllOf<Matchers extends [...Matcher<any>[]]> =
export type ActionMatchingAllOf<Matchers extends Matcher<any>[]> =
UnionToIntersection<ActionMatchingAnyOf<Matchers>>

const matches = (matcher: Matcher<any>, action: any) => {
Expand All @@ -36,7 +36,7 @@ const matches = (matcher: Matcher<any>, action: any) => {
*
* @public
*/
export function isAnyOf<Matchers extends [...Matcher<any>[]]>(
export function isAnyOf<Matchers extends Matcher<any>[]>(
...matchers: Matchers
) {
return (action: any): action is ActionMatchingAnyOf<Matchers> => {
Expand All @@ -53,7 +53,7 @@ export function isAnyOf<Matchers extends [...Matcher<any>[]]>(
*
* @public
*/
export function isAllOf<Matchers extends [...Matcher<any>[]]>(
export function isAllOf<Matchers extends Matcher<any>[]>(
...matchers: Matchers
) {
return (action: any): action is ActionMatchingAllOf<Matchers> => {
Expand Down Expand Up @@ -136,18 +136,7 @@ export function isPending<
return isPending()(asyncThunks[0])
}

return (
action: any,
): action is PendingActionFromAsyncThunk<AsyncThunks[number]> => {
// note: this type will be correct because we have at least 1 asyncThunk
const matchers: [Matcher<any>, ...Matcher<any>[]] = asyncThunks.map(
(asyncThunk) => asyncThunk.pending,
) as any

const combinedMatcher = isAnyOf(...matchers)

return combinedMatcher(action)
}
return isAnyOf(...asyncThunks.map((asyncThunk) => asyncThunk.pending))
}

export type UnknownAsyncThunkRejectedAction = ReturnType<
Expand Down Expand Up @@ -199,18 +188,7 @@ export function isRejected<
return isRejected()(asyncThunks[0])
}

return (
action: any,
): action is RejectedActionFromAsyncThunk<AsyncThunks[number]> => {
// note: this type will be correct because we have at least 1 asyncThunk
const matchers: [Matcher<any>, ...Matcher<any>[]] = asyncThunks.map(
(asyncThunk) => asyncThunk.rejected,
) as any

const combinedMatcher = isAnyOf(...matchers)

return combinedMatcher(action)
}
return isAnyOf(...asyncThunks.map((asyncThunk) => asyncThunk.rejected))
}

export type UnknownAsyncThunkRejectedWithValueAction = ReturnType<
Expand Down Expand Up @@ -264,24 +242,14 @@ export function isRejectedWithValue<
}

if (asyncThunks.length === 0) {
return (action: any) => {
const combinedMatcher = isAllOf(isRejected(...asyncThunks), hasFlag)

return combinedMatcher(action)
}
return isAllOf(isRejected(...asyncThunks), hasFlag)
}

if (!isAsyncThunkArray(asyncThunks)) {
return isRejectedWithValue()(asyncThunks[0])
}

return (
action: any,
): action is RejectedActionFromAsyncThunk<AsyncThunks[number]> => {
const combinedMatcher = isAllOf(isRejected(...asyncThunks), hasFlag)

return combinedMatcher(action)
}
return isAllOf(isRejected(...asyncThunks), hasFlag)
}

export type UnknownAsyncThunkFulfilledAction = ReturnType<
Expand Down Expand Up @@ -333,18 +301,7 @@ export function isFulfilled<
return isFulfilled()(asyncThunks[0])
}

return (
action: any,
): action is FulfilledActionFromAsyncThunk<AsyncThunks[number]> => {
// note: this type will be correct because we have at least 1 asyncThunk
const matchers: [Matcher<any>, ...Matcher<any>[]] = asyncThunks.map(
(asyncThunk) => asyncThunk.fulfilled,
) as any

const combinedMatcher = isAnyOf(...matchers)

return combinedMatcher(action)
}
return isAnyOf(...asyncThunks.map((asyncThunk) => asyncThunk.fulfilled))
}

export type UnknownAsyncThunkAction =
Expand Down Expand Up @@ -404,22 +361,5 @@ export function isAsyncThunkAction<
return isAsyncThunkAction()(asyncThunks[0])
}

return (
action: any,
): action is ActionsFromAsyncThunk<AsyncThunks[number]> => {
// note: this type will be correct because we have at least 1 asyncThunk
const matchers: [Matcher<any>, ...Matcher<any>[]] = [] as any

for (const asyncThunk of asyncThunks) {
matchers.push(
asyncThunk.pending,
asyncThunk.rejected,
asyncThunk.fulfilled,
)
}

const combinedMatcher = isAnyOf(...matchers)

return combinedMatcher(action)
}
return isAnyOf(...asyncThunks.flatMap(asyncThunk => [asyncThunk.pending, asyncThunk.rejected, asyncThunk.fulfilled]))
}