-
Notifications
You must be signed in to change notification settings - Fork 132
Append this._storageSuffix (which includes API key) to unsentKey #204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7fb0749
e99863f
86479c4
fe025f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -166,47 +166,49 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o | |
| }; | ||
|
|
||
| if (AsyncStorage) { | ||
| Promise.all([ | ||
| AsyncStorage.getItem(this._storageSuffix), | ||
| AsyncStorage.getItem(this.options.unsentKey), | ||
| AsyncStorage.getItem(this.options.unsentIdentifyKey), | ||
| ]).then((values) => { | ||
| if (values[0]) { | ||
| const cookieData = JSON.parse(values[0]); | ||
| if (cookieData) { | ||
| _loadCookieDataProps(this, cookieData); | ||
| this._migrateUnsentEvents(() => { | ||
| Promise.all([ | ||
| AsyncStorage.getItem(this._storageSuffix), | ||
| AsyncStorage.getItem(this.options.unsentKey + this._storageSuffix), | ||
| AsyncStorage.getItem(this.options.unsentIdentifyKey + this._storageSuffix), | ||
| ]).then((values) => { | ||
| if (values[0]) { | ||
| const cookieData = JSON.parse(values[0]); | ||
| if (cookieData) { | ||
| _loadCookieDataProps(this, cookieData); | ||
| } | ||
| } | ||
| } | ||
| if (this.options.saveEvents) { | ||
| this._unsentEvents = this._parseSavedUnsentEventsString(values[1]).concat(this._unsentEvents); | ||
| this._unsentIdentifys = this._parseSavedUnsentEventsString(values[2]).concat(this._unsentIdentifys); | ||
| } | ||
| if (DeviceInfo) { | ||
| Promise.all([ | ||
| DeviceInfo.getCarrier(), | ||
| DeviceInfo.getModel(), | ||
| DeviceInfo.getManufacturer(), | ||
| DeviceInfo.getUniqueId(), | ||
| ]).then(values => { | ||
| this.deviceInfo = { | ||
| carrier: values[0], | ||
| model: values[1], | ||
| manufacturer: values[2] | ||
| }; | ||
| initFromStorage(values[3]); | ||
| if (this.options.saveEvents) { | ||
| this._unsentEvents = this._parseSavedUnsentEventsString(values[1]).concat(this._unsentEvents); | ||
| this._unsentIdentifys = this._parseSavedUnsentEventsString(values[2]).concat(this._unsentIdentifys); | ||
| } | ||
| if (DeviceInfo) { | ||
| Promise.all([ | ||
| DeviceInfo.getCarrier(), | ||
| DeviceInfo.getModel(), | ||
| DeviceInfo.getManufacturer(), | ||
| DeviceInfo.getUniqueId(), | ||
| ]).then(values => { | ||
| this.deviceInfo = { | ||
| carrier: values[0], | ||
| model: values[1], | ||
| manufacturer: values[2] | ||
| }; | ||
| initFromStorage(values[3]); | ||
| this.runQueuedFunctions(); | ||
| if (type(opt_callback) === 'function') { | ||
| opt_callback(this); | ||
| } | ||
| }).catch((err) => { | ||
| this.options.onError(err); | ||
| }); | ||
| } else { | ||
| initFromStorage(); | ||
| this.runQueuedFunctions(); | ||
| if (type(opt_callback) === 'function') { | ||
| opt_callback(this); | ||
| } | ||
| }).catch((err) => { | ||
| this.options.onError(err); | ||
| }); | ||
| } else { | ||
| initFromStorage(); | ||
| this.runQueuedFunctions(); | ||
| } | ||
| }).catch((err) => { | ||
| this.options.onError(err); | ||
| } | ||
| }).catch((err) => { | ||
| this.options.onError(err); | ||
| }); | ||
| }); | ||
| } else { | ||
| if (this.options.saveEvents) { | ||
|
|
@@ -225,6 +227,34 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o | |
| } | ||
| }; | ||
|
|
||
| /** | ||
| * @private | ||
| */ | ||
| AmplitudeClient.prototype._migrateUnsentEvents = function _migrateUnsentEvents(cb) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm curious why we have this callback chaining pattern in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to make sure they're not touching asyncStorage at the same time, e.g. making it synchronous via migrating unsent events before accessing it |
||
| Promise.all([ | ||
| AsyncStorage.getItem(this.options.unsentKey), | ||
| AsyncStorage.getItem(this.options.unsentIdentifyKey), | ||
| ]).then((values) => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you running this migration every single time the SDK inits? You only need to run it one time, which is when the user loads this new SDK for the first time. Do you need to check if values is null or something for all the subsequent times it's run?? You could also just check that the unsentKey and unsentIdentifyKey exist in Async storage, if not just skip this entire code block? |
||
| if (this.options.saveEvents) { | ||
| var unsentEventsString = values[0]; | ||
| var unsentIdentifyKey = values[1]; | ||
| Promise.all([ | ||
| AsyncStorage.setItem(this.options.unsentKey + this._storageSuffix, unsentEventsString), | ||
| AsyncStorage.setItem(this.options.unsentIdentifyKey + this._storageSuffix, unsentIdentifyKey), | ||
| ]).then(() => { | ||
| Promise.all([ | ||
| AsyncStorage.removeItem(this.options.unsentKey), | ||
| AsyncStorage.removeItem(this.options.unsentIdentifyKey), | ||
| ]).then(cb); | ||
| }).catch((err) => { | ||
| this.options.onError(err); | ||
| }); | ||
| } | ||
| }).catch((err) => { | ||
| this.options.onError(err); | ||
| }); | ||
| }; | ||
|
|
||
| /** | ||
| * @private | ||
| */ | ||
|
|
@@ -702,15 +732,15 @@ AmplitudeClient.prototype._saveReferrer = function _saveReferrer(referrer) { | |
| AmplitudeClient.prototype.saveEvents = function saveEvents() { | ||
| try { | ||
| if (AsyncStorage) { | ||
| AsyncStorage.setItem(this.options.unsentKey, JSON.stringify(this._unsentEvents)); | ||
| AsyncStorage.setItem(this.options.unsentKey + this._storageSuffix, JSON.stringify(this._unsentEvents)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not blocking but in terms of API I feel like |
||
| } else { | ||
| this._setInStorage(localStorage, this.options.unsentKey, JSON.stringify(this._unsentEvents)); | ||
djih marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } catch (e) {} | ||
|
|
||
| try { | ||
| if (AsyncStorage) { | ||
| AsyncStorage.setItem(this.options.unsentIdentifyKey, JSON.stringify(this._unsentIdentifys)); | ||
| AsyncStorage.setItem(this.options.unsentIdentifyKey + this._storageSuffix, JSON.stringify(this._unsentIdentifys)); | ||
| } else { | ||
| this._setInStorage(localStorage, this.options.unsentIdentifyKey, JSON.stringify(this._unsentIdentifys)); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is all wrapped in the callback of the
_migrateUnsentEvents