Skip to content

Commit

Permalink
Merge pull request #2518 from JulienKode/fix/2448-remove-abort-contro…
Browse files Browse the repository at this point in the history
…ller
  • Loading branch information
markerikson committed Aug 26, 2023
2 parents c8f77a9 + c23f7eb commit 08f3892
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 42 deletions.
34 changes: 1 addition & 33 deletions packages/toolkit/src/createAsyncThunk.ts
Expand Up @@ -553,36 +553,6 @@ export const createAsyncThunk = (() => {
})
)

let displayedWarning = false

const AC =
typeof AbortController !== 'undefined'
? AbortController
: class implements AbortController {
signal = {
aborted: false,
addEventListener() {},
dispatchEvent() {
return false
},
onabort() {},
removeEventListener() {},
reason: undefined,
throwIfAborted() {},
}
abort() {
if (process.env.NODE_ENV !== 'production') {
if (!displayedWarning) {
displayedWarning = true
console.info(
`This platform does not implement AbortController.
If you want to use the AbortController to react to \`abort\` events, please consider importing a polyfill like 'abortcontroller-polyfill/dist/abortcontroller-polyfill-only'.`
)
}
}
}
}

function actionCreator(
arg: ThunkArg
): AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig> {
Expand All @@ -591,10 +561,9 @@ If you want to use the AbortController to react to \`abort\` events, please cons
? options.idGenerator(arg)
: nanoid()

const abortController = new AC()
const abortController = new AbortController()
let abortReason: string | undefined

let started = false
function abort(reason?: string) {
abortReason = reason
abortController.abort()
Expand All @@ -615,7 +584,6 @@ If you want to use the AbortController to react to \`abort\` events, please cons
message: 'Aborted due to condition callback returning false.',
}
}
started = true

const abortedPromise = new Promise<never>((_, reject) =>
abortController.signal.addEventListener('abort', () =>
Expand Down
11 changes: 2 additions & 9 deletions packages/toolkit/src/tests/createAsyncThunk.test.ts
Expand Up @@ -505,22 +505,15 @@ describe('createAsyncThunk with abortController', () => {
vi.resetModules()
})

test('calling `abort` on an asyncThunk works with a FallbackAbortController if no global abortController is not available', async () => {
test('calling a thunk made with createAsyncThunk should fail if no global abortController is not available', async () => {
const longRunningAsyncThunk = freshlyLoadedModule.createAsyncThunk(
'longRunning',
async () => {
await new Promise((resolve) => setTimeout(resolve, 30000))
}
)

store.dispatch(longRunningAsyncThunk()).abort()
// should only log once, even if called twice
store.dispatch(longRunningAsyncThunk()).abort()

expect(getLog().log).toMatchInlineSnapshot(`
"This platform does not implement AbortController.
If you want to use the AbortController to react to \`abort\` events, please consider importing a polyfill like 'abortcontroller-polyfill/dist/abortcontroller-polyfill-only'."
`)
expect(longRunningAsyncThunk()).toThrow("AbortController is not defined")
})
})
})
Expand Down

0 comments on commit 08f3892

Please sign in to comment.