fix: move module-scope Emitter in react.tsx into the Provider - #413
Open
EngineerOnTravel wants to merge 1 commit into
Open
fix: move module-scope Emitter in react.tsx into the Provider#413EngineerOnTravel wants to merge 1 commit into
EngineerOnTravel wants to merge 1 commit into
Conversation
EngineerOnTravel
requested review from
kyle-ssg
and removed request for
a team
August 18, 2026 04:28
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.
Move module-scope Emitter in react.tsx into the Provider
Thanks for submitting a PR! Please check the boxes below:
docs/if required so people know about the feature. (not applicable - this is an internal bugfix with no public API or docs change)Changes
Closes #391
react.tsxinstantiated a singleEmitterat module scope:Every
<FlagsmithProvider>tree in the process shared that one event bus.On long-lived Node hosts (Fluid Compute, serverful SSR, or any app that
mounts more than one
FlagsmithProvider), an internal flag update on oneprovider's
flagsmithinstance would emit on the sharedeventsobject andnotify
useFlags/useFlagsmithLoading/useExperimenthooks belonging tocompletely unrelated provider trees. This was safe today only by luck,
because
useFlagsre-computes a render key from its ownflagsmithinstance and bails out when the value hasn't changed - but it's still
unnecessary cross-request/cross-tree coupling and wasted re-render work.
This PR moves the
Emitterout of module scope and into auseRefowned byFlagsmithProvider, and threads it to the hooks via a new internalEventsContextprovided alongside the existingFlagsmithContext:FlagsmithProvidernow creates oneEmitterper mounted providerinstance (
useRef, lazily initialized) instead of reusing a sharedmodule-level singleton, and provides it via
EventsContext.useFlagsmithLoading,useFlags, anduseExperimentnow read theemitter from
EventsContextinstead of closing over the old module-leveleventsobject, and guard their effects on the emitter being present(mirrors the existing
!flagsmithguards).EventsContextis internal (not exported) - this is purely animplementation detail fix with no public API change.
How did you test this code?
Verified with the repo's own CI commands (
.github/workflows/pull-request.ymlruns
npm i && npm run build && npm test):I also added a new regression test,
test/react-provider-isolation.test.tsx, which renders two independentFlagsmithProvidertrees (two separateflagsmithinstances) and assertsthat firing one instance's internal
_trigger()(the same callflagsmith-coremakes after a real flag update) only re-renders theuseFlagsconsumer under that provider, and never touches the siblingprovider's consumer. I confirmed this test fails against the pre-fix code
(the sibling's
useFlagsconsumer spuriously re-renders because both treesshared the module-scope
eventsemitter) and passes with the fix.