Skip to content

Commit

Permalink
feat: add max count retries on promise throw (#7053)
Browse files Browse the repository at this point in the history
  • Loading branch information
Varixo authored Nov 9, 2024
1 parent a7f67c3 commit 7bd2fff
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
10 changes: 6 additions & 4 deletions packages/qwik/src/core/shared/component-execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
USE_ON_LOCAL,
USE_ON_LOCAL_SEQ_IDX,
} from './utils/markers';
import { isPromise, maybeThen, safeCall } from './utils/promises';
import { MAX_RETRY_ON_PROMISE_COUNT, isPromise, maybeThen, safeCall } from './utils/promises';
import type { ValueOrPromise } from './utils/types';
import type { Container, HostElement } from './types';
import { logWarn } from './utils/log';
Expand Down Expand Up @@ -79,7 +79,7 @@ export const executeComponent = (
componentFn = () => invokeApply(iCtx, inlineComponent, [props || EMPTY_OBJ]);
}

const executeComponentWithPromiseExceptionRetry = (): ValueOrPromise<JSXOutput> =>
const executeComponentWithPromiseExceptionRetry = (retryCount = 0): ValueOrPromise<JSXOutput> =>
safeCall<JSXOutput, JSXOutput, JSXOutput>(
() => {
container.setHostProp(renderHost, ELEMENT_SEQ_IDX, null);
Expand All @@ -100,8 +100,10 @@ export const executeComponent = (
return jsx;
},
(err) => {
if (isPromise(err)) {
return err.then(executeComponentWithPromiseExceptionRetry) as Promise<JSXOutput>;
if (isPromise(err) && retryCount < MAX_RETRY_ON_PROMISE_COUNT) {
return err.then(() =>
executeComponentWithPromiseExceptionRetry(retryCount++)
) as Promise<JSXOutput>;
} else {
throw err;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/qwik/src/core/shared/utils/promises.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { throwErrorAndStop } from './log';
import type { ValueOrPromise } from './types';

export const MAX_RETRY_ON_PROMISE_COUNT = 10;
export const MAX_RETRY_ON_PROMISE_COUNT = 100;

export type PromiseTree<T> = T | Promise<T> | Promise<T[]> | Array<PromiseTree<T>>;

Expand Down
7 changes: 4 additions & 3 deletions packages/qwik/src/qwikloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const qwikLoader = (
const isConnected = 'isConnected';
const qvisible = 'qvisible';
const Q_JSON = '_qwikjson_';
const MAX_RETRY_ON_PROMISE_COUNT = 100;
const nativeQuerySelectorAll = (root: ParentNode, selector: string) =>
Array.from(root.querySelectorAll(selector));
const querySelectorAll = (query: string) => {
Expand Down Expand Up @@ -160,7 +161,7 @@ export const qwikLoader = (
}
const previousCtx = doc[Q_CONTEXT];
if (element[isConnected]) {
const handleEvent = async () => {
const handleEvent = async (retryCount = 0) => {
try {
doc[Q_CONTEXT] = [element, ev, url];
isSync || emitEvent<QwikSymbolEvent>('qsymbol', { ...eventData });
Expand All @@ -171,8 +172,8 @@ export const qwikLoader = (
}
} catch (error) {
// retry if error is a promise. This means that probably a QRL is not resolved yet and we need to retry it.
if (isPromise(error)) {
error.then(() => handleEvent());
if (isPromise(error) && retryCount < MAX_RETRY_ON_PROMISE_COUNT) {
error.then(() => handleEvent(retryCount++));
} else {
emitEvent('qerror', { error, ...eventData });
}
Expand Down

0 comments on commit 7bd2fff

Please sign in to comment.