Skip to content

Commit 55cc437

Browse files
refactor: Reorder Store Persistence Methods
1 parent 68f9d23 commit 55cc437

File tree

1 file changed

+55
-57
lines changed

1 file changed

+55
-57
lines changed

src/store.ts

Lines changed: 55 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,14 @@ export interface IStore {
184184

185185
persistenceData?: IPersistenceMinified;
186186

187-
getConsentState?(mpid: MPID): ConsentState | null;
188-
setConsentState?(mpid: MPID, consentState: ConsentState): void;
189-
190187
_getFromPersistence?<T>(mpid: MPID, key: string): T;
191188
_setPersistence?<T>(mpid: MPID, key: string, value: T): void;
192189

190+
getConsentState?(mpid: MPID): ConsentState | null;
191+
setConsentState?(mpid: MPID, consentState: ConsentState): void;
193192
getDeviceId?(): string;
194193
setDeviceId?(deviceId: string): void;
194+
getGlobalStorageAttributes?(): IGlobalStoreV2MinifiedKeys;
195195
getFirstSeenTime?(mpid: MPID): number;
196196
setFirstSeenTime?(mpid: MPID, time?: number): void;
197197
getLastSeenTime?(mpid: MPID): number;
@@ -202,7 +202,6 @@ export interface IStore {
202202
setUserIdentities?(mpid: MPID, userIdentities: UserIdentities): void;
203203

204204
addMpidToSessionHistory?(mpid: MPID, previousMpid?: MPID): void;
205-
getGlobalStorageAttributes?(): IGlobalStoreV2MinifiedKeys;
206205
hasInvalidIdentifyRequest?: () => boolean;
207206
nullifySession?: () => void;
208207
processConfig(config: SDKInitConfig): void;
@@ -526,33 +525,6 @@ export default function Store(
526525
}
527526
};
528527

529-
this.getGlobalStorageAttributes = () => ({
530-
sid: this.sessionId,
531-
ie: this.isEnabled,
532-
sa: this.sessionAttributes,
533-
ss: this.serverSettings,
534-
dt: this.devToken,
535-
les: this.dateLastEventSent ? this.dateLastEventSent.getTime() : null,
536-
av: this.SDKConfig.appVersion,
537-
cgid: this.clientId,
538-
das: this.deviceId,
539-
c: this.context,
540-
ssd: this.sessionStartDate ? this.sessionStartDate.getTime() : 0,
541-
ia: this.integrationAttributes,
542-
543-
csm: this.sessionId ? this.currentSessionMPIDs : undefined,
544-
});
545-
546-
this.hasInvalidIdentifyRequest = (): boolean => {
547-
const { identifyRequest } = this.SDKConfig;
548-
return (
549-
(isObject(identifyRequest) &&
550-
isObject(identifyRequest.userIdentities) &&
551-
isEmpty(identifyRequest.userIdentities)) ||
552-
!identifyRequest
553-
);
554-
};
555-
556528
this.getConsentState = (mpid: MPID): ConsentState => {
557529
const {
558530
fromMinifiedJsonObject,
@@ -576,23 +548,49 @@ export default function Store(
576548

577549
// If ConsentState is null, we assume the intent is to clear out the consent state
578550
if (consentState || consentState === null) {
579-
this._setPersistence(
551+
this._setPersistence<IMinifiedConsentJSONObject>(
580552
mpid,
581553
'con',
582554
toMinifiedJsonObject(consentState)
583555
);
584556
}
585557
};
586558

587-
this.getDeviceId = () => this.deviceId;
588-
this.setDeviceId = (deviceId: string) => {
559+
this.getGlobalStorageAttributes = (): IGlobalStoreV2MinifiedKeys => ({
560+
sid: this.sessionId,
561+
ie: this.isEnabled,
562+
sa: this.sessionAttributes,
563+
ss: this.serverSettings,
564+
dt: this.devToken,
565+
les: this.dateLastEventSent ? this.dateLastEventSent.getTime() : null,
566+
av: this.SDKConfig.appVersion,
567+
cgid: this.clientId,
568+
das: this.deviceId,
569+
c: this.context,
570+
ssd: this.sessionStartDate ? this.sessionStartDate.getTime() : 0,
571+
ia: this.integrationAttributes,
572+
573+
csm: this.sessionId ? this.currentSessionMPIDs : undefined,
574+
});
575+
576+
this.hasInvalidIdentifyRequest = (): boolean => {
577+
const { identifyRequest } = this.SDKConfig;
578+
return (
579+
(isObject(identifyRequest) &&
580+
isObject(identifyRequest.userIdentities) &&
581+
isEmpty(identifyRequest.userIdentities)) ||
582+
!identifyRequest
583+
);
584+
};
585+
586+
this.getDeviceId = (): string => this.deviceId;
587+
this.setDeviceId = (deviceId: string): void => {
589588
this.deviceId = deviceId;
590589
this.persistenceData.gs.das = deviceId;
591590
mpInstance._Persistence.update();
592591
};
593592

594-
595-
this.getFirstSeenTime = (mpid: MPID) =>
593+
this.getFirstSeenTime = (mpid: MPID): number =>
596594
this._getFromPersistence<number>(mpid, 'fst');
597595

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

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

605-
this._setPersistence(mpid, 'fst', time);
603+
this._setPersistence<number>(mpid, 'fst', time);
606604
};
607605

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

621-
this.setLastSeenTime = (mpid: MPID, _time?: number) => {
619+
this.setLastSeenTime = (mpid: MPID, _time?: number): void => {
622620
if (!mpid) {
623621
return;
624622
}
625623

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

628-
this._setPersistence(mpid, 'lst', time);
629-
};
630-
631-
this.syncPersistenceData = () => {
632-
const persistenceData = mpInstance._Persistence.getPersistence();
633-
634-
this.persistenceData = mpInstance._Helpers.extend(
635-
{},
636-
this.persistenceData,
637-
persistenceData,
638-
);
626+
this._setPersistence<number>(mpid, 'lst', time);
639627
};
640628

641-
this.getUserIdentities = (mpid: MPID): UserIdentities =>
642-
this._getFromPersistence(mpid, 'ui') || {};
643-
644-
this.setUserIdentities = (mpid: MPID, userIdentities: UserIdentities) => {
645-
this._setPersistence(mpid, 'ui', userIdentities);
646-
}
647-
648629
this.getUserAttributes = (mpid: MPID): UserAttributes =>
649630
this._getFromPersistence(mpid, 'ua') || {};
650631

651632
this.setUserAttributes = (
652633
mpid: MPID,
653634
userAttributes: UserAttributes
654-
): void => this._setPersistence(mpid, 'ua', userAttributes);
635+
): void => this._setPersistence<UserAttributes>(mpid, 'ua', userAttributes);
636+
637+
this.getUserIdentities = (mpid: MPID): UserIdentities =>
638+
this._getFromPersistence<UserIdentities>(mpid, 'ui') || {};
639+
640+
this.setUserIdentities = (mpid: MPID, userIdentities: UserIdentities) => {
641+
this._setPersistence<UserIdentities>(mpid, 'ui', userIdentities);
642+
};
655643

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

706694
this.configurationLoaded = true;
707695
};
696+
697+
this.syncPersistenceData = () => {
698+
const persistenceData = mpInstance._Persistence.getPersistence();
699+
700+
this.persistenceData = mpInstance._Helpers.extend(
701+
{},
702+
this.persistenceData,
703+
persistenceData
704+
);
705+
};
708706
}
709707

710708
// https://go.mparticle.com/work/SQDSDKS-6317

0 commit comments

Comments
 (0)