Skip to content
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

Initial attempt to refactor persistence #847

Draft
wants to merge 6 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 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 @@ -68,9 +68,7 @@ export default function filteredMparticleUser(
},
getAllUserAttributes: function() {
var userAttributesCopy = {};
var userAttributes = mpInstance._Persistence.getAllUserAttributes(
mpid
);
var userAttributes = mpInstance._Store.getUserAttributes(mpid);

if (userAttributes) {
for (var prop in userAttributes) {
Expand Down
165 changes: 84 additions & 81 deletions src/identity.js

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions src/mp-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ export default function mParticleInstance(instanceName) {
this._Identity = new Identity(this);
this.Identity = this._Identity.IdentityAPI;
this.generateHash = this._Helpers.generateHash;

// TODO: Replace with store
this.getDeviceId = this._Persistence.getDeviceId;

if (typeof window !== 'undefined') {
Expand Down Expand Up @@ -145,6 +147,7 @@ export default function mParticleInstance(instanceName) {
*/
this.reset = function(instance) {
try {
// QUESITON: Should we move this into Store?
instance._Persistence.resetPersistence();
if (instance._Store) {
delete instance._Store;
Expand All @@ -159,6 +162,8 @@ export default function mParticleInstance(instanceName) {
delete instance._Store;
}
instance._Store = new Store(config, instance);

// TODO: Refactor this to be a store method
instance._Store.isLocalStorageAvailable = instance._Persistence.determineLocalStorageAvailability(
window.localStorage
);
Expand Down Expand Up @@ -217,9 +222,7 @@ export default function mParticleInstance(instanceName) {
}, self);

if (queued) return;

self._Store.SDKConfig.appVersion = version;
self._Persistence.update();
self._Store.setAppVersion(version);
};
/**
* Sets the device id
Expand All @@ -231,7 +234,7 @@ export default function mParticleInstance(instanceName) {
self.setDeviceId(guid);
}, self);
if (queued) return;
this._Persistence.setDeviceId(guid);
this._Store.setDeviceId(guid);
};
/**
* Returns a boolean for whether or not the SDKhas been fully initialized
Expand Down
2 changes: 1 addition & 1 deletion src/persistence.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export interface IGlobalStoreV2MinifiedKeys {
export interface IPersistenceMinified extends Dictionary {
cu: MPID; // Current User MPID
gs: IGlobalStoreV2MinifiedKeys;
l: false; // IsLoggedIn
l: boolean; // IsLoggedIn

// Persistence Minified can also store optional dictionaries with
// an idex of MPID
Expand Down
6 changes: 6 additions & 0 deletions src/persistence.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ export default function _Persistence(mpInstance) {
}
};

// TODO: Move this method into the store
// https://go.mparticle.com/work/SQDSDKS-6045
this.storeDataInMemory = function(obj, currentMPID) {
try {
Expand Down Expand Up @@ -237,6 +238,8 @@ export default function _Persistence(mpInstance) {
mpInstance._Store.sessionStartDate = new Date();
}

// TODO: Investigate to see if this is still relevant, because it's an
// antipattern
if (currentMPID) {
obj = obj[currentMPID];
} else {
Expand Down Expand Up @@ -394,6 +397,7 @@ export default function _Persistence(mpInstance) {
}
};

// TODO: Replace with store version
function setGlobalStorageAttributes(data) {
var store = mpInstance._Store;
data.gs.sid = store.sessionId;
Expand Down Expand Up @@ -694,6 +698,8 @@ export default function _Persistence(mpInstance) {
);
}

// TODO: Write tests around this function
// TODO: How should this be migrated to the store?
this.findPrevCookiesBasedOnUI = function(identityApiData) {
var persistence = mpInstance._Persistence.getPersistence();
var matchedUser;
Expand Down
34 changes: 15 additions & 19 deletions src/sessionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ export default function SessionManager(
self.endSession();
self.startNewSession();
} else {
// https://go.mparticle.com/work/SQDSDKS-6045
const persistence: IPersistenceMinified = mpInstance._Persistence.getPersistence();
if (persistence && !persistence.cu) {
if (!mpInstance._Store.hasCurrentUser()) {
mpInstance.Identity.identify(
mpInstance._Store.SDKConfig.identifyRequest,
mpInstance._Store.SDKConfig.identityCallback
Expand Down Expand Up @@ -129,7 +127,7 @@ export default function SessionManager(
messageType: Types.MessageType.SessionEnd,
});

nullifySession();
mpInstance._Store.nullifySession();
return;
}

Expand All @@ -147,25 +145,30 @@ export default function SessionManager(
let sessionTimeoutInMilliseconds: number;
let timeSinceLastEventSent: number;

const cookies: IPersistenceMinified = mpInstance._Persistence.getPersistence();
// TODO: Destructure les and sid from persistence
const persistence: IPersistenceMinified = mpInstance._Store.getPersistenceData();

if (!cookies || cookies.gs && !cookies.gs.sid) {
if (!persistence || (persistence.gs && !persistence.gs.sid)) {
mpInstance.Logger.verbose(
Messages.InformationMessages.NoSessionToEnd
);
return;
}

// QUESTION: Can we refactor this into the store?
// sessionId is not equal to cookies.sid if cookies.sid is changed in another tab
if (cookies.gs.sid && mpInstance._Store.sessionId !== cookies.gs.sid) {
mpInstance._Store.sessionId = cookies.gs.sid;
if (
persistence.gs.sid &&
mpInstance._Store.sessionId !== persistence.gs.sid
) {
mpInstance._Store.sessionId = persistence.gs.sid;
}

if (cookies?.gs?.les) {
if (persistence && persistence.gs && persistence.gs.les) {
sessionTimeoutInMilliseconds =
mpInstance._Store.SDKConfig.sessionTimeout * 60000;
const newDate: number = new Date().getTime();
timeSinceLastEventSent = newDate - cookies.gs.les;
timeSinceLastEventSent = newDate - persistence.gs.les;

if (timeSinceLastEventSent < sessionTimeoutInMilliseconds) {
self.setSessionTimer();
Expand All @@ -175,7 +178,7 @@ export default function SessionManager(
});

mpInstance._Store.sessionStartDate = null;
nullifySession();
mpInstance._Store.nullifySession();
}
}
};
Expand Down Expand Up @@ -206,7 +209,7 @@ export default function SessionManager(

this.startNewSessionIfNeeded = function(): void {
if (!mpInstance._Store.webviewBridgeEnabled) {
const persistence = mpInstance._Persistence.getPersistence();
const persistence = mpInstance._Store.getPersistenceData();

if (!mpInstance._Store.sessionId && persistence) {
if (persistence.sid) {
Expand All @@ -217,11 +220,4 @@ export default function SessionManager(
}
}
};

function nullifySession(): void {
mpInstance._Store.sessionId = null;
mpInstance._Store.dateLastEventSent = null;
mpInstance._Store.sessionAttributes = {};
mpInstance._Persistence.update();
}
}
Loading
Loading