Skip to content

Commit

Permalink
refactor: Define Type Definitions for Persistence (#400)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexs-mparticle authored and rmi22186 committed Mar 14, 2023
1 parent c827826 commit 9b79a22
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 3 deletions.
138 changes: 138 additions & 0 deletions src/persistence.interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import { Context } from '@mparticle/event-models';
import {
AllUserAttributes,
ConsentState,
IdentityApiData,
MPID,
Product,
UserIdentities,
} from '@mparticle/web-sdk';
import { ForwardingStatsData } from './apiClient';
import {
IntegrationAttributes,
ServerSettings,
SessionAttributes,
} from './store';
import { Dictionary } from './utils';

export type CookieSyncDate = Dictionary<string>;
export type UploadsTable = Dictionary<any>;
export interface iForwardingStatsBatches {
uploadsTable: UploadsTable;
forwardingStatsEventQueue: ForwardingStatsData[];
}

// TODO: Migrate this to @types/mparticle__web-sdk
// https://go.mparticle.com/work/SQDSDKS-5196
export type UserAttributes = AllUserAttributes;

export interface IGlobalStoreV2MinifiedKeys {
sid: string; // Session ID
ie: boolean; // Is Enabled
sa: SessionAttributes;
ss: ServerSettings;
dt: string; // Dev Token
av: string; // App Version
cgid: string; // Client Generated ID
das: string; // Device ID/ Device Application String
ia: IntegrationAttributes;
c: Context;
csm: MPID[]; // Current Session MPIDs
les: number; // Last Event Sent Timestamp
ssd: number; // Session Start Date
}

export interface IPersistenceMinified extends Dictionary {
cu: MPID; // Current User MPID
gs: IGlobalStoreV2MinifiedKeys;
l: false; // IsLoggedIn

// Persistence Minified can also store optional dictionaries with
// an idex of MPID
// [mpid: MPID]: Dictionary<any>;

// For Example:
// {
// cu: '123345',
// gs: {
// sid: '123',
// ie: false,
// sa: {},
// ss: {},
// dt: '',
// av: '',
// cgid: '123',
// das: 'das',
// ia: {},
// c: null,
// csm: ['123'],
// les: 0,
// ssd: 0,
// },
// l: false,
// MPID1: {
// csd: [],
// ui: {
// customerid: '12346',
// },
// },
// };
}

export interface IPersistence {
useLocalStorage(): boolean;
initializeStorage(): void;
update(): void;
storeProductsInMemory(products: Product[], mpid: MPID): void;
storeDataInMemory(obj: IPersistenceMinified, currentMPID: MPID): void;
determineLocalStorageAvailability(storage: Storage): boolean;
getUserProductsFromLS(mpid: MPID): Product[];
getAllUserProductsFromLS(): Product[];
setLocalStorage(): void;
getLocalStorage(): IPersistenceMinified | null;
expireCookies(cookieName: string): void;
getCookie(): IPersistenceMinified | null;
setCookie(): void;
reduceAndEncodePersistence(
persistence: IPersistenceMinified,
expires: string,
domain: string,
maxCookieSize: number
): void;
findPrevCookiesBasedOnUI(identityApiData: IdentityApiData): void;
encodePersistence(persistance: IPersistenceMinified): string;
decodePersistence(persistance: IPersistenceMinified): string;
replaceCommasWithPipes(string: string): string;
replacePipesWithCommas(string: string): string;
replaceApostrophesWithQuotes(string: string): string;
replaceQuotesWithApostrophes(string: string): string;
createCookieString(string: string): string;
revertCookieString(string: string): string;
getCookieDomain(): string;
getDomain(doc: string, locationHostname: string): string;
getUserIdentities(mpid: MPID): UserIdentities;
getAllUserAttributes(mpid: MPID): AllUserAttributes;
getCartProducts(mpid: MPID): Product[];
setCartProducts(allProducts: Product[]): void;
saveUserIdentitiesToPersistence(
mpid: MPID,
userIdentities: UserIdentities
): void;
saveUserAttributesToPersistence(
mpid: MPID,
userAttributes: UserAttributes
): void;
saveUserCookieSyncDatesToPersistence(mpid: MPID, csd: CookieSyncDate): void;
saveUserConsentStateToCookies(mpid, consentState: ConsentState): void;
savePersistence(persistance: IPersistenceMinified): void;
getPersistence(): IPersistenceMinified;
getConsentState(mpid: MPID): ConsentState | null;
getFirstSeenTime(mpid: MPID): string | null;
setFirstSeenTime(mpid: MPID, time: number): void;
getLastSeenTime(mpid: MPID): number | null;
setLastSeenTime(mpid: MPID, time: number): void;
getDeviceId(): string;
setDeviceId(guid: string): void;
resetPersistence(): void;
forwardingStatsBatches: iForwardingStatsBatches;
}
3 changes: 3 additions & 0 deletions src/persistence.js
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,9 @@ export default function _Persistence(mpInstance) {
if (userAttributes) {
if (persistence) {
if (persistence[mpid]) {
// TODO: Investigate why setting this to UI still shows up as UA
// when running `mParticle.getInstance()._Persistence.getLocalStorage()`
// https://go.mparticle.com/work/SQDSDKS-5195
persistence[mpid].ui = userAttributes;
} else {
persistence[mpid] = {
Expand Down
3 changes: 2 additions & 1 deletion src/sdkRuntimeModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Dictionary } from './utils';
import { IServerModel } from './serverModel';
import { IKitConfigs } from './configAPIClient';
import { SDKConsentApi, SDKConsentState } from './consent';
import { IPersistence } from './persistence.interfaces';

// TODO: Resolve this with version in @mparticle/web-sdk
export type SDKEventCustomFlags = Dictionary<any>;
Expand Down Expand Up @@ -139,7 +140,7 @@ export interface MParticleWebSDK {
_Consent: SDKConsentApi;
Consent: SDKConsentApi;
_NativeSdkHelpers: any; // TODO: Set up API
_Persistence: any; // TODO: Set up Persistence API
_Persistence: IPersistence;
_preInit: any; // TODO: Set up API
_resetForTests(MPConfig?: SDKInitConfig): void;
init(apiKey: string, config: SDKInitConfig, instanceName?: string): void;
Expand Down
6 changes: 4 additions & 2 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ function createSDKConfig(config: SDKInitConfig): SDKConfig {
export type PixelConfiguration = Dictionary;
export type MigrationData = Dictionary;
export type ServerSettings = Dictionary;
export type SessionAttributes = Dictionary;
export type IntegrationAttributes = Dictionary<Dictionary<string>>;

type WrapperSDKTypes = 'flutter' | 'none';
interface WrapperSDKInfo {
Expand All @@ -112,7 +114,7 @@ interface WrapperSDKInfo {
// Temporary Interface until Store can be refactored as a class
export interface IStore {
isEnabled: boolean;
sessionAttributes: Dictionary;
sessionAttributes: SessionAttributes;
currentSessionMPIDs: MPID[];
consentState: SDKConsentState | null;
sessionId: string | null;
Expand Down Expand Up @@ -140,7 +142,7 @@ export interface IStore {
identifyCalled: boolean;
isLoggedIn: boolean;
cookieSyncDates: Dictionary<number>;
integrationAttributes: Dictionary<Dictionary<string>>;
integrationAttributes: IntegrationAttributes;
requireDelay: boolean;
isLocalStorageAvailable: boolean | null;
storageName: string | null;
Expand Down

0 comments on commit 9b79a22

Please sign in to comment.