diff --git a/src/store.ts b/src/store.ts index 3e5f9a293..735f7a977 100644 --- a/src/store.ts +++ b/src/store.ts @@ -184,14 +184,14 @@ export interface IStore { persistenceData?: IPersistenceMinified; - getConsentState?(mpid: MPID): ConsentState | null; - setConsentState?(mpid: MPID, consentState: ConsentState): void; - _getFromPersistence?(mpid: MPID, key: string): T; _setPersistence?(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; @@ -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; @@ -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, @@ -576,7 +548,7 @@ 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( mpid, 'con', toMinifiedJsonObject(consentState) @@ -584,15 +556,41 @@ export default function Store( } }; - 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(mpid, 'fst'); this.setFirstSeenTime = (mpid: MPID, _time?: number) => { @@ -602,7 +600,7 @@ export default function Store( const time = _time || new Date().getTime(); - this._setPersistence(mpid, 'fst', time); + this._setPersistence(mpid, 'fst', time); }; this.getLastSeenTime = (mpid: MPID): number => { @@ -618,40 +616,30 @@ export default function Store( return this._getFromPersistence(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(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(mpid, 'ua', userAttributes); + + this.getUserIdentities = (mpid: MPID): UserIdentities => + this._getFromPersistence(mpid, 'ui') || {}; + + this.setUserIdentities = (mpid: MPID, userIdentities: UserIdentities) => { + this._setPersistence(mpid, 'ui', userIdentities); + }; this.addMpidToSessionHistory = (mpid: MPID, previousMPID?: MPID): void => { const indexOfMPID = this.currentSessionMPIDs.indexOf(mpid); @@ -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