Skip to content

Commit b07c551

Browse files
mfazekasclaude
andauthored
fix(location): eliminate NativeEventEmitter warning on Android with TurboModules (#4011)
Refactor locationManager.ts to use Platform.select for both iOS and Android location manager initialization and event emitter creation. This prevents the "new NativeEventEmitter() called without addListener method" warning that occurs on Android when using TurboModules (new architecture). Changes: - Use Platform.select for MapboxLocationManager initialization - Use Platform.select for LocationModuleEventEmitter with TurboModule detection - Use LocationModuleEventEmitter existence to determine event system usage Fixes #4000 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <noreply@anthropic.com>
1 parent 643a67e commit b07c551

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

src/modules/location/locationManager.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,24 @@ import {
1111

1212
import NativeRNMBXLocationModule from '../../specs/NativeRNMBXLocationModule';
1313

14-
const MapboxGL = NativeModules.RNMBXModule;
15-
const MapboxGLLocationManager: typeof NativeRNMBXLocationModule =
16-
Platform.select({
14+
const Mapbox = NativeModules.RNMBXModule;
15+
const MapboxLocationManager: typeof NativeRNMBXLocationModule = Platform.select(
16+
{
1717
ios: NativeModules.RNMBXLocationModule,
1818
android: NativeRNMBXLocationModule,
19-
});
20-
21-
export const LocationModuleEventEmitter = new NativeEventEmitter(
22-
MapboxGLLocationManager as any,
19+
},
2320
);
2421

22+
const IsTurbo: boolean =
23+
typeof MapboxLocationManager.onLocationUpdate === 'function';
24+
25+
export const LocationModuleEventEmitter = Platform.select({
26+
ios: new NativeEventEmitter(MapboxLocationManager as any),
27+
android: !IsTurbo
28+
? new NativeEventEmitter(MapboxLocationManager as any)
29+
: null,
30+
});
31+
2532
/**
2633
* Location sent by locationManager
2734
*/
@@ -109,8 +116,7 @@ export class LocationManager {
109116
// let's silently catch it and simply log out
110117
// instead of throwing an exception
111118
try {
112-
lastKnownLocation =
113-
await MapboxGLLocationManager.getLastKnownLocation();
119+
lastKnownLocation = await MapboxLocationManager.getLastKnownLocation();
114120
} catch (error) {
115121
console.warn('locationManager Error: ', error);
116122
}
@@ -173,19 +179,17 @@ export class LocationManager {
173179
}
174180

175181
if (!this._isListening) {
176-
MapboxGLLocationManager.start(validDisplacement);
182+
MapboxLocationManager.start(validDisplacement);
177183
//Determine if TurboModules (new architecture) are available.
178-
const isTurbo: boolean =
179-
typeof MapboxGLLocationManager.onLocationUpdate === 'function';
180184

181-
if (Platform.OS === 'ios' || !isTurbo) {
185+
if (LocationModuleEventEmitter) {
182186
// Cast to match NativeEventEmitter's strict signature - runtime behavior is correct
183187
this.subscription = LocationModuleEventEmitter.addListener(
184-
MapboxGL.LocationCallbackName.Update,
188+
Mapbox.LocationCallbackName.Update,
185189
this._onUpdate as (...args: readonly Object[]) => unknown,
186190
);
187191
} else {
188-
this.subscription = MapboxGLLocationManager.onLocationUpdate(
192+
this.subscription = MapboxLocationManager.onLocationUpdate(
189193
(location: any) => {
190194
this._onUpdate(location.payload);
191195
},
@@ -197,7 +201,7 @@ export class LocationManager {
197201
}
198202

199203
stop() {
200-
MapboxGLLocationManager.stop();
204+
MapboxLocationManager.stop();
201205

202206
if (this._isListening && this.subscription) {
203207
this.subscription.remove();
@@ -208,11 +212,11 @@ export class LocationManager {
208212

209213
setMinDisplacement(minDisplacement: number) {
210214
this._minDisplacement = minDisplacement;
211-
MapboxGLLocationManager.setMinDisplacement(minDisplacement);
215+
MapboxLocationManager.setMinDisplacement(minDisplacement);
212216
}
213217

214218
setRequestsAlwaysUse(requestsAlwaysUse: boolean) {
215-
MapboxGLLocationManager.setRequestsAlwaysUse(requestsAlwaysUse);
219+
MapboxLocationManager.setRequestsAlwaysUse(requestsAlwaysUse);
216220
this._requestsAlwaysUse = requestsAlwaysUse;
217221
}
218222

@@ -226,7 +230,7 @@ export class LocationManager {
226230
* simulates location updates, experimental [V10, iOS only]
227231
*/
228232
_simulateHeading(changesPerSecond: number, increment: number) {
229-
MapboxGLLocationManager.simulateHeading(changesPerSecond, increment);
233+
MapboxLocationManager.simulateHeading(changesPerSecond, increment);
230234
}
231235

232236
/**
@@ -240,7 +244,7 @@ export class LocationManager {
240244
* @return {void}
241245
*/
242246
setLocationEventThrottle(throttleValue: number) {
243-
MapboxGLLocationManager.setLocationEventThrottle(throttleValue);
247+
MapboxLocationManager.setLocationEventThrottle(throttleValue);
244248
}
245249
}
246250

0 commit comments

Comments
 (0)