Simplify FeatureFlags ref counting by removing WeakMap#7394
Conversation
|
Co-authored-by: mattcosta7 <8616962+mattcosta7@users.noreply.github.com>
0163141
into
copilot/set-dialog-scroll-attribute
|
👋 Hi, this pull request contains changes to the source code that github/github-ui depends on. If you are GitHub staff, test these changes with github/github-ui using the integration workflow. Or, apply the |
Simplifies the
data-dialog-scroll-optimizedattribute ref counting in FeatureFlags by removing unnecessary WeakMap-based instance tracking. The WeakMap was guarding against double-counting, but React's useEffect cleanup semantics already prevent this.Changes
Removed:
dialogScrollOptimizedInstances)useRefandinstanceRefusageAdded:
__resetDialogScrollOptimizedCount()function for test isolation (not exported from public API)Simplified:
Tests import reset function directly from
FeatureFlags.tsx(bypassing public index) for proper isolation.Changelog
Changed
FeatureFlagsref counting to use counter-only approach instead of WeakMap + counterRollout strategy
Testing & Reviewing
All existing tests pass including multi-instance ref counting scenarios. The
__resetDialogScrollOptimizedCountfunction ensures test isolation without being exposed in the public API.Merge checklist
Original prompt
Summary
Simplify the ref counting implementation in PR #7393 by removing the WeakMap and using a simple counter instead. Also expose a test-only reset function that is not part of the public API.
Background
The current implementation uses both a WeakMap and a counter for ref counting, which is overengineered:
This can be simplified to just a counter since:
Changes Needed
1. Simplify
packages/react/src/FeatureFlags/FeatureFlags.tsxReplace the WeakMap + count approach with a simple counter:
2. Update the test file
packages/react/src/FeatureFlags/__tests__/FeatureFlags.test.tsxUse the reset function in beforeEach instead of manual attribute cleanup:
3. Ensure the reset function is NOT exported from the public index
Check
packages/react/src/FeatureFlags/index.ts(or wherever the public exports are) and ensure__resetDialogScrollOptimizedCountis NOT re-exported. It should only be importable directly from the FeatureFlags.tsx file for tests.The double-underscore prefix (
__) is a common convention indicating internal/private APIs.4. Remove the instanceRef
The
useRef({})is no longer needed since we're not using WeakMap:- const instanceRef = useRef({})Documentation
Add a comment in the code explaining this is temporary:
Testing
This pull request was created from Copilot chat.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.