Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions src/batchUploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { SDKEvent, SDKEventCustomFlags, SDKLoggerApi } from './sdkRuntimeModels'
import { convertEvents } from './sdkToEventsApiConverter';
import { MessageType, EventType } from './types';
import { getRampNumber, isEmpty } from './utils';
import { SessionStorageVault, LocalStorageVault } from './vault';
import { createVault, BaseVault } from './vault';
import { VaultKind } from './constants';
import {
AsyncUploader,
FetchUploader,
Expand Down Expand Up @@ -41,8 +42,8 @@ export class BatchUploader {
mpInstance: IMParticleWebSDKInstance;
uploadUrl: string;
batchingEnabled: boolean;
private eventVault: SessionStorageVault<SDKEvent[]>;
private batchVault: LocalStorageVault<Batch[]>;
private eventVault: BaseVault<SDKEvent[]>;
private batchVault: BaseVault<Batch[]>;
private offlineStorageEnabled: boolean = false;
private uploader: AsyncUploader;
private lastASTEventTime: number = 0;
Expand Down Expand Up @@ -75,18 +76,16 @@ export class BatchUploader {
this.offlineStorageEnabled = this.isOfflineStorageAvailable();

if (this.offlineStorageEnabled) {
this.eventVault = new SessionStorageVault<SDKEvent[]>(
this.eventVault = createVault<SDKEvent[]>(
`${mpInstance._Store.storageName}-events`,
{
logger: mpInstance.Logger,
}
VaultKind.SessionStorage,
{ logger: mpInstance.Logger }
);

this.batchVault = new LocalStorageVault<Batch[]>(
this.batchVault = createVault<Batch[]>(
`${mpInstance._Store.storageName}-batches`,
{
logger: mpInstance.Logger,
}
VaultKind.LocalStorage,
{ logger: mpInstance.Logger }
);

// Load Events from Session Storage in case we have any in storage
Expand Down
5 changes: 5 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ const Constants = {

export default Constants;

export enum VaultKind {
LocalStorage = 'local',
SessionStorage = 'session',
}

// https://go.mparticle.com/work/SQDSDKS-6080
export const ONE_DAY_IN_SECONDS = 60 * 60 * 24;
export const MILLIS_IN_ONE_SEC = 1000;
Expand Down
7 changes: 4 additions & 3 deletions src/foregroundTimeTracker.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { isNumber } from './utils';
import { LocalStorageVault } from './vault';
import { createVault, BaseVault } from './vault';
import { VaultKind } from './constants';

export default class ForegroundTimeTracker {
private isTrackerActive: boolean = false;
private localStorageName: string = '';
private timerVault: LocalStorageVault<number>;
private timerVault: BaseVault<number>;
public startTime: number = 0;
public totalTime: number = 0;

constructor(timerKey: string) {
this.localStorageName = `mprtcl-tos-${timerKey}`;
this.timerVault = new LocalStorageVault<number>(this.localStorageName);
this.timerVault = createVault<number>(this.localStorageName, VaultKind.LocalStorage);
this.loadTimeFromStorage();
this.addHandlers();
if (document.hidden === false) {
Expand Down
6 changes: 3 additions & 3 deletions src/mp-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// jQuery v1.10.2 | (c) 2005, 2013 jQuery Foundation, Inc. | jquery.org/license

import { EventType, IdentityType, CommerceEventType, PromotionActionType, ProductActionType, MessageType } from './types';
import Constants from './constants';
import Constants, { VaultKind } from './constants';
import APIClient, { IAPIClient } from './apiClient';
import Helpers from './helpers';
import NativeSdkHelpers from './nativeSdkHelpers';
Expand All @@ -37,7 +37,7 @@ import KitBlocker from './kitBlocking';
import ConfigAPIClient, { IKitConfigs } from './configAPIClient';
import IdentityAPIClient from './identityApiClient';
import { isFunction, parseConfig } from './utils';
import { LocalStorageVault } from './vault';
import { createVault } from './vault';
import { removeExpiredIdentityCacheDates } from './identity-utils';
import IntegrationCapture from './integrationCapture';
import { IPreInit, processReadyQueue } from './pre-init-utils';
Expand Down Expand Up @@ -1523,7 +1523,7 @@ function createKitBlocker(config, mpInstance) {
}

function createIdentityCache(mpInstance) {
return new LocalStorageVault(`${mpInstance._Store.storageName}-id-cache`, {
return createVault(`${mpInstance._Store.storageName}-id-cache`, VaultKind.LocalStorage, {
logger: mpInstance.Logger,
});
}
Expand Down
15 changes: 15 additions & 0 deletions src/vault.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Logger } from '@mparticle/web-sdk';
import { isEmpty, isNumber } from './utils';
import { VaultKind } from './constants';

export interface IVaultOptions {
logger?: Logger;
Expand Down Expand Up @@ -102,4 +103,18 @@ export class SessionStorageVault<StorableItem> extends BaseVault<StorableItem> {
constructor(storageKey: string, options?: IVaultOptions) {
super(storageKey, window.sessionStorage, options);
}
}

export function createVault<StorableItem>(
storageKey: string,
kind: VaultKind,
options?: IVaultOptions
): BaseVault<StorableItem> {
switch (kind) {
case VaultKind.SessionStorage:
return new SessionStorageVault<StorableItem>(storageKey, options);
case VaultKind.LocalStorage:
default:
return new LocalStorageVault<StorableItem>(storageKey, options);
}
}
15 changes: 8 additions & 7 deletions test/src/tests-identity-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
ICachedIdentityCall,
hasIdentityRequestChanged,
} from "../../src/identity-utils";
import { LocalStorageVault } from "../../src/vault";
import { createVault } from "../../src/vault";
import { VaultKind } from "../../src/constants";
import { Dictionary, generateHash } from "../../src/utils";
import { expect } from 'chai';
import {
Expand All @@ -37,7 +38,7 @@ const knownIdentities: IKnownIdentities = createKnownIdentities({
DEVICE_ID
);

const cacheVault = new LocalStorageVault<Dictionary>(localStorageIDKey);
const cacheVault = createVault<Dictionary<ICachedIdentityCall>>(localStorageIDKey, VaultKind.LocalStorage);

const identifyResponse: IdentityResultBody = {
context: null,
Expand Down Expand Up @@ -292,7 +293,7 @@ describe('identity-utils', () => {
const mpIdCache = window.localStorage.getItem(localStorageIDKey);
expect(mpIdCache).to.equal(null);

const cacheVault = new LocalStorageVault<Dictionary>(localStorageIDKey);
const cacheVault = createVault<Dictionary<ICachedIdentityCall>>(localStorageIDKey, VaultKind.LocalStorage);

const result = hasValidCachedIdentity('identify', userIdentities, cacheVault);
expect(result).to.equal(false);
Expand All @@ -302,7 +303,7 @@ describe('identity-utils', () => {
const mpIdCache = window.localStorage.getItem(localStorageIDKey);
expect(mpIdCache).to.equal(null);

const cacheVault = new LocalStorageVault<Dictionary>(localStorageIDKey);
const cacheVault = createVault<Dictionary<ICachedIdentityCall>>(localStorageIDKey, VaultKind.LocalStorage);

// check to ensure there is nothing on the cache first
const result1 = hasValidCachedIdentity('identify', userIdentities, cacheVault);
Expand Down Expand Up @@ -394,7 +395,7 @@ describe('identity-utils', () => {
// set up clock in order to force some time stamps to expire later in the test
const clock = sinon.useFakeTimers();

const cacheVault = new LocalStorageVault<Dictionary>(localStorageIDKey);
const cacheVault = createVault<Dictionary<ICachedIdentityCall>>(localStorageIDKey, VaultKind.LocalStorage);
const knownIdentities1: IKnownIdentities = createKnownIdentities({
userIdentities: {customerid: 'id1'}},
DEVICE_ID
Expand Down Expand Up @@ -464,7 +465,7 @@ describe('identity-utils', () => {

const mpInstance = window.mParticle.getInstance();

const cacheVault = new LocalStorageVault<Dictionary>(localStorageIDKey);
const cacheVault = createVault<Dictionary<ICachedIdentityCall>>(localStorageIDKey, VaultKind.LocalStorage);

const customerId = {customerid: 'id1'}
const knownIdentities1: IKnownIdentities = createKnownIdentities({
Expand Down Expand Up @@ -512,7 +513,7 @@ describe('identity-utils', () => {
window.mParticle.init(apiKey, window.mParticle.config);

const mpInstance = window.mParticle.getInstance();
const cacheVault = new LocalStorageVault<Dictionary>(localStorageIDKey);
const cacheVault = createVault<Dictionary<ICachedIdentityCall>>(localStorageIDKey, VaultKind.LocalStorage);

const customerId = {customerid: 'id1'}
const knownIdentities1: IKnownIdentities = createKnownIdentities({
Expand Down
70 changes: 42 additions & 28 deletions test/src/vault.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Batch } from '@mparticle/event-models';
import { expect } from 'chai';
import { Dictionary } from '../../src/utils';
import { SessionStorageVault, LocalStorageVault } from '../../src/vault';
import { createVault, LocalStorageVault } from '../../src/vault';
import { VaultKind } from '../../src/constants';

const testObject: Dictionary<Dictionary<string>> = {
foo: { foo: 'bar', buzz: 'bazz' },
Expand All @@ -26,9 +27,10 @@ describe('Vault', () => {
it('should store an object', () => {
const storageKey = 'test-key-store-object';

const vault = new SessionStorageVault<
Dictionary<Dictionary<string>>
>(storageKey);
const vault = createVault<Dictionary<Dictionary<string>>>(
storageKey,
VaultKind.SessionStorage
);

vault.store(testObject);

Expand All @@ -41,8 +43,9 @@ describe('Vault', () => {
it('should store an array', () => {
const storageKey = 'test-key-store-array';

const vault = new SessionStorageVault<Dictionary<string>[]>(
const vault = createVault<Dictionary<string>[]>(
storageKey,
VaultKind.SessionStorage
);

vault.store(testArray);
Expand All @@ -56,7 +59,7 @@ describe('Vault', () => {
it('should store a string', () => {
const storageKey = 'test-key-store-string';

const vault = new SessionStorageVault<string>(storageKey);
const vault = createVault<string>(storageKey, VaultKind.SessionStorage);

vault.store(testString);

Expand All @@ -69,7 +72,7 @@ describe('Vault', () => {
it('should store a number', () => {
const storageKey = 'test-key-store-number';

const vault = new SessionStorageVault<number>(storageKey);
const vault = createVault<number>(storageKey, VaultKind.SessionStorage);

vault.store(testNumber);

Expand All @@ -89,9 +92,10 @@ describe('Vault', () => {
JSON.stringify(testObject),
);

const vault = new SessionStorageVault<
Dictionary<Dictionary<string>>
>(storageKey);
const vault = createVault<Dictionary<Dictionary<string>>>(
storageKey,
VaultKind.SessionStorage
);

const retrievedItem = vault.retrieve();

Expand All @@ -109,8 +113,9 @@ describe('Vault', () => {
JSON.stringify(testArray),
);

const vault = new SessionStorageVault<Dictionary<string>[]>(
const vault = createVault<Dictionary<string>[]>(
storageKey,
VaultKind.SessionStorage
);

const retrievedItem = vault.retrieve();
Expand All @@ -126,7 +131,7 @@ describe('Vault', () => {
JSON.stringify(testString),
);

const vault = new SessionStorageVault<string>(storageKey);
const vault = createVault<string>(storageKey, VaultKind.SessionStorage);

const retrievedItem = vault.retrieve();

Expand All @@ -141,7 +146,7 @@ describe('Vault', () => {
JSON.stringify(testNumber),
);

const vault = new SessionStorageVault<number>(storageKey);
const vault = createVault<number>(storageKey, VaultKind.SessionStorage);

const retrievedItem = vault.retrieve();

Expand All @@ -158,9 +163,10 @@ describe('Vault', () => {
JSON.stringify(testObject),
);

const vault = new SessionStorageVault<
Dictionary<Dictionary<string>>
>(storageKey);
const vault = createVault<Dictionary<Dictionary<string>>>(
storageKey,
VaultKind.SessionStorage
);

vault.purge();

Expand All @@ -178,8 +184,9 @@ describe('Vault', () => {
JSON.stringify(testArray),
);

const vault = new SessionStorageVault<Dictionary<string>[]>(
const vault = createVault<Dictionary<string>[]>(
storageKey,
VaultKind.SessionStorage
);

vault.purge();
Expand All @@ -201,9 +208,10 @@ describe('Vault', () => {
it('should store an object', () => {
const storageKey = 'test-key-store-object';

const vault = new LocalStorageVault<
Dictionary<Dictionary<string>>
>(storageKey);
const vault = createVault<Dictionary<Dictionary<string>>>(
storageKey,
VaultKind.LocalStorage
);

vault.store(testObject);

Expand All @@ -216,8 +224,9 @@ describe('Vault', () => {
it('should store an array', () => {
const storageKey = 'test-key-store-array';

const vault = new LocalStorageVault<Dictionary<string>[]>(
const vault = createVault<Dictionary<string>[]>(
storageKey,
VaultKind.LocalStorage
);

vault.store(testArray);
Expand All @@ -231,7 +240,7 @@ describe('Vault', () => {
it('should store a string', () => {
const storageKey = 'test-key-store-array';

const vault = new LocalStorageVault<string>(storageKey);
const vault = createVault<string>(storageKey, VaultKind.LocalStorage);

vault.store(testString);

Expand All @@ -244,7 +253,7 @@ describe('Vault', () => {
it('should store a number', () => {
const storageKey = 'test-key-store-array';

const vault = new LocalStorageVault<number>(storageKey);
const vault = createVault<number>(storageKey, VaultKind.LocalStorage);

vault.store(testNumber);

Expand All @@ -264,9 +273,10 @@ describe('Vault', () => {
JSON.stringify(testObject),
);

const vault = new LocalStorageVault<
Dictionary<Dictionary<string>>
>(storageKey);
const vault = createVault<Dictionary<Dictionary<string>>>(
storageKey,
VaultKind.LocalStorage
);

const retrievedItem = vault.retrieve();

Expand All @@ -284,8 +294,9 @@ describe('Vault', () => {
JSON.stringify(testArray),
);

const vault = new LocalStorageVault<Dictionary<string>[]>(
const vault = createVault<Dictionary<string>[]>(
storageKey,
VaultKind.LocalStorage
);

const retrievedItem = vault.retrieve();
Expand Down Expand Up @@ -398,7 +409,10 @@ describe('Vault', () => {
source_request_id: 'source-request-id-5',
};

const vault = new LocalStorageVault<Partial<Batch>[]>(storageKey);
const vault = createVault<Partial<Batch>[]>(
storageKey,
VaultKind.LocalStorage
);

vault.store([batch1, batch2, batch3, batch4, batch5]);

Expand Down
Loading