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 decouple persistence #832

Draft
wants to merge 7 commits into
base: development
Choose a base branch
from

Conversation

alexs-mparticle
Copy link
Collaborator

Instructions

  1. PR target branch should be against development
  2. PR title name should follow this format: https://github.com/mParticle/mparticle-workflows/blob/main/.github/workflows/pr-title-check.yml
  3. PR branch prefix should follow this format: https://github.com/mParticle/mparticle-workflows/blob/main/.github/workflows/pr-branch-check-name.yml

Summary

  • {provide a thorough description of the changes}

Testing Plan

  • Was this tested locally? If not, explain why.
  • {explain how this has been tested, and what, if any, additional testing should be done}

Reference Issue (For mParticle employees only. Ignore if you are an outside contributor)

Copy link
Member

@rmi22186 rmi22186 left a comment

Choose a reason for hiding this comment

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

This looks good so far. Here are my comments for Monday morning. I didn't look at the tests since they may change given my feedback.

Comment on lines +249 to +250
persistence?.gs &&
persistence?.gs.sid &&
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
persistence?.gs &&
persistence?.gs.sid &&
persistence?.gs?.sid &&

@@ -190,6 +220,8 @@ export default function SessionManager(
mpInstance._Store.SDKConfig.sessionTimeout * 60000;

mpInstance._Store.globalTimer = window.setTimeout(function() {
// TODO: kill this global timer when ending session
Copy link
Member

Choose a reason for hiding this comment

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

good call

Comment on lines +176 to +177
persistence?.gs &&
persistence?.gs.sid &&
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
persistence?.gs &&
persistence?.gs.sid &&
persistence?.gs?.sid &&

src/mp-instance.js Outdated Show resolved Hide resolved
src/mp-instance.js Outdated Show resolved Hide resolved
Comment on lines +190 to +191
persistence?.gs &&
persistence?.gs.les &&
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
persistence?.gs &&
persistence?.gs.les &&
persistence?.gs?.les &&


if (cookies.gs && !cookies.gs.sid) {
// QUESTION: What constitutes ending a session if we don't have persistence?
Copy link
Member

Choose a reason for hiding this comment

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

Addressed in earlier comments, but if we don' have persistence, then the expectation is that everything is saved to the store, so the logic would be if persistence is disabled, check the store instead

@@ -179,8 +210,7 @@ export default function SessionManager(
messageType: Types.MessageType.SessionEnd,
});

mpInstance._Store.sessionStartDate = null;
Copy link
Member

Choose a reason for hiding this comment

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

I think line 182 was removed erroneously.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I intentionally moved it to a new method Store.nullifySession() since it made sense to me to keep this grouped with the other nullify session logic. Is there a reason to not nullify the session start date when we nullify a session?

mpInstance._Store.sessionId = persistence.gs.sid;
}

// TODO: Make this a store method
Copy link
Member

Choose a reason for hiding this comment

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

Can you clarify this TODO? Are you arguing that startNewSession should be on the store and not on the session module?

Copy link
Member

Choose a reason for hiding this comment

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

I think the answer the the above is yes. However I think there's a lot happening in this method that isn't just store related. Maybe there is a way to keep the session logic in session, and at the end, create a new method about updating the store's session attributes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I probably should have put this comment before the startNewSession method. My thought is that we're doing a lot of session related activities within the context of a store. Part of me thinks that we should split out the Session related persistence/store attributes into a seaprate Session object, so that the Session Manager can simply manage Sessions. As we've discussed, the store holds a lot of data about the state of the SDK, but a lot of unrelated modules directly modify the state which makes it hard to predict what the current state of the SDK is, so I thought a quick path to tidying things up would be to create a startNewSession method in the store to clean some things up.

src/store.ts Outdated
if (config.hasOwnProperty('usePersistence')) {
this.SDKConfig.usePersistence = config.usePersistence;
} else {
this.SDKConfig.usePersistence = false;
Copy link
Member

Choose a reason for hiding this comment

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

This should be true by default. Is this false because you are in the middle of development?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good catch. I made this default to false before we made our decision to default it to true. I'll update.

// TODO: Maybe create an updateIdentity method?
// Store relevant identity settings into Store
mpInstance._Store.mpid = currentMPID;
mpInstance._Store.currentSessionMPIDs = currentSessionMPIDs;
Copy link
Collaborator Author

@alexs-mparticle alexs-mparticle Jan 29, 2024

Choose a reason for hiding this comment

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

Lines 20 and 21 can just be removed in favor of this approach.

Line 23 can be combined with the check on 18 so it's using Persistence or Store.

Suggested change
mpInstance._Store.currentSessionMPIDs = currentSessionMPIDs;
mpInstance._Store.currentSessionMPIDs = currentSessionMPIDs;
persistence.cu = currentMPID;
persistence.gs.csm = currentSessionMPIDs;

}

// QUESTION: Does this only matter for cookies or do we need it for
Copy link
Collaborator Author

@alexs-mparticle alexs-mparticle Jan 29, 2024

Choose a reason for hiding this comment

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

It's called cookies, but actually applies to all persistence methods.

Also requires access to the DOM, so we may need a control for that to make sure dom exists.

userAttributes?: Dictionary<UserAttributes>;
getUserAttributes?(mpid: MPID): UserAttributes;

// QUESTION: Is there a difference between this and currentUserIdentities?
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Suggested change
// QUESTION: Is there a difference between this and currentUserIdentities?
// QUESTION: Is there a difference between this and currentUserIdentities?

this.setDeviceId = (deviceId: string): void => {
// TODO: This should update persistence
this.deviceId = deviceId;
mpInstance._Persistence.update();
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Do we still need to call update internally?

};

this.setConsentState = (mpid: MPID, consentState: ConsentState) => {
// QUESTION: What is the context here?
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Comment can be removed as it is not necessary.

Copy link

Quality Gate Failed Quality Gate failed

Failed conditions

27.5% Duplication on New Code (required ≤ 3%)
C Reliability Rating on New Code (required ≥ A)

See analysis details on SonarCloud

idea Catch issues before they fail your Quality Gate with our IDE extension SonarLint SonarLint

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants