From e28bc6b82dca2ddc62b20d481c034c2c6653cd6b Mon Sep 17 00:00:00 2001 From: Jay Date: Sat, 27 Nov 2021 01:49:09 +0900 Subject: [PATCH] fix: initialize hook values when loadOnDismissed previously, ad state values were not initialized when ad is loaded via loadOnDismissed = true, so this fixes the issue. --- src/ads/banner/BaseAd.tsx | 3 +-- src/ads/fullscreen/AppOpenAd.ts | 14 +++++++------- src/ads/fullscreen/FullScreenAd.ts | 18 +++++++++++------- src/ads/fullscreen/InterstitialAd.ts | 11 ++--------- src/hooks/useFullScreenAd.ts | 24 +++++++++++++++--------- src/types.ts | 10 ++++------ 6 files changed, 40 insertions(+), 40 deletions(-) diff --git a/src/ads/banner/BaseAd.tsx b/src/ads/banner/BaseAd.tsx index 975b98c..db3d3a4 100644 --- a/src/ads/banner/BaseAd.tsx +++ b/src/ads/banner/BaseAd.tsx @@ -7,8 +7,7 @@ import { ViewStyle, } from 'react-native'; -import { GAMBannerAdProps } from '../..'; -import { BannerAdProps } from '../../types'; +import { BannerAdProps, GAMBannerAdProps } from '../../types'; interface BannerAdState { style: StyleProp; diff --git a/src/ads/fullscreen/AppOpenAd.ts b/src/ads/fullscreen/AppOpenAd.ts index dcaf211..d0da518 100644 --- a/src/ads/fullscreen/AppOpenAd.ts +++ b/src/ads/fullscreen/AppOpenAd.ts @@ -1,7 +1,7 @@ import { - AppOpenAdEvent, AppOpenAdOptions, - HandlerType, + FullScreenAdEvent, + FullScreenAdHandlerType, RequestOptions, } from '../../types'; @@ -15,10 +15,7 @@ const defaultOptions: AppOpenAdOptions = { let _appOpenRequest = 0; -export default class AppOpenAd extends FullScreenAd< - AppOpenAdEvent, - HandlerType -> { +export default class AppOpenAd extends FullScreenAd { private constructor( requestId: number, unitId: string, @@ -97,7 +94,10 @@ export default class AppOpenAd extends FullScreenAd< * @param event Event name * @param handler Event handler */ - static addEventListener(event: AppOpenAdEvent, handler: HandlerType) { + static addEventListener( + event: FullScreenAdEvent, + handler: FullScreenAdHandlerType + ) { this.checkInstance(); return this.sharedInstance!.addEventListener(event, handler); } diff --git a/src/ads/fullscreen/FullScreenAd.ts b/src/ads/fullscreen/FullScreenAd.ts index 50162ab..b44c750 100644 --- a/src/ads/fullscreen/FullScreenAd.ts +++ b/src/ads/fullscreen/FullScreenAd.ts @@ -7,8 +7,12 @@ import { import { AdType, AppOpenAdOptions, + FullScreenAdEvent, + FullScreenAdHandlerType, FullScreenAdOptions, RequestOptions, + RewardedAdEvent, + RewardedAdHandlerType, } from '../../types'; const RNAdMobEvent = NativeModules.RNAdMobEvent; @@ -39,14 +43,14 @@ const defaultOptions: FullScreenAdOptions = { }; export default class FullScreenAd< - E extends string, - H extends (event?: any) => any + E extends RewardedAdEvent = FullScreenAdEvent, + H extends RewardedAdHandlerType = FullScreenAdHandlerType > { - type: AdType; - requestId: number; - unitId: string; - listeners: EmitterSubscription[]; - options: FullScreenAdOptions | AppOpenAdOptions; + readonly type: AdType; + readonly requestId: number; + readonly unitId: string; + readonly options: FullScreenAdOptions | AppOpenAdOptions; + private listeners: EmitterSubscription[]; private nativeModule: FullScreenAdInterface; protected constructor( diff --git a/src/ads/fullscreen/InterstitialAd.ts b/src/ads/fullscreen/InterstitialAd.ts index ce0e2cc..e7899ff 100644 --- a/src/ads/fullscreen/InterstitialAd.ts +++ b/src/ads/fullscreen/InterstitialAd.ts @@ -1,17 +1,10 @@ -import { - FullScreenAdOptions, - HandlerType, - InterstitialAdEvent, -} from '../../types'; +import { FullScreenAdOptions } from '../../types'; import FullScreenAd from './FullScreenAd'; let _interstitialRequest = 0; -export default class InterstitialAd extends FullScreenAd< - InterstitialAdEvent, - HandlerType -> { +export default class InterstitialAd extends FullScreenAd { private constructor( requestId: number, unitId: string, diff --git a/src/hooks/useFullScreenAd.ts b/src/hooks/useFullScreenAd.ts index 8eadbbc..6de9095 100644 --- a/src/hooks/useFullScreenAd.ts +++ b/src/hooks/useFullScreenAd.ts @@ -6,7 +6,12 @@ import { RewardedAd, RewardedInterstitialAd, } from '../ads/fullscreen'; -import { AdHookReturns, RequestOptions, Reward } from '../types'; +import { + AdHookReturns, + FullScreenAdOptions, + RequestOptions, + Reward, +} from '../types'; export default function useFullScreenAd< T extends @@ -41,7 +46,7 @@ export default function useFullScreenAd< (requestOptions?: RequestOptions) => { if (ad) { initialize(); - ad.load(requestOptions); + ad.load(requestOptions).catch(() => {}); } }, [ad] @@ -49,7 +54,7 @@ export default function useFullScreenAd< const show = useCallback(() => { if (ad) { - ad.show(); + ad.show().catch(() => {}); } }, [ad]); @@ -58,28 +63,29 @@ export default function useFullScreenAd< initialize(); return; } - const isRewardedAd = - ad instanceof RewardedAd || ad instanceof RewardedInterstitialAd; const listeners = [ ad.addEventListener('adLoaded', () => { setAdLoaded(true); - setAdPresented(false); }), ad.addEventListener('adFailedToLoad', (error: Error) => setAdLoadError(error) ), ad.addEventListener('adPresented', () => { setAdPresented(true); - setAdDismissed(false); }), ad.addEventListener('adFailedToPresent', (error: Error) => setAdPresentError(error) ), ad.addEventListener('adDismissed', () => { setAdDismissed(true); - setAdLoaded(false); + if ( + ad.type !== 'AppOpen' && + (ad.options as FullScreenAdOptions).loadOnDismissed + ) { + initialize(); + } }), - isRewardedAd + ad.type === 'Rewarded' || ad.type === 'RewardedInterstitial' ? (ad as RewardedAd | RewardedInterstitialAd).addEventListener( 'rewarded', (r: Reward) => setReward(r) diff --git a/src/types.ts b/src/types.ts index a2ebfaf..99b2a2c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -189,15 +189,13 @@ export type FullScreenAdEvent = | 'adLoaded' | 'adFailedToLoad'; -export type InterstitialAdEvent = FullScreenAdEvent; - export type RewardedAdEvent = FullScreenAdEvent | 'rewarded'; -export type AppOpenAdEvent = FullScreenAdEvent; - -export type HandlerType = (() => void) | ((error: Error) => void); +export type FullScreenAdHandlerType = (() => void) | ((error: Error) => void); -export type RewardedAdHandlerType = HandlerType | ((reward: Reward) => void); +export type RewardedAdHandlerType = + | FullScreenAdHandlerType + | ((reward: Reward) => void); export interface Reward { type: string;