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 21, 2024
1 parent 52ab60a commit d2d6b82
Show file tree
Hide file tree
Showing 8 changed files with 1,785 additions and 119 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
150 changes: 73 additions & 77 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 @@ -632,7 +629,7 @@ export default function Identity(mpInstance) {
* @return {Array} array of users
*/
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 +807,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 +864,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 +890,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 +911,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 +966,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 +981,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 @@ -1007,9 +1001,13 @@ export default function Identity(mpInstance) {

delete userAttributes[key];

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

self.sendUserAttributeChangeEvent(
Expand Down Expand Up @@ -1039,11 +1037,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 +1066,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 @@ -1086,10 +1086,11 @@ export default function Identity(mpInstance) {
isNewAttribute = true;
}

// QUESITON: can we just use mpInstance._Store.setUserAttributes here?
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.setPersistence(persistence);
}

// If the new attributeList length is different previous, then there is a change event.
Expand Down Expand Up @@ -1191,31 +1192,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 +1220,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 +1252,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 +1495,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 +1524,7 @@ export default function Identity(mpInstance) {
identityApiData.userIdentities
);

mpInstance._Persistence.saveUserIdentitiesToPersistence(
mpInstance._Store.setUserIdentities(
previousMPID,
newIdentitiesByType
);
Expand Down Expand Up @@ -1561,7 +1553,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 +1587,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 +1612,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 d2d6b82

Please sign in to comment.