Skip to content

Commit d8cdc87

Browse files
committed
fix: replace codegen event emitter with legacy ee
1 parent 6dc1dbb commit d8cdc87

File tree

9 files changed

+37
-49
lines changed

9 files changed

+37
-49
lines changed

android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsNativeModule.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.facebook.react.bridge.Promise
2222
import com.facebook.react.bridge.ReactApplicationContext
2323
import com.facebook.react.bridge.ReactMethod
2424
import com.facebook.react.bridge.ReadableMap
25+
import com.facebook.react.bridge.WritableMap
2526
import com.facebook.react.module.annotations.ReactModule
2627
import com.google.android.gms.ads.AdListener
2728
import com.google.android.gms.ads.AdLoader
@@ -30,6 +31,8 @@ import com.google.android.gms.ads.VideoController.VideoLifecycleCallbacks
3031
import com.google.android.gms.ads.VideoOptions
3132
import com.google.android.gms.ads.nativead.NativeAd
3233
import com.google.android.gms.ads.nativead.NativeAdOptions
34+
import io.invertase.googlemobileads.common.ReactNativeEventEmitter
35+
import io.invertase.googlemobileads.interfaces.NativeEvent
3336

3437
@ReactModule(name = ReactNativeGoogleMobileAdsNativeModule.NAME)
3538
class ReactNativeGoogleMobileAdsNativeModule(
@@ -201,7 +204,12 @@ class ReactNativeGoogleMobileAdsNativeModule(
201204
val payload = Arguments.createMap()
202205
payload.putString("responseId", nativeAd.responseInfo?.responseId)
203206
payload.putString("type", type)
204-
this@ReactNativeGoogleMobileAdsNativeModule.emitOnAdEvent(payload)
207+
208+
val emitter = ReactNativeEventEmitter.getSharedInstance()
209+
emitter.sendEvent(object : NativeEvent {
210+
override fun getEventName() = "RNGMANativeAdEvent"
211+
override fun getEventBody() = payload
212+
})
205213
}
206214
}
207215

android/src/oldarch/io/invertase/googlemobileads/NativeGoogleMobileAdsNativeModuleSpec.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,8 @@ import com.facebook.react.bridge.Promise
44
import com.facebook.react.bridge.ReactApplicationContext
55
import com.facebook.react.bridge.ReactContextBaseJavaModule
66
import com.facebook.react.bridge.ReadableMap
7-
import com.facebook.react.modules.core.DeviceEventManagerModule
87

98
abstract class NativeGoogleMobileAdsNativeModuleSpec(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
109
abstract fun load(adUnitId: String, requestOptions: ReadableMap, promise: Promise)
1110
abstract fun destroy(responseId: String)
12-
13-
fun emitOnAdEvent(params: ReadableMap) {
14-
reactApplicationContext
15-
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
16-
.emit("RNGMANativeAdEvent", params)
17-
}
1811
}

ios/RNGoogleMobileAds/RNGoogleMobileAdsFullScreenAd.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,4 @@ - (void)showWithRequestId:(int)requestId
217217

218218
@end
219219

220-
#endif
220+
#endif

ios/RNGoogleMobileAds/RNGoogleMobileAdsNativeModule.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@
2525
: NativeGoogleMobileAdsNativeModuleSpecBase <NativeGoogleMobileAdsNativeModuleSpec>
2626
#else
2727
#import <React/RCTBridgeModule.h>
28-
#import <React/RCTEventEmitter.h>
2928

30-
@interface RNGoogleMobileAdsNativeModule : RCTEventEmitter <RCTBridgeModule>
29+
@interface RNGoogleMobileAdsNativeModule : NSObject <RCTBridgeModule>
3130
#endif
3231

3332
- (GADNativeAd *)nativeAdForResponseId:(NSString *)responseId;

ios/RNGoogleMobileAds/RNGoogleMobileAdsNativeModule.mm

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#import "RNGoogleMobileAdsNativeModule.h"
2121
#import "RNGoogleMobileAdsCommon.h"
22+
#import "common/RNRCTEventEmitter.h"
2223

