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
5 changes: 4 additions & 1 deletion src/apiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ export default function APIClient(
const millis: number = parseNumber(mpInstance._Helpers.getFeatureFlag(
Constants.FeatureFlags.EventBatchingIntervalMillis
) as string);
this.uploader = new BatchUploader(mpInstance, millis);
const quickMillis: number = parseNumber(mpInstance._Helpers.getFeatureFlag(
Constants.FeatureFlags.QuickBatchIntervalMillis
) as string);
this.uploader = new BatchUploader(mpInstance, millis, quickMillis);
}
this.uploader.queueEvent(event);

Expand Down
18 changes: 17 additions & 1 deletion src/batchUploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class BatchUploader {
static readonly CONTENT_TYPE: string = 'text/plain;charset=UTF-8';
static readonly MINIMUM_INTERVAL_MILLIS: number = 500;
uploadIntervalMillis: number;
quickBatchIntervalMillis: number;
eventsQueuedForProcessing: SDKEvent[];
batchesQueuedForProcessing: Batch[];
mpInstance: IMParticleWebSDKInstance;
Expand All @@ -53,14 +54,17 @@ export class BatchUploader {
* @param {IMParticleWebSDKInstance} mpInstance - the mParticle SDK instance
* @param {number} uploadInterval - the desired upload interval in milliseconds
*/
constructor(mpInstance: IMParticleWebSDKInstance, uploadInterval: number) {
constructor(mpInstance: IMParticleWebSDKInstance, uploadInterval: number, quickBatchIntervalMillis: number = 2000) {
this.mpInstance = mpInstance;
this.uploadIntervalMillis = uploadInterval;
this.quickBatchIntervalMillis = quickBatchIntervalMillis;
this.batchingEnabled =
uploadInterval >= BatchUploader.MINIMUM_INTERVAL_MILLIS;
if (this.uploadIntervalMillis < BatchUploader.MINIMUM_INTERVAL_MILLIS) {
this.uploadIntervalMillis = BatchUploader.MINIMUM_INTERVAL_MILLIS;
}

const { getFeatureFlag } = this.mpInstance._Helpers;

// Events will be queued during `queueEvents` method
this.eventsQueuedForProcessing = [];
Expand Down Expand Up @@ -104,6 +108,18 @@ export class BatchUploader {
? new FetchUploader(this.uploadUrl)
: new XHRUploader(this.uploadUrl);

// TODO: Create Feature Flag for Quick Batch Interval
if (this.quickBatchIntervalMillis < this.uploadIntervalMillis) {
setTimeout(() => {
if (getFeatureFlag(Constants.FeatureFlags.AstBackgroundEvents)) {
const event = this.createBackgroundASTEvent();
this.queueEvent(event);
}

this.prepareAndUpload(false, false);
}, this.quickBatchIntervalMillis);
}

this.triggerUploadInterval(true, false);
this.addEventListeners();
}
Expand Down
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ const Constants = {
maxCookieSize: 3000, // Number of bytes for cookie size to not exceed
aliasMaxWindow: 90, // Max age of Alias request startTime, in days
uploadInterval: 0, // Maximum milliseconds in between batch uploads, below 500 will mean immediate upload. The server returns this as a string, but we are using it as a number internally
quickBatchIntervalMillis: 2000, // Maximum milliseconds in between quick batch uploads
},
DefaultBaseUrls: {
v1SecureServiceUrl: 'jssdks.mparticle.com/v1/JS/',
Expand Down Expand Up @@ -194,6 +195,7 @@ const Constants = {
// - 'roktonly' → capture only Rokt-related IDs
CaptureIntegrationSpecificIdsV2: 'captureIntegrationSpecificIdsV2',
AstBackgroundEvents: 'astBackgroundEvents',
QuickBatchIntervalMillis: 'quickBatchIntervalMillis',
},
DefaultInstance: 'default_instance',
CCPAPurpose: 'data_sale_opt_out',
Expand Down
5 changes: 4 additions & 1 deletion src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export interface IFeatureFlags {
captureIntegrationSpecificIds?: boolean;
captureIntegrationSpecificIdsV2?: string;
astBackgroundEvents?: boolean;
quickBatchIntervalMillis?: number;
}

// Temporary Interface until Store can be refactored as a class
Expand Down Expand Up @@ -773,7 +774,8 @@ export function processFlags(config: SDKInitConfig): IFeatureFlags {
AudienceAPI,
CaptureIntegrationSpecificIds,
CaptureIntegrationSpecificIdsV2,
AstBackgroundEvents
AstBackgroundEvents,
QuickBatchIntervalMillis,
} = Constants.FeatureFlags;

if (!config.flags) {
Expand All @@ -794,6 +796,7 @@ export function processFlags(config: SDKInitConfig): IFeatureFlags {
flags[CaptureIntegrationSpecificIds] = config.flags[CaptureIntegrationSpecificIds] === 'True';
flags[CaptureIntegrationSpecificIdsV2] = (config.flags[CaptureIntegrationSpecificIdsV2] || 'none');
flags[AstBackgroundEvents] = config.flags[AstBackgroundEvents] === 'True';
flags[QuickBatchIntervalMillis] = config.flags[QuickBatchIntervalMillis] || Constants.DefaultConfig.quickBatchIntervalMillis;
return flags;
}

Expand Down
2 changes: 1 addition & 1 deletion test/jest/batchUploader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('BatchUploader', () => {
}
} as unknown as IMParticleWebSDKInstance;

batchUploader = new BatchUploader(mockMPInstance, 1000);
batchUploader = new BatchUploader(mockMPInstance, 1000, 2000);
});

afterEach(() => {
Expand Down
1 change: 1 addition & 0 deletions test/src/_test.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ import './tests-identityApiClient';
import './tests-integration-capture';
import './tests-batchUploader_4';
import './tests-identity';
import './tests-quick-batch';

1 change: 0 additions & 1 deletion test/src/tests-batchUploader_3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const enableBatchingConfigFlags = {
};

describe('batch uploader', () => {
let mockServer;
let clock;

beforeEach(() => {
Expand Down
Loading
Loading