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

fix: Add longish timeout for polling calls and a shorter one when detecting iOS throttling RELEASE #828

Merged
merged 1 commit into from
Oct 26, 2024
Merged
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
60 changes: 52 additions & 8 deletions packages/sdks/web-component/src/lib/descope-wc/DescopeWc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
handleReportValidityOnBlur,
getUserLocale,
clearPreviousExternalInputs,
timeoutPromise,
} from '../helpers';
import { calculateConditions, calculateCondition } from '../helpers/conditions';
import { getLastAuth, setLastAuth } from '../helpers/lastAuth';
Expand Down Expand Up @@ -615,19 +616,27 @@ class DescopeWc extends BaseDescopeWc {
}

#handlePollingResponse = (
executionId,
stepId,
action,
flowVersion,
componentsVersion,
executionId: string,
stepId: string,
action: string,
flowVersion?: number,
componentsVersion?: string,
rescheduled: boolean = false,
) => {
const pollingDefaultDelay = 2000;
const pollingDefaultTimeout = 6000;
const pollingThrottleDelay = 500;
const pollingThrottleThreshold = 500;
const pollingThrottleTimeout = 1000;
if (action === RESPONSE_ACTIONS.poll) {
// schedule next polling request for 2 seconds from now
this.logger.debug('polling - Scheduling polling request');
const scheduledAt = Date.now();
const delay = rescheduled ? pollingThrottleDelay : pollingDefaultDelay;
this.#pollingTimeout = setTimeout(async () => {
this.logger.debug('polling - Calling next');

const sdkResp = await this.sdk.flow.next(
const nextCall = this.sdk.flow.next(
executionId,
stepId,
CUSTOM_INTERACTIONS.polling,
Expand All @@ -636,6 +645,41 @@ class DescopeWc extends BaseDescopeWc {
{},
);

// Try to detect whether the tab is being throttled when running in a mobile browser, specifically on iOS.
// We check whether the tab seems to hidden and the polling callback was called much later than expected,
// in which case we allow a much shorter timeout for the polling request. The reschedule check ensures
// this cannot happen twice consecutively.
const throttled =
document.hidden &&
shilgapira marked this conversation as resolved.
Show resolved Hide resolved
!rescheduled &&
Date.now() - scheduledAt > delay + pollingThrottleThreshold;
if (throttled) {
this.logger.debug('polling - The polling seems to be throttled');
}

let sdkResp: Awaited<typeof nextCall>;
try {
const timeout = throttled
? pollingThrottleTimeout
: pollingDefaultTimeout;
sdkResp = await timeoutPromise(timeout, nextCall);
} catch (err) {
this.logger.warn(
shilgapira marked this conversation as resolved.
Show resolved Hide resolved
`polling - The ${
throttled ? 'throttled fetch' : 'fetch'
} call timed out or was aborted`,
);
this.#handlePollingResponse(
executionId,
stepId,
action,
flowVersion,
componentsVersion,
throttled,
);
return;
}

if (sdkResp?.error?.errorCode === FETCH_EXCEPTION_ERROR_CODE) {
this.logger.debug(
'polling - Got a generic error due to exception in fetch call',
Expand All @@ -647,9 +691,9 @@ class DescopeWc extends BaseDescopeWc {
flowVersion,
componentsVersion,
);

return;
}

this.logger.debug('polling - Got a response');
if (sdkResp?.error) {
this.logger.debug(
Expand All @@ -668,7 +712,7 @@ class DescopeWc extends BaseDescopeWc {
flowVersion,
componentsVersion,
);
}, 2000);
}, delay);
}
};

Expand Down
Loading