UNPKG

@kirz/react-native-toolkit

Version:

Toolkit to speed up React Native development

57 lines (56 loc) 2.31 kB
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } import AsyncStorage from '@react-native-async-storage/async-storage'; import ReactNativeIdfaAaid from '@sparkfabrik/react-native-idfa-aaid'; import { AppState } from 'react-native'; import { wait } from '../../utils/promise/wait'; import { Plugin } from '../Plugin'; export class IdfaPlugin extends Plugin { constructor(options) { super(); this.options = options; _defineProperty(this, "name", 'IdfaPlugin'); _defineProperty(this, "features", ['IDFA']); _defineProperty(this, "initializationTimeout", null); _defineProperty(this, "_idfa", null); } get idfa() { return this._idfa; } async initialize() { const storedIdfa = await AsyncStorage.getItem('__IdfaPlugin__'); if (storedIdfa) { this._idfa = storedIdfa === 'null' ? null : storedIdfa; return; } let subscription; if (AppState.currentState !== 'active') { await new Promise(resolve => { subscription = AppState.addEventListener('change', nextAppState => { if (nextAppState === 'active') { resolve(); } }); }); } // @ts-ignore if (subscription) { subscription.remove(); } if (this.options.delay) { await wait(this.options.delay ?? 100); } try { const res = await ReactNativeIdfaAaid.getAdvertisingInfo(); if (!res.id) { return; } this._idfa = res.id; } catch { // no-op } await AsyncStorage.setItem('__IdfaPlugin__', this._idfa ? this._idfa : 'null'); } } //# sourceMappingURL=IdfaPlugin.js.map