Skip to content

Conversation

@sputh
Copy link
Contributor

@sputh sputh commented Nov 7, 2019

Currently, when saving events to AsyncStorage, we we are only using unsentKey as the identifier.
This can cause events from multiple projects to get mixed up. By appending this._storageSuffix,
we are adding more specificity to keep unsent events within the correct project scope.

Addresses: #203

FYI: no need to do this for web because it is calling _setInStorage() which utilizes this._storageSuffix already

…re storing in AsyncStorage

Currently, when saving events to AsyncStorage, we we are only using unsentKey as the identifier.
This can cause events from multiple projects to get mixed up. By appending this._storageSuffix,
we are adding more specificity to keep unsent events within the correct project scope.
}
}).catch((err) => {
this.options.onError(err);
});
Copy link
Contributor Author

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

Copy link
Member

@djih djih left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a few tests around the migration? I want to be 100% sure that we don't lose or corrupt the unsent event / identify data.
Some potential test cases:

  • normal migration of event / identify in old key to new key, and validate old key gone, new key has stuff
  • migration is skipped if stuff is already migrated / or nothing to migrate
  • also make sure this doesn't mess up the people using localstorage / not using Async?

Also recommend @blazzy taking a look

try {
if (AsyncStorage) {
AsyncStorage.setItem(this.options.unsentKey, JSON.stringify(this._unsentEvents));
AsyncStorage.setItem(this.options.unsentKey + this._storageSuffix, JSON.stringify(this._unsentEvents));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not blocking but in terms of API I feel like _setInStorage should encapsulate the AsyncStorage logic inside it, so that whoever calls _setInStorage doesn't have to worry about checking Async etc etc you know what I mean?

/**
* @private
*/
AmplitudeClient.prototype._migrateUnsentEvents = function _migrateUnsentEvents(cb) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious why we have this callback chaining pattern in init? Is it because we need it to interact with AsyncStorage? in the case where init is just a consecutive / synchronous you could just write the logic all straight out without any callbacks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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) => {
Copy link
Member

Choose a reason for hiding this comment

The 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?

var unsentIdentifyKey = values[1];
Promise.all([
AsyncStorage.setItem(this.options.unsentKey + this._storageSuffix, unsentEventsString),
AsyncStorage.setItem(this.options.unsentIdentifyKey + this._storageSuffix, unsentIdentifyKey ),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra space after unsentIdentifyKey
side note: would be useful to add a linter or prettier to the JS SDK?

@sputh
Copy link
Contributor Author

sputh commented Nov 20, 2019

@djih updated with the migration. i think it's going to be a lot of effort to add the react-native specific tests + configs. I want to prioritize getting it out for now and I'll do a follow up on testing

@sputh sputh force-pushed the better-react-native-async-storage branch from 1d2bd6c to fe025f0 Compare November 23, 2019 00:17
@sputh sputh merged commit b06134c into amplitude:master Nov 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants