diff --git a/src/mp-instance.js b/src/mp-instance.js index eed6fa9e..fd678a65 100644 --- a/src/mp-instance.js +++ b/src/mp-instance.js @@ -86,6 +86,9 @@ export default function mParticleInstance(instanceName) { this._Identity = new Identity(this); this.Identity = this._Identity.IdentityAPI; this.generateHash = this._Helpers.generateHash; + + // https://go.mparticle.com/work/SQDSDKS-6289 + // TODO: Replace this with Store once Store is moved earlier in the init process this.getDeviceId = this._Persistence.getDeviceId; if (typeof window !== 'undefined') { diff --git a/src/store.ts b/src/store.ts index 892a8d6e..fbe936e2 100644 --- a/src/store.ts +++ b/src/store.ts @@ -162,6 +162,14 @@ export interface IStore { integrationDelayTimeoutStart: number; // UNIX Timestamp webviewBridgeEnabled?: boolean; wrapperSDKInfo: WrapperSDKInfo; + + persistenceData?: IPersistenceMinified; + + getDeviceId?(): string; + setDeviceId?(deviceId: string): void; + + getAllUserAttributes?(mpid: MPID): Dictionary; + setUserAttributes?(mpid: MPID, userAttributes: Dictionary): void; } // TODO: Merge this with SDKStoreApi in sdkRuntimeModels @@ -214,6 +222,27 @@ export default function Store( version: null, isInfoSet: false, }, + + // Placeholder for in-memory persistence model + persistenceData: { + cu: null, + gs: { + sid: null, + ie: null, + sa: null, + ss: null, + dt: null, + av: null, + cgid: null, + das: null, + ia: null, + c: null, + csm: null, + les: null, + ssd: null, + }, + l: null, + }, }; for (var key in defaultStore) { @@ -418,6 +447,14 @@ export default function Store( } } } + + this.getDeviceId = () => this.deviceId; + this.setDeviceId = (deviceId: string) => { + this.deviceId = deviceId; + this.persistenceData.gs.das = deviceId; + + mpInstance._Persistence.savePersistence(this.persistenceData); + }; } export function processFlags( @@ -527,4 +564,4 @@ function processDirectBaseUrls( } return directBaseUrls; -} \ No newline at end of file +} diff --git a/test/src/tests-store.ts b/test/src/tests-store.ts index 584fb538..4f0ad4ec 100644 --- a/test/src/tests-store.ts +++ b/test/src/tests-store.ts @@ -248,6 +248,46 @@ describe('Store', () => { }); }); + describe('#getDeviceId', () => { + it('should return the deviceId from the store', () => { + const store: IStore = new Store( + sampleConfig, + window.mParticle.getInstance() + ); + + store.deviceId = 'foo'; + + expect(store.getDeviceId()).to.equal('foo'); + }); + }); + + describe('#setDeviceId', () => { + it('should set the deviceId in the store', () => { + const store: IStore = new Store( + sampleConfig, + window.mParticle.getInstance() + ); + + store.setDeviceId('foo'); + expect(store.deviceId).to.equal('foo'); + }); + + it('should set the deviceId in persistence', () => { + // Since this relies on persistence, we need to make sure + // we are using an mParticle instance that shares both + // store and persistence modules + const store = window.mParticle.getInstance()._Store; + + store.setDeviceId('foo'); + const fromPersistence = window.mParticle + .getInstance() + ._Persistence.getPersistence(); + + expect(store.deviceId).to.equal('foo'); + expect(fromPersistence.gs.das).to.equal('foo'); + }); + }); + describe('#processFlags', () => { it('should return an empty object if no featureFlags are passed', () => { const flags = processFlags({} as SDKInitConfig, {} as SDKConfig);