Skip to content

fix(usePermission): ignore queries after effect cleanup - #1720

Open
sridharkalaibala wants to merge 1 commit into
react-hookz:masterfrom
sridharkalaibala:pr/fix-1719
Open

sridharkalaibala wants to merge 1 commit into
react-hookz:masterfrom
sridharkalaibala:pr/fix-1719

Conversation

@sridharkalaibala

Copy link
Copy Markdown

What is the current behavior, and the steps to reproduce the issue?

A pending usePermission query 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, final yarn fmt:check / yarn lint, and yarn build pass. 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

  • Have you read the contribution guideline?
  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Is there an existing issue for this PR? usePermission subscribes after unmount and accepts stale query results #1719
  • Have the files been linted and formatted?
  • Have the docs been updated to match the changes in the PR?
  • Have the tests been updated to match the changes in the PR?
  • Have you run the tests locally to confirm they pass?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

usePermission subscribes after unmount and accepts stale query results

1 participant