Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 35 additions & 17 deletions RNAdMobInterstitial.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,53 @@ import { createErrorFromErrorData } from './utils';

const RNAdMobInterstitial = NativeModules.RNAdMobInterstitial;

const adMobInterstitialEmitter = new NativeEventEmitter(RNAdMobInterstitial);
const eventEmitter = new NativeEventEmitter(RNAdMobInterstitial);

const eventHandlers = {};
const eventMap = {
adLoaded: 'interstitialAdLoaded',
adFailedToLoad: 'interstitialAdFailedToLoad',
adOpened: 'interstitialAdOpened',
adClosed: 'interstitialAdClosed',
adLeftApplication: 'interstitialAdLeftApplication',
};

const _subscriptions = new Map();

const addEventListener = (type, handler) => {
eventHandlers[type] = eventHandlers[type] || new Map();
if (type === 'adFailedToLoad') {
eventHandlers[type].set(handler, adMobInterstitialEmitter.addListener(type, error => handler(createErrorFromErrorData(error))));
const addEventListener = (event, handler) => {
const mappedEvent = eventMap[event];
if (mappedEvent) {
let listener;
if (event === 'adFailedToLoad') {
listener = eventEmitter.addListener(mappedEvent, error => handler(createErrorFromErrorData(error)));
} else {
listener = eventEmitter.addListener(mappedEvent, handler);
}
_subscriptions.set(handler, listener);
return {
remove: () => removeEventListener(event, handler)
};
} else {
eventHandlers[type].set(handler, adMobInterstitialEmitter.addListener(type, handler));
console.warn(`Trying to subscribe to unknown event: "${event}"`);
return {
remove: () => {},
};
}
};

const removeEventListener = (type, handler) => {
if (!eventHandlers[type].has(handler)) {
const listener = _subscriptions.get(handler);
if (!listener) {
return;
}
eventHandlers[type].get(handler).remove();
eventHandlers[type].delete(handler);
listener.remove();
_subscriptions.delete(handler);
};

const removeAllListeners = () => {
const types = Object.keys(eventHandlers);
types.forEach(type => (
eventHandlers[type].forEach((subscription, key, map) => {
subscription.remove();
map.delete(key);
})
));
_subscriptions.forEach((listener, key, map) => {
listener.remove();
map.delete(key);
});
};

export default {
Expand Down
54 changes: 37 additions & 17 deletions RNAdMobRewarded.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,55 @@ import { createErrorFromErrorData } from './utils';

const RNAdMobRewarded = NativeModules.RNAdMobRewarded;

const adMobRewardedEmitter = new NativeEventEmitter(RNAdMobRewarded);
const eventEmitter = new NativeEventEmitter(RNAdMobRewarded);

const eventHandlers = {};
const eventMap = {
adLoaded: 'rewardedVideoAdLoaded',
adFailedToLoad: 'rewardedVideoAdFailedToLoad',
adOpened: 'rewardedVideoAdOpened',
adClosed: 'rewardedVideoAdClosed',
adLeftApplication: 'rewardedVideoAdLeftApplication',
rewarded: 'rewardedVideoAdRewarded',
videoStarted: 'rewardedVideoAdVideoStarted',
};

const _subscriptions = new Map();

const addEventListener = (type, handler) => {
eventHandlers[type] = eventHandlers[type] || new Map();
if (type === 'adFailedToLoad') {
eventHandlers[type].set(handler, adMobRewardedEmitter.addListener(type, error => handler(createErrorFromErrorData(error))));
const addEventListener = (event, handler) => {
const mappedEvent = eventMap[event];
if (mappedEvent) {
let listener;
if (event === 'adFailedToLoad') {
listener = eventEmitter.addListener(mappedEvent, error => handler(createErrorFromErrorData(error)));
} else {
listener = eventEmitter.addListener(mappedEvent, handler);
}
_subscriptions.set(handler, listener);
return {
remove: () => removeEventListener(event, handler)
};
} else {
eventHandlers[type].set(handler, adMobRewardedEmitter.addListener(type, handler));
console.warn(`Trying to subscribe to unknown event: "${event}"`);
return {
remove: () => {},
};
}
};

const removeEventListener = (type, handler) => {
if (!eventHandlers[type].has(handler)) {
const listener = _subscriptions.get(handler);
if (!listener) {
return;
}
eventHandlers[type].get(handler).remove();
eventHandlers[type].delete(handler);
listener.remove();
_subscriptions.delete(handler);
};

const removeAllListeners = () => {
const types = Object.keys(eventHandlers);
types.forEach(type => (
eventHandlers[type].forEach((subscription, key, map) => {
subscription.remove();
map.delete(key);
})
));
_subscriptions.forEach((listener, key, map) => {
listener.remove();
map.delete(key);
});
};

export default {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public class RNAdMobInterstitialAdModule extends ReactContextBaseJavaModule {

public static final String REACT_CLASS = "RNAdMobInterstitial";

public static final String EVENT_AD_LOADED = "onAdLoaded";
public static final String EVENT_AD_FAILED_TO_LOAD = "onAdFailedToLoad";
public static final String EVENT_AD_OPENED = "onAdOpened";
public static final String EVENT_AD_CLOSED = "onAdClosed";
public static final String EVENT_AD_LEFT_APPLICATION = "onAdLeftApplication";
public static final String EVENT_AD_LOADED = "interstitialAdLoaded";
public static final String EVENT_AD_FAILED_TO_LOAD = "interstitialAdFailedToLoad";
public static final String EVENT_AD_OPENED = "interstitialAdOpened";
public static final String EVENT_AD_CLOSED = "interstitialAdClosed";
public static final String EVENT_AD_LEFT_APPLICATION = "interstitialAdLeftApplication";

InterstitialAd mInterstitialAd;
String[] testDevices;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ public class RNAdMobRewardedVideoAdModule extends ReactContextBaseJavaModule imp

public static final String REACT_CLASS = "RNAdMobRewarded";

public static final String EVENT_AD_LOADED = "onAdLoaded";
public static final String EVENT_AD_FAILED_TO_LOAD = "onAdFailedToLoad";
public static final String EVENT_AD_OPENED = "onAdOpened";
public static final String EVENT_AD_CLOSED = "onAdClosed";
public static final String EVENT_AD_LEFT_APPLICATION = "onAdLeftApplication";
public static final String EVENT_REWARDED = "rewarded";
public static final String EVENT_VIDEO_STARTED = "videoStarted";
public static final String EVENT_AD_LOADED = "rewardedVideoAdLoaded";
public static final String EVENT_AD_FAILED_TO_LOAD = "rewardedVideoAdFailedToLoad";
public static final String EVENT_AD_OPENED = "rewardedVideoAdOpened";
public static final String EVENT_AD_CLOSED = "rewardedVideoAdClosed";
public static final String EVENT_AD_LEFT_APPLICATION = "rewardedVideoAdLeftApplication";
public static final String EVENT_REWARDED = "rewardedVideoAdRewarded";
public static final String EVENT_VIDEO_STARTED = "rewardedVideoAdVideoStarted";

RewardedVideoAd mRewardedVideoAd;
String adUnitID;
Expand Down
12 changes: 6 additions & 6 deletions ios/RNAdMobInterstitial.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
#import "RCTUtils.h"
#endif

static NSString *const kEventAdLoaded = @"adLoaded";
static NSString *const kEventAdFailedToLoad = @"adFailedToLoad";
static NSString *const kEventAdOpened = @"adOpened";
static NSString *const kEventAdFailedToOpen = @"adFailedToOpen";
static NSString *const kEventAdClosed = @"adClosed";
static NSString *const kEventAdLeftApplication = @"adLeftApplication";
static NSString *const kEventAdLoaded = @"interstitialAdLoaded";
static NSString *const kEventAdFailedToLoad = @"interstitialAdFailedToLoad";
static NSString *const kEventAdOpened = @"interstitialAdOpened";
static NSString *const kEventAdFailedToOpen = @"interstitialAdFailedToOpen";
static NSString *const kEventAdClosed = @"interstitialAdClosed";
static NSString *const kEventAdLeftApplication = @"interstitialAdLeftApplication";

@implementation RNAdMobInterstitial
{
Expand Down
14 changes: 7 additions & 7 deletions ios/RNAdMobRewarded.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
#import "RCTUtils.h"
#endif

static NSString *const kEventAdLoaded = @"adLoaded";
static NSString *const kEventAdFailedToLoad = @"adFailedToLoad";
static NSString *const kEventAdOpened = @"adOpened";
static NSString *const kEventAdClosed = @"adClosed";
static NSString *const kEventAdLeftApplication = @"adLeftApplication";
static NSString *const kEventRewarded = @"rewarded";
static NSString *const kEventVideoStarted = @"videoStarted";
static NSString *const kEventAdLoaded = @"rewardedVideoAdLoaded";
static NSString *const kEventAdFailedToLoad = @"rewardedVideoAdFailedToLoad";
static NSString *const kEventAdOpened = @"rewardedVideoAdOpened";
static NSString *const kEventAdClosed = @"rewardedVideoAdClosed";
static NSString *const kEventAdLeftApplication = @"rewardedVideoAdLeftApplication";
static NSString *const kEventRewarded = @"rewardedVideoAdRewarded";
static NSString *const kEventVideoStarted = @"rewardedVideoAdVideoStarted";

@implementation RNAdMobRewarded
{
Expand Down