Skip to content

Commit 6e7bb41

Browse files
Initial attempt to refactor persistence
1 parent 52ab60a commit 6e7bb41

12 files changed

Lines changed: 1958 additions & 178 deletions

src/filteredMparticleUser.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default function filteredMparticleUser(
1010
return {
1111
getUserIdentities: function() {
1212
var currentUserIdentities = {};
13-
var identities = mpInstance._Persistence.getUserIdentities(mpid);
13+
var identities = mpInstance._Store.getUserIdentities(mpid);
1414

1515
for (var identityType in identities) {
1616
if (identities.hasOwnProperty(identityType)) {
@@ -67,10 +67,9 @@ export default function filteredMparticleUser(
6767
return userAttributesLists;
6868
},
6969
getAllUserAttributes: function() {
70+
// TODO: May not need to make a copy since Store should return a copy
7071
var userAttributesCopy = {};
71-
var userAttributes = mpInstance._Persistence.getAllUserAttributes(
72-
mpid
73-
);
72+
var userAttributes = mpInstance._Store.getUserAttributes(mpid);
7473

7574
if (userAttributes) {
7675
for (var prop in userAttributes) {

src/identity.js

Lines changed: 76 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
createKnownIdentities,
66
tryCacheIdentity,
77
} from './identity-utils';
8+
import { mergeObjects } from './utils';
89

910
var Messages = Constants.Messages,
1011
HTTPCodes = Constants.HTTPCodes;
@@ -21,12 +22,7 @@ export default function Identity(mpInstance) {
2122
currentSessionMPIDs
2223
) {
2324
if (previousMPID && currentMPID && previousMPID !== currentMPID) {
24-
var persistence = mpInstance._Persistence.getPersistence();
25-
if (persistence) {
26-
persistence.cu = currentMPID;
27-
persistence.gs.csm = currentSessionMPIDs;
28-
mpInstance._Persistence.savePersistence(persistence);
29-
}
25+
mpInstance._Store.swapIdentity(currentMPID, currentSessionMPIDs);
3026
}
3127
};
3228

@@ -588,6 +584,7 @@ export default function Identity(mpInstance) {
588584
if (mpInstance._Store) {
589585
mpid = mpInstance._Store.mpid;
590586
if (mpid) {
587+
// TODO: Why does this need to be sliced and mutated?
591588
mpid = mpInstance._Store.mpid.slice();
592589
return self.mParticleUser(
593590
mpid,
@@ -611,7 +608,7 @@ export default function Identity(mpInstance) {
611608
* @return {Object} the user for mpid
612609
*/
613610
getUser: function(mpid) {
614-
var persistence = mpInstance._Persistence.getPersistence();
611+
var persistence = mpInstance._Store.getPersistenceData();
615612
if (persistence) {
616613
if (
617614
persistence[mpid] &&
@@ -631,8 +628,9 @@ export default function Identity(mpInstance) {
631628
* @method getUsers
632629
* @return {Array} array of users
633630
*/
631+
// TODO: Create a test for this
634632
getUsers: function() {
635-
var persistence = mpInstance._Persistence.getPersistence();
633+
var persistence = mpInstance._Store.getPersistenceData();
636634
var users = [];
637635
if (persistence) {
638636
for (var key in persistence) {
@@ -810,9 +808,7 @@ export default function Identity(mpInstance) {
810808
getUserIdentities: function() {
811809
var currentUserIdentities = {};
812810

813-
var identities = mpInstance._Persistence.getUserIdentities(
814-
mpid
815-
);
811+
var identities = mpInstance._Store.getUserIdentities(mpid);
816812

817813
for (var identityType in identities) {
818814
if (identities.hasOwnProperty(identityType)) {
@@ -869,13 +865,10 @@ export default function Identity(mpInstance) {
869865
* @param {String} value
870866
*/
871867
setUserAttribute: function(key, newValue) {
872-
var cookies,
873-
userAttributes,
874-
previousUserAttributeValue,
875-
isNewAttribute;
876-
868+
// QUESTION: Why does this need to reset the timer?
877869
mpInstance._SessionManager.resetSessionTimer();
878870

871+
// TODO: break out these guards to return directly
879872
if (mpInstance._Helpers.canLog()) {
880873
if (
881874
!mpInstance._Helpers.Validators.isValidAttributeValue(
@@ -898,7 +891,9 @@ export default function Identity(mpInstance) {
898891
JSON.stringify({ key: key, value: newValue })
899892
);
900893
} else {
901-
cookies = mpInstance._Persistence.getPersistence();
894+
var userAttributes;
895+
var previousUserAttributeValue;
896+
var isNewAttribute;
902897

903898
userAttributes = this.getAllUserAttributes();
904899

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

919914
userAttributes[key] = newValue;
920-
if (cookies && cookies[mpid]) {
921-
cookies[mpid].ua = userAttributes;
922-
mpInstance._Persistence.savePersistence(
923-
cookies,
924-
mpid
925-
);
926-
}
915+
916+
mpInstance._Store.setUserAttributes(
917+
mpid,
918+
userAttributes
919+
);
927920

928921
self.sendUserAttributeChangeEvent(
929922
key,
@@ -974,7 +967,8 @@ export default function Identity(mpInstance) {
974967
* @param {String} key
975968
*/
976969
removeUserAttribute: function(key) {
977-
var cookies, userAttributes;
970+
// var persistence;
971+
var userAttributes;
978972
mpInstance._SessionManager.resetSessionTimer();
979973

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

993988
userAttributes = this.getAllUserAttributes();
994989

@@ -997,20 +992,25 @@ export default function Identity(mpInstance) {
997992
key
998993
);
999994

995+
// QUESTION: Why is this here? If existingProp is not found
996+
// we still try to remove t?
1000997
if (existingProp) {
1001998
key = existingProp;
1002999
}
10031000

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

1006+
// QUESTION: What happens if key is null?
10081007
delete userAttributes[key];
10091008

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

10151015
self.sendUserAttributeChangeEvent(
10161016
key,
@@ -1039,11 +1039,11 @@ export default function Identity(mpInstance) {
10391039
* @param {Array} value an array of values
10401040
*/
10411041
setUserAttributeList: function(key, newValue) {
1042-
var cookies,
1043-
userAttributes,
1044-
previousUserAttributeValue,
1045-
isNewAttribute,
1046-
userAttributeChange;
1042+
// var persistence;
1043+
var userAttributes;
1044+
var previousUserAttributeValue;
1045+
var isNewAttribute;
1046+
var userAttributeChange;
10471047

10481048
mpInstance._SessionManager.resetSessionTimer();
10491049

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

10731075
userAttributes = this.getAllUserAttributes();
10741076

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

10891091
userAttributes[key] = arrayCopy;
1090-
if (cookies && cookies[mpid]) {
1091-
cookies[mpid].ua = userAttributes;
1092-
mpInstance._Persistence.savePersistence(cookies, mpid);
1093-
}
1092+
// if (persistence && persistence[mpid]) {
1093+
// persistence[mpid].ua = userAttributes;
1094+
mpInstance._Store.setUserAttributes(mpid, userAttributes);
1095+
// }
10941096

10951097
// If the new attributeList length is different previous, then there is a change event.
10961098
// Loop through new attributes list, see if they are all in the same index as previous user attributes list
@@ -1191,31 +1193,15 @@ export default function Identity(mpInstance) {
11911193
return userAttributesLists;
11921194
},
11931195
/**
1194-
* Returns all user attributes
1196+
* Returns a copy of all user attributes
11951197
* @method getAllUserAttributes
11961198
* @return {Object} an object of all user attributes. Example: { attr1: 'value1', attr2: ['a', 'b', 'c'] }
11971199
*/
11981200
getAllUserAttributes: function() {
1199-
var userAttributesCopy = {};
1200-
var userAttributes = mpInstance._Persistence.getAllUserAttributes(
1201+
var userAttributes = mpInstance._Store.getAllUserAttributes(
12011202
mpid
12021203
);
1203-
1204-
if (userAttributes) {
1205-
for (var prop in userAttributes) {
1206-
if (userAttributes.hasOwnProperty(prop)) {
1207-
if (Array.isArray(userAttributes[prop])) {
1208-
userAttributesCopy[prop] = userAttributes[
1209-
prop
1210-
].slice();
1211-
} else {
1212-
userAttributesCopy[prop] = userAttributes[prop];
1213-
}
1214-
}
1215-
}
1216-
}
1217-
1218-
return userAttributesCopy;
1204+
return mergeObjects({}, userAttributes);
12191205
},
12201206
/**
12211207
* Returns the cart object for the current user
@@ -1235,18 +1221,25 @@ export default function Identity(mpInstance) {
12351221
* @return a ConsentState object
12361222
*/
12371223
getConsentState: function() {
1238-
return mpInstance._Persistence.getConsentState(mpid);
1224+
// TODO: we should not assume mpid comes from persistence
1225+
// Likely we should check the current user
1226+
// return mpInstance._Persistence.getConsentState(mpid);
1227+
return mpInstance._Store.getConsentState(mpid);
12391228
},
12401229
/**
12411230
* Sets the Consent State stored locally for this user.
12421231
* @method setConsentState
12431232
* @param {Object} consent state
12441233
*/
12451234
setConsentState: function(state) {
1246-
mpInstance._Persistence.saveUserConsentStateToCookies(
1247-
mpid,
1248-
state
1249-
);
1235+
// TODO: we should not assume mpid comes from persistence
1236+
// Likely we should check the current user
1237+
// mpInstance._Persistence.saveUserConsentStateToCookies(
1238+
// mpid,
1239+
// state
1240+
// );
1241+
mpInstance._Store.setConsentState(mpid, state);
1242+
12501243
mpInstance._Forwarders.initForwarders(
12511244
this.getUserIdentities().userIdentities,
12521245
mpInstance._APIClient.prepareForwardingStats
@@ -1260,10 +1253,16 @@ export default function Identity(mpInstance) {
12601253
return isLoggedIn;
12611254
},
12621255
getLastSeenTime: function() {
1263-
return mpInstance._Persistence.getLastSeenTime(mpid);
1256+
// TODO: we should not assume mpid comes from persistence
1257+
// Likely we should check the current user
1258+
// return mpInstance._Persistence.getLastSeenTime(mpid);
1259+
return mpInstance._Store.getLastSeenTime(mpid);
12641260
},
12651261
getFirstSeenTime: function() {
1266-
return mpInstance._Persistence.getFirstSeenTime(mpid);
1262+
// TODO: we should not assume mpid comes from persistence
1263+
// Likely we should check the current user
1264+
// return mpInstance._Persistence.getFirstSeenTime(mpid);
1265+
return mpInstance._Store.getFirstSeenTime(mpid);
12671266
},
12681267
};
12691268
};
@@ -1497,18 +1496,12 @@ export default function Identity(mpInstance) {
14971496
mpInstance._Store.mpid = identityApiResult.mpid;
14981497

14991498
if (prevUser) {
1500-
mpInstance._Persistence.setLastSeenTime(previousMPID);
1499+
mpInstance._Store.setLastSeenTime(previousMPID);
15011500
}
15021501

1503-
if (
1504-
!mpInstance._Persistence.getFirstSeenTime(
1505-
identityApiResult.mpid
1506-
)
1507-
)
1502+
if (!mpInstance._Store.getFirstSeenTime(identityApiResult.mpid))
15081503
mpidIsNotInCookies = true;
1509-
mpInstance._Persistence.setFirstSeenTime(
1510-
identityApiResult.mpid
1511-
);
1504+
mpInstance._Store.setFirstSeenTime(identityApiResult.mpid);
15121505
}
15131506

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

1535-
mpInstance._Persistence.saveUserIdentitiesToPersistence(
1528+
mpInstance._Store.setUserIdentities(
15361529
previousMPID,
15371530
newIdentitiesByType
15381531
);
@@ -1561,7 +1554,7 @@ export default function Identity(mpInstance) {
15611554
prevUser &&
15621555
identityApiResult.mpid === prevUser.getMPID()
15631556
) {
1564-
mpInstance._Persistence.setFirstSeenTime(
1557+
mpInstance._Store.setFirstSeenTime(
15651558
identityApiResult.mpid
15661559
);
15671560
}
@@ -1595,6 +1588,7 @@ export default function Identity(mpInstance) {
15951588
);
15961589
}
15971590

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

16211615
// https://go.mparticle.com/work/SQDSDKS-6041
1622-
mpInstance._Persistence.saveUserIdentitiesToPersistence(
1616+
mpInstance._Store.setUserIdentities(
16231617
identityApiResult.mpid,
16241618
newIdentitiesByType
16251619
);
1626-
mpInstance._Persistence.update();
1620+
// Is update actually necessary here?
1621+
// mpInstance._Persistence.update();
16271622

1623+
// TODO: LEFT OFF HERE
1624+
// TODO: Migrate this to store
16281625
mpInstance._Persistence.findPrevCookiesBasedOnUI(
16291626
identityApiData
16301627
);

0 commit comments

Comments
 (0)