Skip to content

Commit

Permalink
add namespace to events (#192)
Browse files Browse the repository at this point in the history
since events are not scoped to a module they did clash.
  • Loading branch information
koenpunt committed Oct 3, 2017
1 parent 728f9dd commit fcb1f5a
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 59 deletions.
52 changes: 35 additions & 17 deletions RNAdMobInterstitial.js
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
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
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
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
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
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

0 comments on commit fcb1f5a

Please sign in to comment.