From 7db308572f3c1b26b08a6a09afddcdeec9f0cac0 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Thu, 2 May 2024 11:08:22 +0100 Subject: [PATCH 1/4] try out simplifying matcher code --- packages/toolkit/src/matchers.ts | 84 ++++++-------------------------- 1 file changed, 15 insertions(+), 69 deletions(-) diff --git a/packages/toolkit/src/matchers.ts b/packages/toolkit/src/matchers.ts index 6a5ccea09f..117410cd96 100644 --- a/packages/toolkit/src/matchers.ts +++ b/packages/toolkit/src/matchers.ts @@ -12,11 +12,11 @@ import type { } from './createAsyncThunk' /** @public */ -export type ActionMatchingAnyOf[]]> = +export type ActionMatchingAnyOf[]> = ActionFromMatcher /** @public */ -export type ActionMatchingAllOf[]]> = +export type ActionMatchingAllOf[]> = UnionToIntersection> const matches = (matcher: Matcher, action: any) => { @@ -36,7 +36,7 @@ const matches = (matcher: Matcher, action: any) => { * * @public */ -export function isAnyOf[]]>( +export function isAnyOf[]>( ...matchers: Matchers ) { return (action: any): action is ActionMatchingAnyOf => { @@ -53,7 +53,7 @@ export function isAnyOf[]]>( * * @public */ -export function isAllOf[]]>( +export function isAllOf[]>( ...matchers: Matchers ) { return (action: any): action is ActionMatchingAllOf => { @@ -136,18 +136,7 @@ export function isPending< return isPending()(asyncThunks[0]) } - return ( - action: any, - ): action is PendingActionFromAsyncThunk => { - // note: this type will be correct because we have at least 1 asyncThunk - const matchers: [Matcher, ...Matcher[]] = 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< @@ -199,18 +188,7 @@ export function isRejected< return isRejected()(asyncThunks[0]) } - return ( - action: any, - ): action is RejectedActionFromAsyncThunk => { - // note: this type will be correct because we have at least 1 asyncThunk - const matchers: [Matcher, ...Matcher[]] = 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< @@ -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 => { - const combinedMatcher = isAllOf(isRejected(...asyncThunks), hasFlag) - - return combinedMatcher(action) - } + return isAllOf(isRejected(...asyncThunks), hasFlag) } export type UnknownAsyncThunkFulfilledAction = ReturnType< @@ -333,18 +301,7 @@ export function isFulfilled< return isFulfilled()(asyncThunks[0]) } - return ( - action: any, - ): action is FulfilledActionFromAsyncThunk => { - // note: this type will be correct because we have at least 1 asyncThunk - const matchers: [Matcher, ...Matcher[]] = asyncThunks.map( - (asyncThunk) => asyncThunk.fulfilled, - ) as any - - const combinedMatcher = isAnyOf(...matchers) - - return combinedMatcher(action) - } + return isAnyOf(...asyncThunks.map((asyncThunk) => asyncThunk.fulfilled)) } export type UnknownAsyncThunkAction = @@ -404,22 +361,11 @@ export function isAsyncThunkAction< return isAsyncThunkAction()(asyncThunks[0]) } - return ( - action: any, - ): action is ActionsFromAsyncThunk => { - // note: this type will be correct because we have at least 1 asyncThunk - const matchers: [Matcher, ...Matcher[]] = [] as any - - for (const asyncThunk of asyncThunks) { - matchers.push( - asyncThunk.pending, - asyncThunk.rejected, - asyncThunk.fulfilled, - ) - } - - const combinedMatcher = isAnyOf(...matchers) - - return combinedMatcher(action) + const matchers: Matcher[] = [] + + for (const asyncThunk of asyncThunks) { + matchers.push(asyncThunk.pending, asyncThunk.rejected, asyncThunk.fulfilled) } + + return isAnyOf(...matchers) } From 6cabf376b20a3039fcef7f7b4bde4649c558a4d8 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Thu, 2 May 2024 11:15:29 +0100 Subject: [PATCH 2/4] try removing identical return statement --- packages/toolkit/src/matchers.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/toolkit/src/matchers.ts b/packages/toolkit/src/matchers.ts index 117410cd96..adb55da7eb 100644 --- a/packages/toolkit/src/matchers.ts +++ b/packages/toolkit/src/matchers.ts @@ -241,10 +241,6 @@ export function isRejectedWithValue< return action && action.meta && action.meta.rejectedWithValue } - if (asyncThunks.length === 0) { - return isAllOf(isRejected(...asyncThunks), hasFlag) - } - if (!isAsyncThunkArray(asyncThunks)) { return isRejectedWithValue()(asyncThunks[0]) } From 9376ac86cb8f3966b820b6cd06cefaafce260ede Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Thu, 2 May 2024 11:29:46 +0100 Subject: [PATCH 3/4] add if condition back to prevent call stack issue --- packages/toolkit/src/matchers.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/toolkit/src/matchers.ts b/packages/toolkit/src/matchers.ts index adb55da7eb..117410cd96 100644 --- a/packages/toolkit/src/matchers.ts +++ b/packages/toolkit/src/matchers.ts @@ -241,6 +241,10 @@ export function isRejectedWithValue< return action && action.meta && action.meta.rejectedWithValue } + if (asyncThunks.length === 0) { + return isAllOf(isRejected(...asyncThunks), hasFlag) + } + if (!isAsyncThunkArray(asyncThunks)) { return isRejectedWithValue()(asyncThunks[0]) } From bbb3f229dae2a47d0142eeb2c5fc0640a422bf73 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Thu, 2 May 2024 20:28:08 +0100 Subject: [PATCH 4/4] use flatMap instead of for loop Co-authored-by: Lenz Weber-Tronic --- packages/toolkit/src/matchers.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/packages/toolkit/src/matchers.ts b/packages/toolkit/src/matchers.ts index 117410cd96..6cb6d194d6 100644 --- a/packages/toolkit/src/matchers.ts +++ b/packages/toolkit/src/matchers.ts @@ -361,11 +361,5 @@ export function isAsyncThunkAction< return isAsyncThunkAction()(asyncThunks[0]) } - const matchers: Matcher[] = [] - - for (const asyncThunk of asyncThunks) { - matchers.push(asyncThunk.pending, asyncThunk.rejected, asyncThunk.fulfilled) - } - - return isAnyOf(...matchers) + return isAnyOf(...asyncThunks.flatMap(asyncThunk => [asyncThunk.pending, asyncThunk.rejected, asyncThunk.fulfilled])) }