From b5327321401a99ae3eeb99375f1669ee2c65f4fd Mon Sep 17 00:00:00 2001 From: Eran Dagan Date: Sat, 20 Oct 2018 13:01:12 +0300 Subject: [PATCH] Fix registration rejections due to invalid handles --- src/native-ads/withNativeAd.tsx | 56 +++++++++++++++++---------------- src/util/areSetsEqual.ts | 7 +++++ 2 files changed, 36 insertions(+), 27 deletions(-) create mode 100644 src/util/areSetsEqual.ts diff --git a/src/native-ads/withNativeAd.tsx b/src/native-ads/withNativeAd.tsx index 87c67460..45645321 100644 --- a/src/native-ads/withNativeAd.tsx +++ b/src/native-ads/withNativeAd.tsx @@ -14,6 +14,7 @@ import { } from './contexts'; import { HasNativeAd, NativeAd } from './nativeAd'; import AdsManager from './NativeAdsManager'; +import { areSetsEqual } from '../util/areSetsEqual'; interface NativeAdViewProps { adsManager: string; @@ -89,35 +90,36 @@ export default ( ); } - public componentDidUpdate( - prevProps: AdWrapperProps, - prevState: AdWrapperState, - ) { + public componentDidUpdate(_: AdWrapperProps, prevState: AdWrapperState) { if ( - this.state.mediaViewNodeHandle !== -1 || - this.state.adIconViewNodeHandle !== -1 || - this.state.clickableChildren.size > 0 + this.state.mediaViewNodeHandle === -1 || + this.state.adIconViewNodeHandle === -1 ) { - const mediaViewNodeHandleChanged = - this.state.mediaViewNodeHandle !== prevState.mediaViewNodeHandle; - const adIconViewNodeHandleChanged = - this.state.adIconViewNodeHandle !== prevState.adIconViewNodeHandle; - const clickableChildrenChanged = [ - ...prevState.clickableChildren, - ].filter(child => !this.state.clickableChildren.has(child)); - - if ( - mediaViewNodeHandleChanged || - adIconViewNodeHandleChanged || - clickableChildrenChanged - ) { - AdsManager.registerViewsForInteractionAsync( - findNodeHandle(this.nativeAdViewRef!)!, - this.state.mediaViewNodeHandle, - this.state.adIconViewNodeHandle, - [...this.state.clickableChildren], - ); - } + // Facebook's SDK requires both MediaView and AdIconView references in order to register + // interactable views. If one of them is missing, we can't proceed with the registration. + return; + } + + const mediaViewNodeHandleChanged = + this.state.mediaViewNodeHandle !== prevState.mediaViewNodeHandle; + const adIconViewNodeHandleChanged = + this.state.adIconViewNodeHandle !== prevState.adIconViewNodeHandle; + const clickableChildrenChanged = areSetsEqual( + prevState.clickableChildren, + this.state.clickableChildren, + ); + + if ( + mediaViewNodeHandleChanged || + adIconViewNodeHandleChanged || + clickableChildrenChanged + ) { + AdsManager.registerViewsForInteractionAsync( + findNodeHandle(this.nativeAdViewRef!)!, + this.state.mediaViewNodeHandle, + this.state.adIconViewNodeHandle, + [...this.state.clickableChildren], + ); } } diff --git a/src/util/areSetsEqual.ts b/src/util/areSetsEqual.ts new file mode 100644 index 00000000..0cf12eae --- /dev/null +++ b/src/util/areSetsEqual.ts @@ -0,0 +1,7 @@ +export const areSetsEqual = (a: Set, b: Set) => { + if (a.size !== b.size) return false; + for (const aItem of a) { + if (!b.has(aItem)) return false; + } + return true; +};