Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(2448): remove abort controller polyfill #2518

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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