Skip to content

Commit ac8fa25

Browse files
brainbicyclemeta-codesync[bot]
authored andcommitted
fix: reset listenerCount when invalidating RCTEventEmitter (#54809)
Summary: If a subclass of RCTEventEmitter outlives the bridge and the bridge is invalidated, the invalidate method is called on RCTEventEmitter which calls stopObserving but does not change the listenerCount. Once the bridge is recreated and a listener added to the RCTEventEmitter if the listenerCount is > 1 startListening is never called: https://github.com/facebook/react-native/blob/a9a02689533f9cf30fc20cfc7be1ade4f4a624da/packages/react-native/React/Modules/RCTEventEmitter.m#L109 This just resets the listenerCount to prevent this. Probably not a good idea to have modules that outlive bridge in first place but this seems like unwanted behavior in any case :). ## Changelog: [iOS] [Fixed] - fix listenerCount not resetting for RCTEventEmitter Pull Request resolved: #54809 Test Plan: I tested this code via a patch in the artsy/eigen app repo: artsy/eigen#13068 The app was using a native module that posts events over the bridge to handle push notifications. Behavior before: 1. Start app listeners added to ARNotificationManager module, startListener called listenerCount = 2 2. Force a dev reload invalidating the bridge 3. Invalidate called on bridge and the native module causing stopObserving to be called but listenerCount remains 2 4. Bridge refreshes listeners added again but startObserving never called Behavior after: 1. Start app listeners added to ARNotificationManager module, startListener called listenerCount = 2 2. Force a dev reload invalidating the bridge 3. Invalidate called on bridge and the native module causing stopObserving to be called but listenerCount reset to 0 4. Bridge refreshes listeners added again listeners added again, startObserving called after first Reviewed By: cipolleschi Differential Revision: D88647586 Pulled By: javache fbshipit-source-id: 8a8b8b8cf4c7e683f02a08e40fc9d95c8ba4e4ac
1 parent 37bf285 commit ac8fa25

1 file changed

Lines changed: 1 addition & 0 deletions

File tree

packages/react-native/React/Modules/RCTEventEmitter.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ - (void)invalidate
8989

9090
if (_listenerCount > 0) {
9191
[self stopObserving];
92+
_listenerCount = 0;
9293
}
9394
}
9495

0 commit comments

Comments
 (0)