@@ -13,17 +13,14 @@ import UUID from './uuid';
1313import { version } from '../package.json' ;
1414import DEFAULT_OPTIONS from './options' ;
1515
16- let asyncStorage ;
16+ let AsyncStorage ;
1717let Platform ;
1818let DeviceInfo ;
1919if ( BUILD_COMPAT_REACT_NATIVE ) {
2020 const reactNative = require ( 'react-native' ) ;
21+ AsyncStorage = require ( '@react-native-community/async-storage' ) . default ;
2122 Platform = reactNative . Platform ;
22- asyncStorage = reactNative . AsyncStorage ;
23- try {
24- DeviceInfo = require ( 'react-native-device-info' ) ;
25- } catch ( e ) {
26- }
23+ DeviceInfo = require ( 'react-native-device-info' ) ;
2724}
2825
2926/**
@@ -98,19 +95,20 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
9895 } ) ;
9996 this . options . domain = this . cookieStorage . options ( ) . domain ;
10097
101- if ( this . _instanceName === Constants . DEFAULT_INSTANCE ) {
102- _upgradeCookieData ( this ) ;
98+ if ( ! BUILD_COMPAT_REACT_NATIVE ) {
99+ if ( this . _instanceName === Constants . DEFAULT_INSTANCE ) {
100+ _upgradeCookieData ( this ) ;
101+ }
103102 }
104103 _loadCookieData ( this ) ;
105104 this . _pendingReadStorage = true ;
106105
107-
108- const initFromStorage = ( ) => {
106+ const initFromStorage = ( deviceId ) => {
109107 // load deviceId and userId from input, or try to fetch existing value from cookie
110108 this . options . deviceId = ( type ( opt_config ) === 'object' && type ( opt_config . deviceId ) === 'string' &&
111109 ! utils . isEmptyString ( opt_config . deviceId ) && opt_config . deviceId ) ||
112110 ( this . options . deviceIdFromUrlParam && this . _getDeviceIdFromUrlParam ( this . _getUrlParams ( ) ) ) ||
113- this . options . deviceId || UUID ( ) + 'R' ;
111+ this . options . deviceId || deviceId || UUID ( ) + 'R' ;
114112 this . options . userId =
115113 ( type ( opt_userId ) === 'string' && ! utils . isEmptyString ( opt_userId ) && opt_userId ) ||
116114 ( type ( opt_userId ) === 'number' && opt_userId . toString ( ) ) ||
@@ -136,9 +134,6 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
136134
137135 // load unsent events and identifies before any attempt to log new ones
138136 if ( this . options . saveEvents ) {
139- this . _unsentEvents = this . _loadSavedUnsentEvents ( this . options . unsentKey ) . concat ( this . _unsentEvents ) ;
140- this . _unsentIdentifys = this . _loadSavedUnsentEvents ( this . options . unsentIdentifyKey ) . concat ( this . _unsentIdentifys ) ;
141-
142137 // validate event properties for unsent events
143138 for ( let i = 0 ; i < this . _unsentEvents . length ; i ++ ) {
144139 var eventProperties = this . _unsentEvents [ i ] . event_properties ;
@@ -170,38 +165,63 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
170165 this . _isInitialized = true ;
171166 } ;
172167
173- if ( asyncStorage ) {
174- asyncStorage . getItem ( this . _storageSuffix ) . then ( ( json ) => {
175- const cookieData = JSON . parse ( json ) ;
176- if ( cookieData ) {
177- _loadCookieDataProps ( this , cookieData ) ;
168+ if ( AsyncStorage ) {
169+ Promise . all ( [
170+ AsyncStorage . getItem ( this . _storageSuffix ) ,
171+ AsyncStorage . getItem ( this . options . unsentKey ) ,
172+ AsyncStorage . getItem ( this . options . unsentIdentifyKey ) ,
173+ ] ) . then ( ( values ) => {
174+ if ( values [ 0 ] ) {
175+ const cookieData = JSON . parse ( values [ 0 ] ) ;
176+ if ( cookieData ) {
177+ _loadCookieDataProps ( this , cookieData ) ;
178+ }
179+ }
180+ if ( this . options . saveEvents ) {
181+ this . _unsentEvents = this . _parseSavedUnsentEventsString ( values [ 1 ] ) . concat ( this . _unsentEvents ) ;
182+ this . _unsentIdentifys = this . _parseSavedUnsentEventsString ( values [ 2 ] ) . concat ( this . _unsentIdentifys ) ;
178183 }
179184 if ( DeviceInfo ) {
180- Promise . all ( [ DeviceInfo . getCarrier ( ) , DeviceInfo . getModel ( ) , DeviceInfo . getManufacturer ( ) ] ) . then ( values => {
185+ Promise . all ( [
186+ DeviceInfo . getCarrier ( ) ,
187+ DeviceInfo . getModel ( ) ,
188+ DeviceInfo . getManufacturer ( ) ,
189+ DeviceInfo . getUniqueId ( ) ,
190+ ] ) . then ( values => {
181191 this . deviceInfo = {
182192 carrier : values [ 0 ] ,
183193 model : values [ 1 ] ,
184194 manufacturer : values [ 2 ]
185195 } ;
186- initFromStorage ( ) ;
196+ initFromStorage ( values [ 3 ] ) ;
187197 this . runQueuedFunctions ( ) ;
198+ if ( type ( opt_callback ) === 'function' ) {
199+ opt_callback ( this ) ;
200+ }
201+ } ) . catch ( ( err ) => {
202+ this . options . onError ( err ) ;
188203 } ) ;
189204 } else {
190205 initFromStorage ( ) ;
191206 this . runQueuedFunctions ( ) ;
192207 }
208+ } ) . catch ( ( err ) => {
209+ this . options . onError ( err ) ;
193210 } ) ;
194211 } else {
212+ if ( this . options . saveEvents ) {
213+ this . _unsentEvents = this . _loadSavedUnsentEvents ( this . options . unsentKey ) . concat ( this . _unsentEvents ) ;
214+ this . _unsentIdentifys = this . _loadSavedUnsentEvents ( this . options . unsentIdentifyKey ) . concat ( this . _unsentIdentifys ) ;
215+ }
195216 initFromStorage ( ) ;
196217 this . runQueuedFunctions ( ) ;
218+ if ( type ( opt_callback ) === 'function' ) {
219+ opt_callback ( this ) ;
220+ }
197221 }
198-
199- } catch ( e ) {
200- utils . log . error ( e ) ;
201- } finally {
202- if ( type ( opt_callback ) === 'function' ) {
203- opt_callback ( this ) ;
204- }
222+ } catch ( err ) {
223+ utils . log . error ( err ) ;
224+ this . options . onError ( err ) ;
205225 }
206226} ;
207227
@@ -554,8 +574,8 @@ var _saveCookieData = function _saveCookieData(scope) {
554574 identifyId : scope . _identifyId ,
555575 sequenceNumber : scope . _sequenceNumber
556576 } ;
557- if ( asyncStorage ) {
558- asyncStorage . setItem ( scope . _storageSuffix , JSON . stringify ( cookieData ) ) ;
577+ if ( AsyncStorage ) {
578+ AsyncStorage . setItem ( scope . _storageSuffix , JSON . stringify ( cookieData ) ) ;
559579 }
560580 scope . cookieStorage . set ( scope . options . cookieName + scope . _storageSuffix , cookieData ) ;
561581} ;
@@ -681,11 +701,19 @@ AmplitudeClient.prototype._saveReferrer = function _saveReferrer(referrer) {
681701 */
682702AmplitudeClient . prototype . saveEvents = function saveEvents ( ) {
683703 try {
684- this . _setInStorage ( localStorage , this . options . unsentKey , JSON . stringify ( this . _unsentEvents ) ) ;
704+ if ( AsyncStorage ) {
705+ AsyncStorage . setItem ( this . options . unsentKey , JSON . stringify ( this . _unsentEvents ) ) ;
706+ } else {
707+ this . _setInStorage ( localStorage , this . options . unsentKey , JSON . stringify ( this . _unsentEvents ) ) ;
708+ }
685709 } catch ( e ) { }
686710
687711 try {
688- this . _setInStorage ( localStorage , this . options . unsentIdentifyKey , JSON . stringify ( this . _unsentIdentifys ) ) ;
712+ if ( AsyncStorage ) {
713+ AsyncStorage . setItem ( this . options . unsentIdentifyKey , JSON . stringify ( this . _unsentIdentifys ) ) ;
714+ } else {
715+ this . _setInStorage ( localStorage , this . options . unsentIdentifyKey , JSON . stringify ( this . _unsentIdentifys ) ) ;
716+ }
689717 } catch ( e ) { }
690718} ;
691719
@@ -999,7 +1027,9 @@ AmplitudeClient.prototype.setVersionName = function setVersionName(versionName)
9991027 * @private
10001028 */
10011029AmplitudeClient . prototype . _logEvent = function _logEvent ( eventType , eventProperties , apiProperties , userProperties , groups , groupProperties , timestamp , callback ) {
1002- _loadCookieData ( this ) ; // reload cookie before each log event to sync event meta-data between windows and tabs
1030+ if ( ! BUILD_COMPAT_REACT_NATIVE ) {
1031+ _loadCookieData ( this ) ; // reload cookie before each log event to sync event meta-data between windows and tabs
1032+ }
10031033 if ( ! eventType ) {
10041034 if ( type ( callback ) === 'function' ) {
10051035 callback ( 0 , 'No request sent' , { reason : 'Missing eventType' } ) ;
0 commit comments