feat(useAsyncEffect): provide an AbortSignal to the effect and abort it on cleanup#385
Open
haejunejung wants to merge 1 commit into
Open
feat(useAsyncEffect): provide an AbortSignal to the effect and abort it on cleanup#385haejunejung wants to merge 1 commit into
haejunejung wants to merge 1 commit into
Conversation
🦋 Changeset detectedLatest commit: 5faa0f6 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds AbortSignal support to useAsyncEffect so async work can be canceled on cleanup (unmount/deps change), and documents + tests the new behavior.
Changes:
- Pass an
AbortSignalinto the async effect and abort it during cleanup (optionally with a reason). - Extend unit tests to validate signal passing/aborting and dependency-change teardown behavior.
- Add a changeset announcing the new capability.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| packages/core/src/hooks/useAsyncEffect/useAsyncEffect.ts | Create/abort an AbortController per run and pass signal into the effect; update JSDoc and signature. |
| packages/core/src/hooks/useAsyncEffect/useAsyncEffect.spec.ts | Add tests covering AbortSignal, abort-on-unmount, abort-on-deps-change, and abort reason. |
| .changeset/useasynceffect-abort-signal.md | Publish minor release note for AbortSignal support and abort reason parameter. |
Comments suppressed due to low confidence (2)
packages/core/src/hooks/useAsyncEffect/useAsyncEffect.ts:1
- The primary example still shows an effect callback with no parameters, but the JSDoc now documents the callback as
(signal: AbortSignal) => .... Even though callers can ignore the parameter, this is confusing for readers. Update the example to acceptsignal(or_signal) to match the documented API, or explicitly mention in the docs that the parameter can be omitted/ignored.
import { DependencyList, useEffect } from 'react';
packages/core/src/hooks/useAsyncEffect/useAsyncEffect.ts:56
- If the async
effectrejects (including common cases likefetchrejecting withAbortErrorafterabort()), this creates an unhandled promise rejection because there is no.catch(...). Consider attaching a rejection handler: at minimum, suppress rejections that occur after the signal was aborted; and for non-abort errors, report them (e.g.,console.error) so they don't silently fail or crash environments configured to treat unhandled rejections as fatal.
effect(abortController.signal).then(result => {
cleanup = result;
if (isCleaned) {
cleanup?.();
}
});
| reason?: unknown | ||
| ) { | ||
| useEffect(() => { | ||
| const abortController = new AbortController(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
solve #220
Enhances
useAsyncEffectto create anAbortControllerfor each effect run and pass its signal to the effect. The signal is aborted on unmount or dependency change, so in-flight async work (such as fetch) can be cancelled without extra boilerplate.Checklist
yarn run fixto format and lint the code and docs?yarn run test:coverageto make sure there is no uncovered line?