Skip to content

Commit 2c2eeb3

Browse files
feat: Trigger first batch quickly with AST Background Event
1 parent 7548f0b commit 2c2eeb3

8 files changed

Lines changed: 440 additions & 5 deletions

File tree

src/apiClient.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ export default function APIClient(
5656
const millis: number = parseNumber(mpInstance._Helpers.getFeatureFlag(
5757
Constants.FeatureFlags.EventBatchingIntervalMillis
5858
) as string);
59-
this.uploader = new BatchUploader(mpInstance, millis);
59+
const quickMillis: number = parseNumber(mpInstance._Helpers.getFeatureFlag(
60+
Constants.FeatureFlags.QuickBatchIntervalMillis
61+
) as string);
62+
this.uploader = new BatchUploader(mpInstance, millis, quickMillis);
6063
}
6164
this.uploader.queueEvent(event);
6265

src/batchUploader.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export class BatchUploader {
3636
static readonly CONTENT_TYPE: string = 'text/plain;charset=UTF-8';
3737
static readonly MINIMUM_INTERVAL_MILLIS: number = 500;
3838
uploadIntervalMillis: number;
39+
quickBatchIntervalMillis: number;
3940
eventsQueuedForProcessing: SDKEvent[];
4041
batchesQueuedForProcessing: Batch[];
4142
mpInstance: IMParticleWebSDKInstance;
@@ -53,14 +54,17 @@ export class BatchUploader {
5354
* @param {IMParticleWebSDKInstance} mpInstance - the mParticle SDK instance
5455
* @param {number} uploadInterval - the desired upload interval in milliseconds
5556
*/
56-
constructor(mpInstance: IMParticleWebSDKInstance, uploadInterval: number) {
57+
constructor(mpInstance: IMParticleWebSDKInstance, uploadInterval: number, quickBatchIntervalMillis: number = 2000) {
5758
this.mpInstance = mpInstance;
5859
this.uploadIntervalMillis = uploadInterval;
60+
this.quickBatchIntervalMillis = quickBatchIntervalMillis;
5961
this.batchingEnabled =
6062
uploadInterval >= BatchUploader.MINIMUM_INTERVAL_MILLIS;
6163
if (this.uploadIntervalMillis < BatchUploader.MINIMUM_INTERVAL_MILLIS) {
6264
this.uploadIntervalMillis = BatchUploader.MINIMUM_INTERVAL_MILLIS;
6365
}
66+
67+
const { getFeatureFlag } = this.mpInstance._Helpers;
6468

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

111+
// TODO: Create Feature Flag for Quick Batch Interval
112+
if (this.quickBatchIntervalMillis < this.uploadIntervalMillis) {
113+
setTimeout(() => {
114+
if (getFeatureFlag(Constants.FeatureFlags.AstBackgroundEvents)) {
115+
const event = this.createBackgroundASTEvent();
116+
this.queueEvent(event);
117+
}
118+
119+
this.prepareAndUpload(false, false);
120+
}, this.quickBatchIntervalMillis);
121+
}
122+
107123
this.triggerUploadInterval(true, false);
108124
this.addEventListeners();
109125
}

src/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ const Constants = {
129129
maxCookieSize: 3000, // Number of bytes for cookie size to not exceed
130130
aliasMaxWindow: 90, // Max age of Alias request startTime, in days
131131
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
132+
quickBatchIntervalMillis: 2000, // Maximum milliseconds in between quick batch uploads
132133
},
133134
DefaultBaseUrls: {
134135
v1SecureServiceUrl: 'jssdks.mparticle.com/v1/JS/',
@@ -194,6 +195,7 @@ const Constants = {
194195
// - 'roktonly' → capture only Rokt-related IDs
195196
CaptureIntegrationSpecificIdsV2: 'captureIntegrationSpecificIdsV2',
196197
AstBackgroundEvents: 'astBackgroundEvents',
198+
QuickBatchIntervalMillis: 'quickBatchIntervalMillis',
197199
},
198200
DefaultInstance: 'default_instance',
199201
CCPAPurpose: 'data_sale_opt_out',

src/store.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ export interface IFeatureFlags {
147147
captureIntegrationSpecificIds?: boolean;
148148
captureIntegrationSpecificIdsV2?: string;
149149
astBackgroundEvents?: boolean;
150+
quickBatchIntervalMillis?: number;
150151
}
151152

152153
// Temporary Interface until Store can be refactored as a class
@@ -773,7 +774,8 @@ export function processFlags(config: SDKInitConfig): IFeatureFlags {
773774
AudienceAPI,
774775
CaptureIntegrationSpecificIds,
775776
CaptureIntegrationSpecificIdsV2,
776-
AstBackgroundEvents
777+
AstBackgroundEvents,
778+
QuickBatchIntervalMillis,
777779
} = Constants.FeatureFlags;
778780

779781
if (!config.flags) {
@@ -794,6 +796,7 @@ export function processFlags(config: SDKInitConfig): IFeatureFlags {
794796
flags[CaptureIntegrationSpecificIds] = config.flags[CaptureIntegrationSpecificIds] === 'True';
795797
flags[CaptureIntegrationSpecificIdsV2] = (config.flags[CaptureIntegrationSpecificIdsV2] || 'none');
796798
flags[AstBackgroundEvents] = config.flags[AstBackgroundEvents] === 'True';
799+
flags[QuickBatchIntervalMillis] = config.flags[QuickBatchIntervalMillis] || Constants.DefaultConfig.quickBatchIntervalMillis;
797800
return flags;
798801
}
799802

test/jest/batchUploader.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('BatchUploader', () => {
3030
}
3131
} as unknown as IMParticleWebSDKInstance;
3232

33-
batchUploader = new BatchUploader(mockMPInstance, 1000);
33+
batchUploader = new BatchUploader(mockMPInstance, 1000, 2000);
3434
});
3535

3636
afterEach(() => {

test/src/_test.index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,5 @@ import './tests-identityApiClient';
3939
import './tests-integration-capture';
4040
import './tests-batchUploader_4';
4141
import './tests-identity';
42+
import './tests-quick-batch';
4243

test/src/tests-batchUploader_3.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ const enableBatchingConfigFlags = {
1919
};
2020

2121
describe('batch uploader', () => {
22-
let mockServer;
2322
let clock;
2423

2524
beforeEach(() => {

0 commit comments

Comments
 (0)