2324
typedef void (^RNGMANativeAdLoadCompletionHandler)(GADNativeAd *_Nullable nativeAd,
2425
NSError *_Nullable error);
@@ -53,10 +54,6 @@ - (dispatch_queue_t)methodQueue {
5354
(const facebook::react::ObjCTurboModule::InitParams &)params {
5455
return std::make_shared<facebook::react::NativeGoogleMobileAdsNativeModuleSpecJSI>(params);
5556
}
56-
#else
57-
- (NSArray<NSString *> *)supportedEvents {
58-
return @[ @"RNGMANativeAdEvent" ];
59-
}
6057
#endif
6158

6259
- (instancetype)init {
@@ -278,11 +275,7 @@ - (void)emitAdEvent:(NSString *)type {
278275
}
279276
NSDictionary *payload =
280277
@{@"responseId" : _nativeAd.responseInfo.responseIdentifier, @"type" : type};
281-
#ifdef RCT_NEW_ARCH_ENABLED
282-
[_nativeModule emitOnAdEvent:payload];
283-
#else
284-
[_nativeModule sendEventWithName:@"RNGMANativeAdEvent" body:payload];
285-
#endif
278+
[[RNRCTEventEmitter shared] sendEventWithName:@"RNGMANativeAdEvent" body:payload];
286279
}
287280

288281
@end

src/MobileAds.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ class MobileAdsModule implements MobileAdsModuleInterface {
2525

2626
subscribeToNativeModuleEvent(eventName: string) {
2727
if (!NATIVE_MODULE_EVENT_SUBSCRIPTIONS[eventName]) {
28-
GoogleMobileAdsNativeEventEmitter.addListener(eventName, event => {
29-
SharedEventEmitter.emit(`${eventName}:${event.adUnitId}:${event.requestId}`, event);
30-
});
28+
GoogleMobileAdsNativeEventEmitter.addListener<{ adUnitId: string; requestId: number }>(
29+
eventName,
30+
event => {
31+
SharedEventEmitter.emit(`${eventName}:${event.adUnitId}:${event.requestId}`, event);
32+
},
33+
);
3134

3235
NATIVE_MODULE_EVENT_SUBSCRIPTIONS[eventName] = true;
3336
}

src/ads/native-ad/NativeAd.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
*/
1717

18-
import { EventSubscription, NativeEventEmitter, Platform } from 'react-native';
18+
import { EventSubscription } from 'react-native';
1919
import EventEmitter from 'react-native/Libraries/vendor/emitter/EventEmitter';
2020

2121
import { NativeAdEventType } from '../../NativeAdEventType';
@@ -28,6 +28,7 @@ import NativeGoogleMobileAdsNativeModule, {
2828
} from '../../specs/modules/NativeGoogleMobileAdsNativeModule';
2929
import { NativeAdRequestOptions } from '../../types';
3030
import { validateAdRequestOptions } from '../../validateAdRequestOptions';
31+
import { GoogleMobileAdsNativeEventEmitter } from '../../internal/GoogleMobileAdsNativeEventEmitter';
3132

3233
/**
3334
* A class for loading Native Ads.
@@ -65,22 +66,17 @@ export class NativeAd {
6566
this.mediaContent = props.mediaContent;
6667
this.extras = props.extras as Record<string, unknown>;
6768

68-
if ('onAdEvent' in NativeGoogleMobileAdsNativeModule) {
69-
this.nativeEventSubscription = NativeGoogleMobileAdsNativeModule.onAdEvent(
70-
this.onNativeAdEvent.bind(this),
71-
);
72-
} else {
73-
let eventEmitter;
74-
if (Platform.OS === 'ios') {
75-
eventEmitter = new NativeEventEmitter(NativeGoogleMobileAdsNativeModule);
76-
} else {
77-
eventEmitter = new NativeEventEmitter();
78-
}
79-
this.nativeEventSubscription = eventEmitter.addListener(
80-
'RNGMANativeAdEvent',
81-
this.onNativeAdEvent.bind(this),
82-
);
83-
}
69+
// Codegen EventEmitter was introduced in RN 0.76.2, so we can't apply it for now, due to backward compatibility.
70+
// if ('onAdEvent' in NativeGoogleMobileAdsNativeModule) {
71+
// this.nativeEventSubscription = NativeGoogleMobileAdsNativeModule.onAdEvent(
72+
// this.onNativeAdEvent.bind(this),
73+
// );
74+
// }
75+
76+
this.nativeEventSubscription = GoogleMobileAdsNativeEventEmitter.addListener(
77+
'RNGMANativeAdEvent',
78+
this.onNativeAdEvent.bind(this),
79+
);
8480
this.eventEmitter = new EventEmitter();
8581
}
8682

src/internal/GoogleMobileAdsNativeEventEmitter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ class GANativeEventEmitter extends NativeEventEmitter {
2727
this.ready = false;
2828
}
2929

30-
addListener(
30+
addListener<T>(
3131
eventType: string,
32-
listener: (event: { adUnitId: string; requestId: number }) => void,
32+
listener: (event: T) => void,
3333
context?: Record<string, unknown>,
3434
) {
3535
if (!this.ready) {

src/specs/modules/NativeGoogleMobileAdsNativeModule.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@
1717

1818
import type { TurboModule } from 'react-native';
1919
import { TurboModuleRegistry } from 'react-native';
20-
import type {
21-
Double,
22-
Float,
23-
UnsafeObject,
24-
EventEmitter,
25-
} from 'react-native/Libraries/Types/CodegenTypes';
20+
import type { Double, Float, UnsafeObject } from 'react-native/Libraries/Types/CodegenTypes';
2621

2722
export type NativeAdProps = {
2823
responseId: string;
@@ -58,7 +53,8 @@ export type NativeAdEventPayload = {
5853
export interface Spec extends TurboModule {
5954
load(adUnitId: string, requestOptions: UnsafeObject): Promise<NativeAdProps>;
6055
destroy(responseId: string): void;
61-
readonly onAdEvent: EventEmitter<NativeAdEventPayload>;
56+
// Codegen EventEmitter was introduced in RN 0.76.2, so we can't apply it for now, due to backward compatibility.
57+
// readonly onAdEvent: EventEmitter<NativeAdEventPayload>;
6258
}
6359

6460
export default TurboModuleRegistry.getEnforcing<Spec>('RNGoogleMobileAdsNativeModule');

0 commit comments

Comments
 (0)