Skip to content

Commit

Permalink
Merge pull request #139 from callstack/fix-invalid-handles
Browse files Browse the repository at this point in the history
Fix Invalid View Registration Handles
  • Loading branch information
Trancever authored Oct 24, 2018
2 parents a2230d7 + 4e56654 commit 563472d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 27 deletions.
56 changes: 29 additions & 27 deletions src/native-ads/withNativeAd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -89,35 +90,36 @@ export default <T extends HasNativeAd>(
);
}

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],
);
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/util/areSetsEqual.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const areSetsEqual = <T>(a: Set<T>, b: Set<T>) => {
if (a.size !== b.size) return false;
for (const aItem of a) {
if (!b.has(aItem)) return false;
}
return true;
};

0 comments on commit 563472d

Please sign in to comment.