1616 */
1717
1818import { EventSubscription , NativeEventEmitter , Platform } from 'react-native' ;
19- import EventEmitter from 'react-native/Libraries/vendor/emitter/EventEmitter' ;
2019
2120import { NativeAdEventType } from '../../NativeAdEventType' ;
2221import { isFunction , isOneOf , isString } from '../../common' ;
@@ -52,7 +51,8 @@ export class NativeAd {
5251 readonly extras : Record < string , unknown > | null ;
5352
5453 private nativeEventSubscription : EventSubscription ;
55- private eventEmitter : EventEmitter ;
54+ private eventEmitter : NativeEventEmitter ;
55+ private readonly eventEmitterTypes : string [ ] ;
5656
5757 private constructor ( adUnitId : string , props : NativeAdProps ) {
5858 this . adUnitId = adUnitId ;
@@ -68,6 +68,7 @@ export class NativeAd {
6868 this . images = props . images ;
6969 this . mediaContent = props . mediaContent ;
7070 this . extras = props . extras as Record < string , unknown > ;
71+ this . eventEmitterTypes = [ ] ;
7172
7273 if ( 'onAdEvent' in NativeGoogleMobileAdsNativeModule ) {
7374 this . nativeEventSubscription = NativeGoogleMobileAdsNativeModule . onAdEvent (
@@ -84,8 +85,9 @@ export class NativeAd {
8485 'RNGMANativeAdEvent' ,
8586 this . onNativeAdEvent . bind ( this ) ,
8687 ) ;
88+ this . eventEmitterTypes . push ( 'RNGMANativeAdEvent' ) ;
8789 }
88- this . eventEmitter = new EventEmitter ( ) ;
90+ this . eventEmitter = new NativeEventEmitter ( ) ;
8991 }
9092
9193 private onNativeAdEvent ( { responseId, type, ...data } : NativeAdEventPayload ) {
@@ -106,11 +108,16 @@ export class NativeAd {
106108 throw new Error ( `NativeAd.addAdEventListener(_, *) 'listener' expected a function.` ) ;
107109 }
108110
111+ this . eventEmitterTypes . push ( type ) ;
109112 return this . eventEmitter . addListener ( type , listener ) ;
110113 }
111114
112115 removeAllAdEventListeners ( ) {
113- this . eventEmitter . removeAllListeners ( ) ;
116+ this . eventEmitterTypes . forEach ( type => {
117+ this . eventEmitter . removeAllListeners ( type ) ;
118+ } ) ;
119+ // we want the array to be read-only, but also want to clear it.
120+ this . eventEmitterTypes . splice ( 0 , this . eventEmitterTypes . length ) ;
114121 }
115122
116123 destroy ( ) {
0 commit comments