fix(usePermission): ignore queries after effect cleanup - #1720
Open
sridharkalaibala wants to merge 1 commit into
Open
sridharkalaibala wants to merge 1 commit into
sridharkalaibala wants to merge 1 commit into
Conversation
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.
What is the current behavior, and the steps to reproduce the issue?
A pending
usePermissionquery survives effect cleanup. If it resolves after unmount, the hook attaches a change listener whose cleanup has already run. If the permission name changes while queries are outstanding, an older result can replace the current state: resolve the new query as granted, then the old query as denied, and the hook reports denied.What is the expected behavior?
Only the active effect's query may update state or attach a listener. Listeners attached while active must still be removed on cleanup.
How does this PR fix the problem?
Track whether the effect is active, set it inactive on cleanup, and ignore late query results before updating state or subscribing. The existing listener cleanup remains in place. The JSDoc records the lifecycle behavior.
Fixes #1719.
Validation: controlled query promises reproduce both failures on unchanged master (two failures, ten passing permission DOM/SSR/control tests). With the fix, the complete suite passes: 535 tests in 118 files, including SSR. The new cases cover unmount-before-resolution, older-result-after-descriptor-change, and cleanup after a query resolves while mounted.
yarn fmt,yarn lint:fix, finalyarn fmt:check/yarn lint, andyarn buildpass. Only the hook and its new regression file are changed.This does not add an unsupported state, change API availability policy, or handle query rejections; those are separate from the effect lifecycle. The tests use React's hook runner, controlled Permissions API promises and EventTarget-backed status objects; no physical-device permission behavior is claimed. Prepared with AI assistance on Windows, Node 24.19 and immutable pinned Yarn 4.17.1 dependencies. No dependency/lockfile changes.
Checklist