Skip to content

Commit

Permalink
fix: Migrate deviceId methods to Store
Browse files Browse the repository at this point in the history
  • Loading branch information
alexs-mparticle committed Apr 2, 2024
1 parent c6b462d commit 91bff4d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/mp-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
39 changes: 38 additions & 1 deletion src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -527,4 +564,4 @@ function processDirectBaseUrls(
}

return directBaseUrls;
}
}
40 changes: 40 additions & 0 deletions test/src/tests-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 91bff4d

Please sign in to comment.