Skip to content

Commit 7323379

Browse files
fix: prevent multiple listeners for a native event (#130)
1 parent d8a0cda commit 7323379

File tree

5 files changed

+46
-43
lines changed

5 files changed

+46
-43
lines changed

SampleApp/ios/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ PODS:
382382
- React-jsinspector (0.72.6)
383383
- React-logger (0.72.6):
384384
- glog
385-
- react-native-navigation-sdk (0.1.5-beta):
385+
- react-native-navigation-sdk (0.2.2-beta):
386386
- GoogleNavigation (= 5.4.0)
387387
- React-Core
388388
- React-NativeModulesApple (0.72.6):
@@ -719,7 +719,7 @@ SPEC CHECKSUMS:
719719
React-jsiexecutor: 3bf18ff7cb03cd8dfdce08fbbc0d15058c1d71ae
720720
React-jsinspector: 194e32c6aab382d88713ad3dd0025c5f5c4ee072
721721
React-logger: cebf22b6cf43434e471dc561e5911b40ac01d289
722-
react-native-navigation-sdk: a68e122d1301f130b42260924df0589cdd312e34
722+
react-native-navigation-sdk: d31a1e8b7ab7bad0a83fb2c5cfd75ac93db3feed
723723
React-NativeModulesApple: 02e35e9a51e10c6422f04f5e4076a7c02243fff2
724724
React-perflogger: e3596db7e753f51766bceadc061936ef1472edc3
725725
React-RCTActionSheet: 17ab132c748b4471012abbcdcf5befe860660485

SampleApp/package-lock.json

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SampleApp/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"react": "18.2.0",
1515
"react-native": "^0.72.4",
1616
"react-native-eject": "^0.2.0",
17-
"react-native-navigation-sdk": "github:googlemaps/react-native-navigation-sdk",
17+
"react-native-navigation-sdk": "github:googlemaps/react-native-navigation-sdk#0.2.2-beta",
1818
"react-native-permissions": "^3.10.1",
1919
"react-native-select-dropdown": "^3.4.0",
2020
"react-native-snackbar": "^2.6.2"

components/navigation/navigationView/index.tsx

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {ArrivalEvent, NavigationViewProps} from './types';
4141
export default class NavigationView extends React.Component<NavigationViewProps> {
4242
private viewId: number = -1;
4343
private mapViewRef?: any;
44+
private nativeEventsToCallbackMap;
4445

4546
constructor(_props: NavigationViewProps) {
4647
super(_props);
@@ -62,6 +63,28 @@ export default class NavigationView extends React.Component<NavigationViewProps>
6263
orientation: isPortrait() ? 'portrait' : 'landscape',
6364
});
6465
});
66+
67+
this.nativeEventsToCallbackMap = {
68+
'onRouteChanged': this.onRouteChanged,
69+
'onRemainingTimeOrDistanceChanged': this.onRemainingTimeOrDistanceChanged,
70+
'onTrafficUpdated': this.onTrafficUpdated,
71+
'onArrival': this.onArrival,
72+
'onNavigationReady': this.onNavigationReady,
73+
'onStartGuidance': this.onStartGuidance,
74+
'onRecenterButtonClick': this.onRecenterButtonClick,
75+
'onRouteStatusResult': this.onRouteStatusResult,
76+
'onMapReady': this.onMapReady,
77+
'onReroutingRequestedByOffRoute': this.onReroutingRequestedByOffRoute,
78+
'onLocationChanged': this.onLocationChanged,
79+
'onNavigationInitError': this.onNavigationInitError,
80+
'onMarkerInfoWindowTapped': this.onMarkerInfoWindowTapped,
81+
'onMarkerClick': this.onMarkerClick,
82+
'onPolylineClick': this.onPolylineClick,
83+
'onPolygonClick': this.onPolygonClick,
84+
'onCircleClick': this.onCircleClick,
85+
'logDebugInfo': this.logDebugInfo
86+
};
87+
6588
}
6689

6790
/**
@@ -322,45 +345,24 @@ export default class NavigationView extends React.Component<NavigationViewProps>
322345
}
323346
};
324347

325-
private registerNavModuleListener = () => {
326-
const NavModuleEvt = new NativeEventEmitter(
348+
private unregisterNavModuleListeners = () => {
349+
const eventEmitter = new NativeEventEmitter(
327350
NativeModules.CustomEventDispatcher,
328351
);
329352

330-
NavModuleEvt.addListener('onRouteChanged', this.onRouteChanged);
331-
NavModuleEvt.addListener(
332-
'onRemainingTimeOrDistanceChanged',
333-
this.onRemainingTimeOrDistanceChanged,
334-
);
335-
NavModuleEvt.addListener('onTrafficUpdated', this.onTrafficUpdated);
336-
NavModuleEvt.addListener('onArrival', this.onArrival);
337-
NavModuleEvt.addListener('onNavigationReady', this.onNavigationReady);
338-
NavModuleEvt.addListener('onStartGuidance', this.onStartGuidance);
339-
NavModuleEvt.addListener(
340-
'onRecenterButtonClick',
341-
this.onRecenterButtonClick,
342-
);
343-
NavModuleEvt.addListener('onRouteStatusResult', this.onRouteStatusResult);
344-
NavModuleEvt.addListener('onMapReady', this.onMapReady);
345-
NavModuleEvt.addListener(
346-
'onReroutingRequestedByOffRoute',
347-
this.onReroutingRequestedByOffRoute,
348-
);
349-
NavModuleEvt.addListener('onLocationChanged', this.onLocationChanged);
350-
NavModuleEvt.addListener(
351-
'onNavigationInitError',
352-
this.onNavigationInitError,
353-
);
353+
for (const eventName of Object.keys(this.nativeEventsToCallbackMap)) {
354+
eventEmitter.removeAllListeners(eventName);
355+
}
356+
}
354357

355-
NavModuleEvt.addListener(
356-
'onMarkerInfoWindowTapped',
357-
this.onMarkerInfoWindowTapped,
358+
private registerNavModuleListener = () => {
359+
const eventEmitter = new NativeEventEmitter(
360+
NativeModules.CustomEventDispatcher,
358361
);
359-
NavModuleEvt.addListener('onMarkerClick', this.onMarkerClick);
360-
NavModuleEvt.addListener('onPolylineClick', this.onPolylineClick);
361-
NavModuleEvt.addListener('onPolygonClick', this.onPolygonClick);
362-
NavModuleEvt.addListener('onCircleClick', this.onCircleClick);
363-
NavModuleEvt.addListener('logDebugInfo', this.logDebugInfo);
362+
363+
for (const eventName of Object.keys(this.nativeEventsToCallbackMap)) {
364+
eventEmitter.addListener(eventName, this.nativeEventsToCallbackMap[eventName]);
365+
}
364366
};
365367

366368
/**
@@ -375,6 +377,7 @@ export default class NavigationView extends React.Component<NavigationViewProps>
375377
this.viewId = findNodeHandle(this.mapViewRef) || 0;
376378

377379
if (Platform.OS == 'ios') {
380+
this.unregisterNavModuleListeners();
378381
this.registerNavModuleListener();
379382
}
380383

@@ -404,6 +407,7 @@ export default class NavigationView extends React.Component<NavigationViewProps>
404407
}
405408

406409
override componentWillUnmount() {
410+
this.unregisterNavModuleListeners();
407411
this.deleteFragment();
408412
}
409413

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)