Skip to content

Commit

Permalink
refactor: Reorder Store Persistence Methods
Browse files Browse the repository at this point in the history
  • Loading branch information
alexs-mparticle committed Apr 25, 2024
1 parent 68f9d23 commit 55cc437
Showing 1 changed file with 55 additions and 57 deletions.
112 changes: 55 additions & 57 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,14 @@ export interface IStore {

persistenceData?: IPersistenceMinified;

getConsentState?(mpid: MPID): ConsentState | null;
setConsentState?(mpid: MPID, consentState: ConsentState): void;

_getFromPersistence?<T>(mpid: MPID, key: string): T;
_setPersistence?<T>(mpid: MPID, key: string, value: T): void;

getConsentState?(mpid: MPID): ConsentState | null;
setConsentState?(mpid: MPID, consentState: ConsentState): void;
getDeviceId?(): string;
setDeviceId?(deviceId: string): void;
getGlobalStorageAttributes?(): IGlobalStoreV2MinifiedKeys;
getFirstSeenTime?(mpid: MPID): number;
setFirstSeenTime?(mpid: MPID, time?: number): void;
getLastSeenTime?(mpid: MPID): number;
Expand All @@ -202,7 +202,6 @@ export interface IStore {
setUserIdentities?(mpid: MPID, userIdentities: UserIdentities): void;

addMpidToSessionHistory?(mpid: MPID, previousMpid?: MPID): void;
getGlobalStorageAttributes?(): IGlobalStoreV2MinifiedKeys;
hasInvalidIdentifyRequest?: () => boolean;
nullifySession?: () => void;
processConfig(config: SDKInitConfig): void;
Expand Down Expand Up @@ -526,33 +525,6 @@ export default function Store(
}
};

this.getGlobalStorageAttributes = () => ({
sid: this.sessionId,
ie: this.isEnabled,
sa: this.sessionAttributes,
ss: this.serverSettings,
dt: this.devToken,
les: this.dateLastEventSent ? this.dateLastEventSent.getTime() : null,
av: this.SDKConfig.appVersion,
cgid: this.clientId,
das: this.deviceId,
c: this.context,
ssd: this.sessionStartDate ? this.sessionStartDate.getTime() : 0,
ia: this.integrationAttributes,

csm: this.sessionId ? this.currentSessionMPIDs : undefined,
});

this.hasInvalidIdentifyRequest = (): boolean => {
const { identifyRequest } = this.SDKConfig;
return (
(isObject(identifyRequest) &&
isObject(identifyRequest.userIdentities) &&
isEmpty(identifyRequest.userIdentities)) ||
!identifyRequest
);
};

this.getConsentState = (mpid: MPID): ConsentState => {
const {
fromMinifiedJsonObject,
Expand All @@ -576,23 +548,49 @@ export default function Store(

// If ConsentState is null, we assume the intent is to clear out the consent state
if (consentState || consentState === null) {
this._setPersistence(
this._setPersistence<IMinifiedConsentJSONObject>(
mpid,
'con',
toMinifiedJsonObject(consentState)
);
}
};

this.getDeviceId = () => this.deviceId;
this.setDeviceId = (deviceId: string) => {
this.getGlobalStorageAttributes = (): IGlobalStoreV2MinifiedKeys => ({
sid: this.sessionId,
ie: this.isEnabled,
sa: this.sessionAttributes,
ss: this.serverSettings,
dt: this.devToken,
les: this.dateLastEventSent ? this.dateLastEventSent.getTime() : null,
av: this.SDKConfig.appVersion,
cgid: this.clientId,
das: this.deviceId,
c: this.context,
ssd: this.sessionStartDate ? this.sessionStartDate.getTime() : 0,
ia: this.integrationAttributes,

csm: this.sessionId ? this.currentSessionMPIDs : undefined,
});

this.hasInvalidIdentifyRequest = (): boolean => {
const { identifyRequest } = this.SDKConfig;
return (
(isObject(identifyRequest) &&
isObject(identifyRequest.userIdentities) &&
isEmpty(identifyRequest.userIdentities)) ||
!identifyRequest
);
};

this.getDeviceId = (): string => this.deviceId;
this.setDeviceId = (deviceId: string): void => {
this.deviceId = deviceId;
this.persistenceData.gs.das = deviceId;
mpInstance._Persistence.update();
};


this.getFirstSeenTime = (mpid: MPID) =>
this.getFirstSeenTime = (mpid: MPID): number =>
this._getFromPersistence<number>(mpid, 'fst');

this.setFirstSeenTime = (mpid: MPID, _time?: number) => {
Expand All @@ -602,7 +600,7 @@ export default function Store(

const time = _time || new Date().getTime();

this._setPersistence(mpid, 'fst', time);
this._setPersistence<number>(mpid, 'fst', time);
};

this.getLastSeenTime = (mpid: MPID): number => {
Expand All @@ -618,40 +616,30 @@ export default function Store(
return this._getFromPersistence<number>(mpid, 'lst');
};

this.setLastSeenTime = (mpid: MPID, _time?: number) => {
this.setLastSeenTime = (mpid: MPID, _time?: number): void => {
if (!mpid) {
return;
}

const time = _time || new Date().getTime();

this._setPersistence(mpid, 'lst', time);
};

this.syncPersistenceData = () => {
const persistenceData = mpInstance._Persistence.getPersistence();

this.persistenceData = mpInstance._Helpers.extend(
{},
this.persistenceData,
persistenceData,
);
this._setPersistence<number>(mpid, 'lst', time);
};

this.getUserIdentities = (mpid: MPID): UserIdentities =>
this._getFromPersistence(mpid, 'ui') || {};

this.setUserIdentities = (mpid: MPID, userIdentities: UserIdentities) => {
this._setPersistence(mpid, 'ui', userIdentities);
}

this.getUserAttributes = (mpid: MPID): UserAttributes =>
this._getFromPersistence(mpid, 'ua') || {};

this.setUserAttributes = (
mpid: MPID,
userAttributes: UserAttributes
): void => this._setPersistence(mpid, 'ua', userAttributes);
): void => this._setPersistence<UserAttributes>(mpid, 'ua', userAttributes);

this.getUserIdentities = (mpid: MPID): UserIdentities =>
this._getFromPersistence<UserIdentities>(mpid, 'ui') || {};

this.setUserIdentities = (mpid: MPID, userIdentities: UserIdentities) => {
this._setPersistence<UserIdentities>(mpid, 'ui', userIdentities);
};

this.addMpidToSessionHistory = (mpid: MPID, previousMPID?: MPID): void => {
const indexOfMPID = this.currentSessionMPIDs.indexOf(mpid);
Expand Down Expand Up @@ -705,6 +693,16 @@ export default function Store(

this.configurationLoaded = true;
};

this.syncPersistenceData = () => {
const persistenceData = mpInstance._Persistence.getPersistence();

this.persistenceData = mpInstance._Helpers.extend(
{},
this.persistenceData,
persistenceData
);
};
}

// https://go.mparticle.com/work/SQDSDKS-6317
Expand Down

0 comments on commit 55cc437

Please sign in to comment.