Skip to content

Commit

Permalink
Initial attempt to refactor persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
alexs-mparticle committed Mar 27, 2024
1 parent 52ab60a commit 6e7bb41
Show file tree
Hide file tree
Showing 12 changed files with 1,958 additions and 178 deletions.
7 changes: 3 additions & 4 deletions src/filteredMparticleUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default function filteredMparticleUser(
return {
getUserIdentities: function() {
var currentUserIdentities = {};
var identities = mpInstance._Persistence.getUserIdentities(mpid);
var identities = mpInstance._Store.getUserIdentities(mpid);

for (var identityType in identities) {
if (identities.hasOwnProperty(identityType)) {
Expand Down Expand Up @@ -67,10 +67,9 @@ export default function filteredMparticleUser(
return userAttributesLists;
},
getAllUserAttributes: function() {
// TODO: May not need to make a copy since Store should return a copy
var userAttributesCopy = {};
var userAttributes = mpInstance._Persistence.getAllUserAttributes(
mpid
);
var userAttributes = mpInstance._Store.getUserAttributes(mpid);

if (userAttributes) {
for (var prop in userAttributes) {
Expand Down
155 changes: 76 additions & 79 deletions src/identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
createKnownIdentities,
tryCacheIdentity,
} from './identity-utils';
import { mergeObjects } from './utils';

var Messages = Constants.Messages,
HTTPCodes = Constants.HTTPCodes;
Expand All @@ -21,12 +22,7 @@ export default function Identity(mpInstance) {
currentSessionMPIDs
) {
if (previousMPID && currentMPID && previousMPID !== currentMPID) {
var persistence = mpInstance._Persistence.getPersistence();
if (persistence) {
persistence.cu = currentMPID;
persistence.gs.csm = currentSessionMPIDs;
mpInstance._Persistence.savePersistence(persistence);
}
mpInstance._Store.swapIdentity(currentMPID, currentSessionMPIDs);
}
};

Expand Down Expand Up @@ -588,6 +584,7 @@ export default function Identity(mpInstance) {
if (mpInstance._Store) {
mpid = mpInstance._Store.mpid;
if (mpid) {
// TODO: Why does this need to be sliced and mutated?
mpid = mpInstance._Store.mpid.slice();
return self.mParticleUser(
mpid,
Expand All @@ -611,7 +608,7 @@ export default function Identity(mpInstance) {
* @return {Object} the user for mpid
*/
getUser: function(mpid) {
var persistence = mpInstance._Persistence.getPersistence();
var persistence = mpInstance._Store.getPersistenceData();
if (persistence) {
if (
persistence[mpid] &&
Expand All @@ -631,8 +628,9 @@ export default function Identity(mpInstance) {
* @method getUsers
* @return {Array} array of users
*/
// TODO: Create a test for this
getUsers: function() {
var persistence = mpInstance._Persistence.getPersistence();
var persistence = mpInstance._Store.getPersistenceData();
var users = [];
if (persistence) {
for (var key in persistence) {
Expand Down Expand Up @@ -810,9 +808,7 @@ export default function Identity(mpInstance) {
getUserIdentities: function() {
var currentUserIdentities = {};

var identities = mpInstance._Persistence.getUserIdentities(
mpid
);
var identities = mpInstance._Store.getUserIdentities(mpid);

for (var identityType in identities) {
if (identities.hasOwnProperty(identityType)) {
Expand Down Expand Up @@ -869,13 +865,10 @@ export default function Identity(mpInstance) {
* @param {String} value
*/
setUserAttribute: function(key, newValue) {
var cookies,
userAttributes,
previousUserAttributeValue,
isNewAttribute;

// QUESTION: Why does this need to reset the timer?
mpInstance._SessionManager.resetSessionTimer();

// TODO: break out these guards to return directly
if (mpInstance._Helpers.canLog()) {
if (
!mpInstance._Helpers.Validators.isValidAttributeValue(
Expand All @@ -898,7 +891,9 @@ export default function Identity(mpInstance) {
JSON.stringify({ key: key, value: newValue })
);
} else {
cookies = mpInstance._Persistence.getPersistence();
var userAttributes;
var previousUserAttributeValue;
var isNewAttribute;

userAttributes = this.getAllUserAttributes();

Expand All @@ -917,13 +912,11 @@ export default function Identity(mpInstance) {
}

userAttributes[key] = newValue;
if (cookies && cookies[mpid]) {
cookies[mpid].ua = userAttributes;
mpInstance._Persistence.savePersistence(
cookies,
mpid
);
}

mpInstance._Store.setUserAttributes(
mpid,
userAttributes
);

self.sendUserAttributeChangeEvent(
key,
Expand Down Expand Up @@ -974,7 +967,8 @@ export default function Identity(mpInstance) {
* @param {String} key
*/
removeUserAttribute: function(key) {
var cookies, userAttributes;
// var persistence;
var userAttributes;
mpInstance._SessionManager.resetSessionTimer();

if (!mpInstance._Helpers.Validators.isValidKeyValue(key)) {
Expand All @@ -988,7 +982,8 @@ export default function Identity(mpInstance) {
JSON.stringify({ key: key, value: null })
);
} else {
cookies = mpInstance._Persistence.getPersistence();
// QUESTION: Is this redundant?
// persistence = mpInstance._Store.getPersistenceData();

userAttributes = this.getAllUserAttributes();

Expand All @@ -997,20 +992,25 @@ export default function Identity(mpInstance) {
key
);

// QUESTION: Why is this here? If existingProp is not found
// we still try to remove t?
if (existingProp) {
key = existingProp;
}

// TODO: Technically, this is the value of the object, not the key
var deletedUAKeyCopy = userAttributes[key]
? userAttributes[key].toString()
: null;

// QUESTION: What happens if key is null?
delete userAttributes[key];

if (cookies && cookies[mpid]) {
cookies[mpid].ua = userAttributes;
mpInstance._Persistence.savePersistence(cookies, mpid);
}
// if (persistence && persistence[mpid]) {
// persistence[mpid].ua = userAttributes;
// QUESTION: why is mpid an argument here when it's not in the signature?
mpInstance._Store.setUserAttributes(mpid, userAttributes);
// }

self.sendUserAttributeChangeEvent(
key,
Expand Down Expand Up @@ -1039,11 +1039,11 @@ export default function Identity(mpInstance) {
* @param {Array} value an array of values
*/
setUserAttributeList: function(key, newValue) {
var cookies,
userAttributes,
previousUserAttributeValue,
isNewAttribute,
userAttributeChange;
// var persistence;
var userAttributes;
var previousUserAttributeValue;
var isNewAttribute;
var userAttributeChange;

mpInstance._SessionManager.resetSessionTimer();

Expand All @@ -1068,7 +1068,9 @@ export default function Identity(mpInstance) {
JSON.stringify({ key: key, value: arrayCopy })
);
} else {
cookies = mpInstance._Persistence.getPersistence();
// QUESTION: Is this redundant?
// persistence = mpInstance._Persistence.getPersistence();
// persistence = mpInstance._Store.getPersistenceData();

userAttributes = this.getAllUserAttributes();

Expand All @@ -1087,10 +1089,10 @@ export default function Identity(mpInstance) {
}

userAttributes[key] = arrayCopy;
if (cookies && cookies[mpid]) {
cookies[mpid].ua = userAttributes;
mpInstance._Persistence.savePersistence(cookies, mpid);
}
// if (persistence && persistence[mpid]) {
// persistence[mpid].ua = userAttributes;
mpInstance._Store.setUserAttributes(mpid, userAttributes);
// }

// If the new attributeList length is different previous, then there is a change event.
// Loop through new attributes list, see if they are all in the same index as previous user attributes list
Expand Down Expand Up @@ -1191,31 +1193,15 @@ export default function Identity(mpInstance) {
return userAttributesLists;
},
/**
* Returns all user attributes
* Returns a copy of all user attributes
* @method getAllUserAttributes
* @return {Object} an object of all user attributes. Example: { attr1: 'value1', attr2: ['a', 'b', 'c'] }
*/
getAllUserAttributes: function() {
var userAttributesCopy = {};
var userAttributes = mpInstance._Persistence.getAllUserAttributes(
var userAttributes = mpInstance._Store.getAllUserAttributes(
mpid
);

if (userAttributes) {
for (var prop in userAttributes) {
if (userAttributes.hasOwnProperty(prop)) {
if (Array.isArray(userAttributes[prop])) {
userAttributesCopy[prop] = userAttributes[
prop
].slice();
} else {
userAttributesCopy[prop] = userAttributes[prop];
}
}
}
}

return userAttributesCopy;
return mergeObjects({}, userAttributes);
},
/**
* Returns the cart object for the current user
Expand All @@ -1235,18 +1221,25 @@ export default function Identity(mpInstance) {
* @return a ConsentState object
*/
getConsentState: function() {
return mpInstance._Persistence.getConsentState(mpid);
// TODO: we should not assume mpid comes from persistence
// Likely we should check the current user
// return mpInstance._Persistence.getConsentState(mpid);
return mpInstance._Store.getConsentState(mpid);
},
/**
* Sets the Consent State stored locally for this user.
* @method setConsentState
* @param {Object} consent state
*/
setConsentState: function(state) {
mpInstance._Persistence.saveUserConsentStateToCookies(
mpid,
state
);
// TODO: we should not assume mpid comes from persistence
// Likely we should check the current user
// mpInstance._Persistence.saveUserConsentStateToCookies(
// mpid,
// state
// );
mpInstance._Store.setConsentState(mpid, state);

mpInstance._Forwarders.initForwarders(
this.getUserIdentities().userIdentities,
mpInstance._APIClient.prepareForwardingStats
Expand All @@ -1260,10 +1253,16 @@ export default function Identity(mpInstance) {
return isLoggedIn;
},
getLastSeenTime: function() {
return mpInstance._Persistence.getLastSeenTime(mpid);
// TODO: we should not assume mpid comes from persistence
// Likely we should check the current user
// return mpInstance._Persistence.getLastSeenTime(mpid);
return mpInstance._Store.getLastSeenTime(mpid);
},
getFirstSeenTime: function() {
return mpInstance._Persistence.getFirstSeenTime(mpid);
// TODO: we should not assume mpid comes from persistence
// Likely we should check the current user
// return mpInstance._Persistence.getFirstSeenTime(mpid);
return mpInstance._Store.getFirstSeenTime(mpid);
},
};
};
Expand Down Expand Up @@ -1497,18 +1496,12 @@ export default function Identity(mpInstance) {
mpInstance._Store.mpid = identityApiResult.mpid;

if (prevUser) {
mpInstance._Persistence.setLastSeenTime(previousMPID);
mpInstance._Store.setLastSeenTime(previousMPID);
}

if (
!mpInstance._Persistence.getFirstSeenTime(
identityApiResult.mpid
)
)
if (!mpInstance._Store.getFirstSeenTime(identityApiResult.mpid))
mpidIsNotInCookies = true;
mpInstance._Persistence.setFirstSeenTime(
identityApiResult.mpid
);
mpInstance._Store.setFirstSeenTime(identityApiResult.mpid);
}

if (xhr.status === 200) {
Expand All @@ -1532,7 +1525,7 @@ export default function Identity(mpInstance) {
identityApiData.userIdentities
);

mpInstance._Persistence.saveUserIdentitiesToPersistence(
mpInstance._Store.setUserIdentities(
previousMPID,
newIdentitiesByType
);
Expand Down Expand Up @@ -1561,7 +1554,7 @@ export default function Identity(mpInstance) {
prevUser &&
identityApiResult.mpid === prevUser.getMPID()
) {
mpInstance._Persistence.setFirstSeenTime(
mpInstance._Store.setFirstSeenTime(
identityApiResult.mpid
);
}
Expand Down Expand Up @@ -1595,6 +1588,7 @@ export default function Identity(mpInstance) {
);
}

// TODO: Do I need to care about this for persistence refactor right now?
mpInstance._CookieSyncManager.attemptCookieSync(
previousMPID,
identityApiResult.mpid,
Expand All @@ -1619,12 +1613,15 @@ export default function Identity(mpInstance) {
}

// https://go.mparticle.com/work/SQDSDKS-6041
mpInstance._Persistence.saveUserIdentitiesToPersistence(
mpInstance._Store.setUserIdentities(
identityApiResult.mpid,
newIdentitiesByType
);
mpInstance._Persistence.update();
// Is update actually necessary here?
// mpInstance._Persistence.update();

// TODO: LEFT OFF HERE
// TODO: Migrate this to store
mpInstance._Persistence.findPrevCookiesBasedOnUI(
identityApiData
);
Expand Down
Loading

0 comments on commit 6e7bb41

Please sign in to comment.