Skip to content

Commit

Permalink
refactor: eliminates require cycles, and some unused code (#39)
Browse files Browse the repository at this point in the history
* Enables "no-undef" ESLint rule, cleans up related issues
* Removes "statics" from GoogleAdsModule config
I am quite sure, it wasn't used anywhere in the code
* Gets rid of require cycles
* The link to CONTRIBUTING in PR template was wrong
  • Loading branch information
birdofpreyru authored Dec 13, 2021
1 parent c53c657 commit 323c400
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 85 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
src/version.js
lib/version.js
**/node_modules/**
node_modules
scripts/
Expand Down
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = {
'class-methods-use-this': 0,
'no-console': 1,
'no-plusplus': 0,
'no-undef': 0,
'no-undef': 'error',
'no-shadow': 0,
'no-catch-shadow': 0,
'no-underscore-dangle': 'off',
Expand Down
3 changes: 2 additions & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

### Checklist

- I read the [Contributor Guide](../CONTRIBUTING.md) and followed the process outlined there for submitting PRs.
- I read the [Contributor Guide](https://github.com/invertase/react-native-google-ads/blob/main/CONTRIBUTING.md)
and followed the process outlined there for submitting PRs.
- [ ] Yes
- My change supports the following platforms;
- [ ] `Android`
Expand Down
1 change: 0 additions & 1 deletion example/e2e/init.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable no-undef */
const detox = require('detox');
const adapter = require('detox/runners/mocha/adapter');

Expand Down
2 changes: 1 addition & 1 deletion lib/ads/InterstitialAd.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

import { isFunction, isString } from '../common';
import googleAds from '../index';
import googleAds from '../googleMobileAds';
import validateAdRequestOptions from '../validateAdRequestOptions';
import validateAdShowOptions from '../validateAdShowOptions';
import MobileAd from './MobileAd';
Expand Down
2 changes: 1 addition & 1 deletion lib/ads/RewardedAd.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

import { isFunction, isString } from '../common';
import googleAds from '../index';
import googleAds from '../googleMobileAds';
import validateAdRequestOptions from '../validateAdRequestOptions';
import validateAdShowOptions from '../validateAdShowOptions';
import MobileAd from './MobileAd';
Expand Down
1 change: 1 addition & 0 deletions lib/common/Base64.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable no-bitwise */
/* global Blob, FileReader */
/*
* Copyright (c) 2016-present Invertase Limited & Contributors
*
Expand Down
12 changes: 0 additions & 12 deletions lib/common/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,6 @@ export function isValidPath(path) {
return typeof path === 'string' && path.length !== 0 && !INVALID_PATH_REGEX.test(path);
}

// eslint-disable-next-line no-control-regex
export const INVALID_KEY_REGEX = /[\[\].#$\/\u0000-\u001F\u007F]/;

/**
* Ensures a given key is a valid Firebase key
* @param key
* @returns {boolean}
*/
export function isValidKey(key) {
return typeof key === 'string' && key.length !== 0 && !INVALID_KEY_REGEX.test(path);
}

/**
* Converts a file path to a standardized string path
* @param path
Expand Down
50 changes: 50 additions & 0 deletions lib/googleMobileAds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Module } from './internal';
import validateAdRequestConfiguration from './validateAdRequestConfiguration';
import version from './version';

const namespace = 'google_ads';

const nativeModuleName = [
'RNGoogleAdsModule',
'RNGoogleAdsInterstitialModule',
'RNGoogleAdsRewardedModule',
];

class GoogleAdsModule extends Module {
constructor(...args) {
super(...args);

this.emitter.addListener('google_ads_interstitial_event', event => {
this.emitter.emit(
`google_ads_interstitial_event:${event.adUnitId}:${event.requestId}`,
event,
);
});

this.emitter.addListener('google_ads_rewarded_event', event => {
this.emitter.emit(`google_ads_rewarded_event:${event.adUnitId}:${event.requestId}`, event);
});
}

setRequestConfiguration(requestConfiguration) {
let config;
try {
config = validateAdRequestConfiguration(requestConfiguration);
} catch (e) {
throw new Error(`googleAds.setRequestConfiguration(*) ${e.message}`);
}

return this.native.setRequestConfiguration(config);
}
}

const googleMobileAds = new GoogleAdsModule('AppName', {
version,
namespace,
nativeModuleName,
nativeEvents: ['google_ads_interstitial_event', 'google_ads_rewarded_event'],
});

export default () => {
return googleMobileAds;
};
68 changes: 1 addition & 67 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,78 +15,12 @@
*
*/

import { Module } from './internal';
import validateAdRequestConfiguration from './validateAdRequestConfiguration';
import version from './version';
import AdEventType from './AdEventType';
import AdsConsentDebugGeography from './AdsConsentDebugGeography';
import AdsConsentStatus from './AdsConsentStatus';
import MaxAdContentRating from './MaxAdContentRating';
import RewardedAdEventType from './RewardedAdEventType';
import BannerAdSize from './BannerAdSize';
import TestIds from './TestIds';

const statics = {
AdsConsentDebugGeography,
AdsConsentStatus,
AdEventType,
RewardedAdEventType,
MaxAdContentRating,
TestIds,
BannerAdSize,
};

const namespace = 'google_ads';

const nativeModuleName = [
'RNGoogleAdsModule',
'RNGoogleAdsInterstitialModule',
'RNGoogleAdsRewardedModule',
];

class GoogleAdsModule extends Module {
constructor(...args) {
super(...args);

this.emitter.addListener('google_ads_interstitial_event', event => {
this.emitter.emit(
`google_ads_interstitial_event:${event.adUnitId}:${event.requestId}`,
event,
);
});

this.emitter.addListener('google_ads_rewarded_event', event => {
this.emitter.emit(`google_ads_rewarded_event:${event.adUnitId}:${event.requestId}`, event);
});
}

setRequestConfiguration(requestConfiguration) {
let config;
try {
config = validateAdRequestConfiguration(requestConfiguration);
} catch (e) {
throw new Error(`googleAds.setRequestConfiguration(*) ${e.message}`);
}

return this.native.setRequestConfiguration(config);
}
}

// import { SDK_VERSION } from '@invertase/react-native-google-ads';
export const SDK_VERSION = version;

const googleMobileAds = new GoogleAdsModule('AppName', {
statics,
version,
namespace,
nativeModuleName,
nativeEvents: ['google_ads_interstitial_event', 'google_ads_rewarded_event'],
});

export default () => {
return googleMobileAds;
};

export { default } from './googleMobileAds';
export { default as AdsConsentDebugGeography } from './AdsConsentDebugGeography';
export { default as AdsConsentStatus } from './AdsConsentStatus';
export { default as MaxAdContentRating } from './MaxAdContentRating';
Expand Down

0 comments on commit 323c400

Please sign in to comment.