all files / firebase/modules/admob/ index.js

53.57% Statements 15/28
12.5% Branches 1/8
57.14% Functions 4/7
53.57% Lines 15/28
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121                                                                                                                                                                                                                   
/**
 * @flow
 * AdMob representation wrapper
 */
import { SharedEventEmitter } from '../../utils/events';
import { getLogger } from '../../utils/log';
import { getNativeModule } from '../../utils/native';
import ModuleBase from '../../utils/ModuleBase';
 
import Interstitial from './Interstitial';
import RewardedVideo from './RewardedVideo';
import AdRequest from './AdRequest';
import VideoOptions from './VideoOptions';
import Banner from './Banner';
import NativeExpress from './NativeExpress';
 
import EventTypes, {
  NativeExpressEventTypes,
  RewardedVideoEventTypes,
} from './EventTypes';
 
import type App from '../core/app';
 
type NativeEvent = {
  adUnit: string,
  payload: Object,
  type: string,
};
 
const NATIVE_EVENTS = ['interstitial_event', 'rewarded_video_event'];
 
export const MODULE_NAME = 'RNFirebaseAdMob';
export const NAMESPACE = 'admob';
 
export default class AdMob extends ModuleBase {
  _appId: ?string;
  _initialized: boolean;
 
  constructor(app: App) {
    super(app, {
      events: NATIVE_EVENTS,
      moduleName: MODULE_NAME,
      multiApp: false,
      namespace: NAMESPACE,
    });
 
    this._initialized = false;
    this._appId = null;
 
    SharedEventEmitter.addListener(
      'interstitial_event',
      this._onInterstitialEvent.bind(this)
    );
    SharedEventEmitter.addListener(
      'rewarded_video_event',
      this._onRewardedVideoEvent.bind(this)
    );
  }
 
  _onInterstitialEvent(event: NativeEvent): void {
    const { adUnit } = event;
    const jsEventType = `interstitial_${adUnit}`;
 
    if (SharedEventEmitter.listeners(jsEventType).length === 0) {
      // TODO
    }
 
    SharedEventEmitter.emit(jsEventType, event);
  }
 
  _onRewardedVideoEvent(event: NativeEvent): void {
    const { adUnit } = event;
    const jsEventType = `rewarded_video_${adUnit}`;
 
    if (SharedEventEmitter.listeners(jsEventType).length === 0) {
      // TODO
    }
 
    SharedEventEmitter.emit(jsEventType, event);
  }
 
  initialize(appId: string): void {
    Iif (this._initialized) {
      getLogger(this).warn('AdMob has already been initialized!');
    } else {
      this._initialized = true;
      this._appId = appId;
      getNativeModule(this).initialize(appId);
    }
  }
 
  openDebugMenu(): void {
    if (!this._initialized) {
      getLogger(this).warn(
        'AdMob needs to be initialized before opening the dev menu!'
      );
    } else {
      getLogger(this).info('Opening debug menu');
      getNativeModule(this).openDebugMenu(this._appId);
    }
  }
 
  interstitial(adUnit: string): Interstitial {
    return new Interstitial(this, adUnit);
  }
 
  rewarded(adUnit: string): RewardedVideo {
    return new RewardedVideo(this, adUnit);
  }
}
 
export const statics = {
  Banner,
  NativeExpress,
  AdRequest,
  VideoOptions,
  EventTypes,
  RewardedVideoEventTypes,
  NativeExpressEventTypes,
};