Skip to content

Commit

Permalink
refactor: Migrate Types to TypeScript as enums and utils (#950)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexs-mparticle authored and rmi22186 committed Dec 18, 2024
1 parent 5c0d48e commit fc5dedc
Show file tree
Hide file tree
Showing 23 changed files with 1,110 additions and 585 deletions.
5 changes: 4 additions & 1 deletion src/apiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ export default function APIClient(
this.queueEventForBatchUpload(event);
}

if (event.EventName !== Types.MessageType.AppStateTransition) {
// https://go.mparticle.com/work/SQDSDKS-6935
// While Event Name is 'usually' a string, there are some cases where it is a number
// in that it could be a type of MessageType Enum
if (event.EventName as unknown as number !== Types.MessageType.AppStateTransition) {
if (kitBlocker && kitBlocker.kitBlockingEnabled) {
event = kitBlocker.createBlockedEvent(event);
}
Expand Down
46 changes: 26 additions & 20 deletions src/batchUploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Batch } from '@mparticle/event-models';
import Constants from './constants';
import { SDKEvent, MParticleWebSDK, SDKLoggerApi } from './sdkRuntimeModels';
import { convertEvents } from './sdkToEventsApiConverter';
import Types from './types';
import { MessageType } from './types';
import { getRampNumber, isEmpty } from './utils';
import { SessionStorageVault, LocalStorageVault } from './vault';
import {
Expand Down Expand Up @@ -167,29 +167,35 @@ export class BatchUploader {
* @param event event that should be queued
*/
public queueEvent(event: SDKEvent): void {
if (!isEmpty(event)) {
this.eventsQueuedForProcessing.push(event);
if (this.offlineStorageEnabled && this.eventVault) {
this.eventVault.store(this.eventsQueuedForProcessing);
}
this.mpInstance.Logger.verbose(
`Queuing event: ${JSON.stringify(event)}`
);
this.mpInstance.Logger.verbose(
`Queued event count: ${this.eventsQueuedForProcessing.length}`
);
if (isEmpty(event)) {
return;
}

// TODO: Remove this check once the v2 code path is removed
// https://go.mparticle.com/work/SQDSDKS-3720
if (
!this.batchingEnabled ||
Types.TriggerUploadType[event.EventDataType]
) {
this.prepareAndUpload(false, false);
}
const { verbose } = this.mpInstance.Logger;

this.eventsQueuedForProcessing.push(event);
if (this.offlineStorageEnabled && this.eventVault) {
this.eventVault.store(this.eventsQueuedForProcessing);
}

verbose(`Queuing event: ${JSON.stringify(event)}`);
verbose(`Queued event count: ${this.eventsQueuedForProcessing.length}`);

if (this.shouldTriggerImmediateUpload(event.EventDataType)) {
this.prepareAndUpload(false, false);
}
}

// https://go.mparticle.com/work/SQDSDKS-3720
private shouldTriggerImmediateUpload (eventDataType: number): boolean {
const priorityEvents = [
MessageType.Commerce,
MessageType.UserIdentityChange,
] as const;

return !this.batchingEnabled || priorityEvents.includes(eventDataType as typeof priorityEvents[number]);
};

/**
* This implements crucial logic to:
* - bucket pending events by MPID, and then by Session, and upload individual batches for each bucket.
Expand Down
5 changes: 5 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ const Constants = {
Login: 'login',
Identify: 'identify',
},

Environment: {
Development: 'development',
Production: 'production',
},
} as const;

export default Constants;
Expand Down
3 changes: 0 additions & 3 deletions src/forwarders.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ export interface ConfiguredKit
setOptOut(isOptingOut: boolean): string | KitMappedMethodFailure;
removeUserAttribute(key: string): string;
setUserAttribute(key: string, value: string): string;

// TODO: Convert type to enum during Identity migration
// https://go.mparticle.com/work/SQDSDKS-5218
setUserIdentity(id: UserIdentityId, type: UserIdentityType): void;

// TODO: https://go.mparticle.com/work/SQDSDKS-5156
Expand Down
6 changes: 3 additions & 3 deletions src/identity-user-interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AllUserAttributes, MPID, Product, User } from '@mparticle/web-sdk';
import { SDKIdentityTypeEnum } from './identity.interfaces';
import { MessageType } from './types.interfaces';
import { MessageType } from './types';
import { BaseEvent } from './sdkRuntimeModels';
import Constants from './constants';
const { HTTPCodes } = Constants;
Expand Down Expand Up @@ -58,7 +58,7 @@ export interface ISDKUserIdentityChangeData {
}

export interface IUserIdentityChangeEvent extends BaseEvent {
messageType: MessageType.UserIdentityChange;
messageType: typeof MessageType.UserIdentityChange;
userIdentityChanges: ISDKUserIdentityChanges;
}

Expand All @@ -75,7 +75,7 @@ export interface ISDKUserAttributeChangeData {
}

export interface IUserAttributeChangeEvent extends BaseEvent {
messageType: MessageType.UserAttributeChange;
messageType: typeof MessageType.UserAttributeChange;
userAttributeChanges: ISDKUserAttributeChangeData;
}

Expand Down
5 changes: 2 additions & 3 deletions src/identity.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Constants, { HTTP_OK } from './constants';
import Types from './types';
import Types, { IdentityType } from './types';
import {
cacheOrClearIdCache,
createKnownIdentities,
Expand All @@ -17,7 +17,6 @@ import {
isObject,
} from './utils';
import { hasMPIDAndUserLoginChanged, hasMPIDChanged } from './user-utils';
import { getNewIdentitiesByName } from './type-utils';
import { processReadyQueue } from './pre-init-utils';

export default function Identity(mpInstance) {
Expand Down Expand Up @@ -1623,7 +1622,7 @@ export default function Identity(mpInstance) {
self.setForwarderCallbacks(newUser, method);
}

const newIdentitiesByName = getNewIdentitiesByName(
const newIdentitiesByName = IdentityType.getNewIdentitiesByName(
newIdentitiesByType
);

Expand Down
13 changes: 6 additions & 7 deletions src/kitFilterHelper.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { generateHash } from "./utils";
import { generateHash, valueof } from "./utils";
// TODO: https://mparticle-eng.atlassian.net/browse/SQDSDKS-5381
import { EventTypeEnum, IdentityType } from "./types.interfaces";
import Constants from './constants';
import { EventType, IdentityType } from "./types";

export default class KitFilterHelper {
static hashEventType(eventType: EventTypeEnum): number {
static hashEventType(eventType: valueof<typeof EventType>): number {
return generateHash(eventType as unknown as string);
};

static hashEventName(eventName: string, eventType: EventTypeEnum): number {
static hashEventName(eventName: string, eventType: valueof<typeof EventType>): number {
return generateHash(eventType + eventName);
};

static hashEventAttributeKey(eventType: EventTypeEnum, eventName: string, customAttributeName: string): number {
static hashEventAttributeKey(eventType: valueof<typeof EventType>, eventName: string, customAttributeName: string): number {
return generateHash(eventType + eventName + customAttributeName);
}

Expand All @@ -22,7 +21,7 @@ export default class KitFilterHelper {

// User Identities are not actually hashed, this method is named this way to
// be consistent with the filter class. UserIdentityType is also a number
static hashUserIdentity(userIdentity: IdentityType): IdentityType {
static hashUserIdentity(userIdentity: typeof IdentityType): typeof IdentityType {
return userIdentity;
}

Expand Down
4 changes: 2 additions & 2 deletions src/mp-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ export default function mParticleInstance(instanceName) {
*/
this.getEnvironment = function() {
return self._Store.SDKConfig.isDevelopmentMode
? Types.Environment.Development
: Types.Environment.Production;
? Constants.Environment.Development
: Constants.Environment.Production;
};
/**
* Returns the mParticle SDK version number
Expand Down
6 changes: 3 additions & 3 deletions src/sdkRuntimeModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '@mparticle/web-sdk';
import { IStore } from './store';
import Validators from './validators';
import { Dictionary } from './utils';
import { Dictionary, valueof } from './utils';
import { IServerModel } from './serverModel';
import { IKitConfigs } from './configAPIClient';
import { SDKConsentApi, SDKConsentState } from './consent';
Expand All @@ -28,7 +28,7 @@ import {
IdentityCallback,
ISDKUserAttributes,
} from './identity-user-interfaces';
import { IIdentityType } from './types.interfaces';
import { IdentityType } from './types';
import IntegrationCapture from './integrationCapture';
import { INativeSdkHelpers } from './nativeSdkHelpers.interfaces';
import { ICookieSyncManager, IPixelConfiguration } from './cookieSyncManager.interfaces';
Expand Down Expand Up @@ -153,7 +153,7 @@ interface IEvents {
export interface MParticleWebSDK {
addForwarder(mockForwarder: MPForwarder): void;
_IntegrationCapture: IntegrationCapture;
IdentityType: IIdentityType;
IdentityType: valueof<typeof IdentityType>;
_Identity: IIdentity;
Identity: SDKIdentityApi;
Logger: SDKLoggerApi;
Expand Down
23 changes: 12 additions & 11 deletions src/sideloadedKit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@ import {
IKitFilterSettings,
} from './configAPIClient';
import { UnregisteredKit } from './forwarders.interfaces';
import { EventTypeEnum, IdentityType } from './types.interfaces';
import { EventType, IdentityType } from './types';
import { valueof } from './utils';

export interface IMPSideloadedKit {
kitInstance: UnregisteredKit;
filterDictionary: IKitFilterSettings;

addEventTypeFilter(eventType: EventTypeEnum): void;
addEventNameFilter(eventType: EventTypeEnum, eventName: string): void;
addEventTypeFilter(eventType: valueof<typeof EventType>): void;
addEventNameFilter(eventType: valueof<typeof EventType>, eventName: string): void;
addEventAttributeFilter(
eventType: EventTypeEnum,
eventType: valueof<typeof EventType>,
eventName: string,
customAttributeKey: string
): void;
addScreenNameFilter(screenName: string): void;
addScreenAttributeFilter(screenName: string, screenAttribute: string): void;
addUserIdentityFilter(userIdentity: IdentityType): void;
addUserIdentityFilter(userIdentity: typeof IdentityType): void;
addUserAttributeFilter(userAttributeKey: string): void;
}

Expand Down Expand Up @@ -57,13 +58,13 @@ export default class MPSideloadedKit implements IMPSideloadedKit{
this.kitInstance = unregisteredKitInstance;
}

public addEventTypeFilter(eventType: EventTypeEnum): void {
public addEventTypeFilter(eventType: valueof<typeof EventType>): void {
const hashedEventType = KitFilterHelper.hashEventType(eventType);
this.filterDictionary.eventTypeFilters.push(hashedEventType);
}

public addEventNameFilter(
eventType: EventTypeEnum,
eventType: valueof<typeof EventType>,
eventName: string
): void {
const hashedEventName = KitFilterHelper.hashEventName(
Expand All @@ -74,7 +75,7 @@ export default class MPSideloadedKit implements IMPSideloadedKit{
}

public addEventAttributeFilter(
eventType: EventTypeEnum,
eventType: valueof<typeof EventType>,
eventName: string,
customAttributeKey: string
): void {
Expand All @@ -89,7 +90,7 @@ export default class MPSideloadedKit implements IMPSideloadedKit{
public addScreenNameFilter(screenName: string): void {
const hashedScreenName = KitFilterHelper.hashEventName(
screenName,
EventTypeEnum.Unknown
EventType.Unknown,
);
this.filterDictionary.screenNameFilters.push(hashedScreenName);
}
Expand All @@ -99,7 +100,7 @@ export default class MPSideloadedKit implements IMPSideloadedKit{
screenAttribute: string
): void {
const hashedScreenAttribute = KitFilterHelper.hashEventAttributeKey(
EventTypeEnum.Unknown,
EventType.Unknown,
screenName,
screenAttribute
);
Expand All @@ -108,7 +109,7 @@ export default class MPSideloadedKit implements IMPSideloadedKit{
);
}

public addUserIdentityFilter(userIdentity: IdentityType): void {
public addUserIdentityFilter(userIdentity: typeof IdentityType): void {
const hashedIdentityType = KitFilterHelper.hashUserIdentity(
userIdentity
);
Expand Down
66 changes: 0 additions & 66 deletions src/type-utils.ts

This file was deleted.

Loading

0 comments on commit fc5dedc

Please sign in to comment.