UNPKG

react-native-google-mobile-ads

Version:

React Native Google Mobile Ads is an easy way to monetize mobile apps with targeted, in-app advertising.

122 lines (119 loc) 4.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NativeAd = void 0; var _reactNative = require("react-native"); var _EventEmitter = _interopRequireDefault(require("react-native/Libraries/vendor/emitter/EventEmitter")); var _NativeAdEventType = require("../../NativeAdEventType"); var _common = require("../../common"); var _NativeGoogleMobileAdsNativeModule = _interopRequireDefault(require("../../specs/modules/NativeGoogleMobileAdsNativeModule")); var _validateAdRequestOptions = require("../../validateAdRequestOptions"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } /* * Copyright (c) 2016-present Invertase Limited & Contributors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this library except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ /** * A class for loading Native Ads. */ class NativeAd { constructor(adUnitId, props) { this.adUnitId = adUnitId; this.responseId = props.responseId; this.advertiser = props.advertiser; this.body = props.body; this.callToAction = props.callToAction; this.headline = props.headline; this.price = props.price; this.store = props.store; this.starRating = props.starRating; this.icon = props.icon; this.images = props.images; this.mediaContent = props.mediaContent; this.extras = props.extras; if ('onAdEvent' in _NativeGoogleMobileAdsNativeModule.default) { this.nativeEventSubscription = _NativeGoogleMobileAdsNativeModule.default.onAdEvent(this.onNativeAdEvent.bind(this)); } else { let eventEmitter; if (_reactNative.Platform.OS === 'ios') { eventEmitter = new _reactNative.NativeEventEmitter(_NativeGoogleMobileAdsNativeModule.default); } else { eventEmitter = new _reactNative.NativeEventEmitter(); } this.nativeEventSubscription = eventEmitter.addListener('RNGMANativeAdEvent', this.onNativeAdEvent.bind(this)); } this.eventEmitter = new _EventEmitter.default(); } onNativeAdEvent({ responseId, type }) { if (this.responseId !== responseId) { return; } this.eventEmitter.emit(type); } addAdEventListener(type, listener) { if (!(0, _common.isOneOf)(type, Object.values(_NativeAdEventType.NativeAdEventType))) { throw new Error(`NativeAd.addAdEventListener(*) 'type' expected a valid event type value.`); } if (!(0, _common.isFunction)(listener)) { throw new Error(`NativeAd.addAdEventListener(_, *) 'listener' expected a function.`); } return this.eventEmitter.addListener(type, listener); } removeAllAdEventListeners() { this.eventEmitter.removeAllListeners(); } destroy() { _NativeGoogleMobileAdsNativeModule.default.destroy(this.responseId); this.nativeEventSubscription.remove(); this.removeAllAdEventListeners(); } /** * Creates a new NativeAd instance. * * #### Example * * ```js * import { NativeAd, AdEventType, TestIds } from 'react-native-google-mobile-ads'; * * const nativeAd = await NativeAd.createForAdRequest(TestIds.NATIVE, { * requestAgent: 'CoolAds', * }); * ``` * * @param adUnitId The Ad Unit ID for the Native Ad. You can find this on your Google Mobile Ads dashboard. * @param requestOptions Optional RequestOptions used to load the ad. */ static async createForAdRequest(adUnitId, requestOptions) { if (!(0, _common.isString)(adUnitId)) { throw new Error("NativeAd.createForAdRequest(*) 'adUnitId' expected an string value."); } let options = {}; try { options = (0, _validateAdRequestOptions.validateAdRequestOptions)(requestOptions); } catch (e) { if (e instanceof Error) { throw new Error(`NativeAd.createForAdRequest(_, *) ${e.message}.`); } } const props = await _NativeGoogleMobileAdsNativeModule.default.load(adUnitId, options); return new NativeAd(adUnitId, props); } } exports.NativeAd = NativeAd; //# sourceMappingURL=NativeAd.js.map