Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[main] deactive send beacon when local storage is available #2184

Merged
merged 9 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
24 changes: 21 additions & 3 deletions channels/1ds-post-js/src/HttpManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ export class HttpManager {
_disableXhrSync = !!channelConfig.disableXhrSync;
_disableFetchKeepAlive = !!channelConfig.disableFetchKeepAlive;
_addNoResponse = channelConfig.addNoResponse !== false;

if (!!core.getPlugin("LocalStorage")) {
// Always disable fetch keep alive when persisten storage is available
_disableFetchKeepAlive = true;
}

_useBeacons = !isReactNative(); // Only use beacons if not running in React Native
_serializer = new Serializer(_core, valueSanitizer, stringifyObjects, enableCompoundKey);
Expand Down Expand Up @@ -596,8 +601,10 @@ export class HttpManager {
let nav = getNavigator();
if (!nav.sendBeacon(theUrl, payload.data)) {
if (thePayload) {
let persistStorage = !!_core.getPlugin("Localstorage");
// Failed to send entire payload so try and split data and try to send as much events as possible
let droppedBatches: EventBatch[] = [];
let sentBatches: EventBatch[] = [];
arrForEach(thePayload.batches, (theBatch) => {
if (droppedBatches && theBatch && theBatch.count() > 0) {
let theEvents = theBatch.events();
Expand All @@ -606,6 +613,8 @@ export class HttpManager {
// Can't send anymore, so split the batch and drop the rest
droppedBatches.push(theBatch.split(lp));
break;
} else {
sentBatches.push(theBatch.split(lp));
}
}
} else {
Expand All @@ -614,7 +623,14 @@ export class HttpManager {
}
});

_sendBatchesNotification(droppedBatches, EventBatchNotificationReason.SizeLimitExceeded, thePayload.sendType, true);
if (sentBatches.length > 0) {
// Update the payload with the sent batches
thePayload.sentEvts = sentBatches;
}

if (!persistStorage) {
_sendBatchesNotification(droppedBatches, EventBatchNotificationReason.SizeLimitExceeded, thePayload.sendType, true);
}
} else {
status = 0;
}
Expand Down Expand Up @@ -1205,19 +1221,21 @@ export class HttpManager {
_postManager._backOffTransmission();
}

let theBatches = thePayload.batches;
if (batchReason === EventBatchNotificationReason.Complete) {
theBatches = thePayload.sentEvts || thePayload.batches;
if (!backOffTrans && !thePayload.isSync) {
// We have a successful async response, so the lets open the floodgates
// The reason for checking isSync is to avoid unblocking if beacon send occurred as it
// doesn't wait for a response.
_postManager._clearBackOff();
}

_addCompleteTimings(thePayload.batches);
_addCompleteTimings(theBatches);
}

// Send the notifications synchronously
_sendBatchesNotification(thePayload.batches, batchReason, thePayload.sendType, true);
_sendBatchesNotification(theBatches, batchReason, thePayload.sendType, true);

} finally {
if (thePayload.sendType === EventSendType.Batched) {
Expand Down
5 changes: 5 additions & 0 deletions channels/1ds-post-js/src/Serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ export interface ISerializedPayload {
*/
batches: EventBatch[];

/**
* The events that have been sent if not the full payload
*/
sentEvts?: EventBatch[];

/**
* The number of events in the payload
*/
Expand Down
Loading