11import { generateQuery , getFrame , getMountElement } from './utils' ;
2- import { HCAPTCHA_LOAD_FN_NAME , MAX_RETRIES , SCRIPT_ERROR , SCRIPT_ID , SENTRY_TAG } from './constants' ;
2+ import { HCAPTCHA_LOAD_FN_NAME , MAX_RETRIES , RETRY_DELAY , SCRIPT_ERROR , SCRIPT_ID , SENTRY_TAG } from './constants' ;
33import { initSentry } from './sentry' ;
44import { fetchScript } from './script' ;
55
@@ -123,8 +123,18 @@ export function hCaptchaApi(params: ILoaderParams = { cleanup: false }, sentry:
123123 }
124124}
125125
126- export async function loadScript ( params , sentry , retries = 0 ) {
127- const message = retries < MAX_RETRIES ? 'Retry loading hCaptcha Api' : 'Exceeded maximum retries' ;
126+ function delay ( ms : number ) : Promise < void > {
127+ return new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
128+ }
129+
130+ export async function loadScript (
131+ params : ILoaderParams ,
132+ sentry : SentryHub ,
133+ retries = 0
134+ ) : Promise < any > {
135+ const maxRetries = params . maxRetries ?? MAX_RETRIES ;
136+ const retryDelay = params . retryDelay ?? RETRY_DELAY ;
137+ const message = retries < maxRetries ? 'Retry loading hCaptcha Api' : 'Exceeded maximum retries' ;
128138
129139 try {
130140 const result = await hCaptchaApi ( params , sentry ) ;
@@ -139,19 +149,25 @@ export async function loadScript(params, sentry, retries = 0) {
139149 message,
140150 } ) ;
141151
142- if ( retries >= MAX_RETRIES ) {
152+ if ( retries >= maxRetries ) {
143153 lastLoadFailed = true ;
144-
154+
145155 sentry . captureException ( error ) ;
146156 return Promise . reject ( error ) ;
147157 } else {
158+ sentry . addBreadcrumb ( {
159+ category : SENTRY_TAG ,
160+ message : `Waiting ${ retryDelay } ms before retry attempt ${ retries + 1 } ` ,
161+ } ) ;
162+
163+ await delay ( retryDelay ) ;
148164 retries += 1 ;
165+
149166 return loadScript ( params , sentry , retries ) ;
150167 }
151168 }
152169}
153170
154-
155171export async function hCaptchaLoader ( params : ILoaderParams = { } ) {
156172 const sentry = initSentry ( params . sentry ) ;
157173
0 commit comments