From f9e4cff95fbdd3e2886a21d7171f77e079cd76ca Mon Sep 17 00:00:00 2001 From: gabriel reis Date: Tue, 13 Aug 2024 16:31:24 -0300 Subject: [PATCH] implement scheduling without timers --- src/constants.js | 2 -- src/index.js | 67 +++++++++++++++++++----------------------------- 2 files changed, 26 insertions(+), 43 deletions(-) diff --git a/src/constants.js b/src/constants.js index bd80715..0812936 100644 --- a/src/constants.js +++ b/src/constants.js @@ -3,8 +3,6 @@ export const scopeTag = { value: '@hCaptcha/react' } -export const MAX_EXECUTION_TRIALS = 3; - export const breadcrumbMessages = { mounted: 'hCaptcha component mounted', expired: 'hCaptcha expired', diff --git a/src/index.js b/src/index.js index 77dd12f..8e66617 100644 --- a/src/index.js +++ b/src/index.js @@ -2,7 +2,7 @@ import * as React from 'react'; import { hCaptchaLoader, initSentry } from '@hcaptcha/loader'; import { getFrame, getMountElement } from './utils.js'; -import { breadcrumbMessages, scopeTag, MAX_EXECUTION_TRIALS } from "./constants"; +import { breadcrumbMessages, scopeTag } from "./constants"; class HCaptcha extends React.Component { @@ -22,6 +22,7 @@ class HCaptcha extends React.Component { this.resetCaptcha = this.resetCaptcha.bind(this); this.removeCaptcha = this.removeCaptcha.bind(this); this.isReady = this.isReady.bind(this); + this._onReady = null; // Event Handlers this.loadCaptcha = this.loadCaptcha.bind(this); @@ -37,8 +38,6 @@ class HCaptcha extends React.Component { this.apiScriptRequested = false; this.sentryHub = null; - this.executionCounts = 0; - this.state = { isApiReady: false, isRemoved: false, @@ -193,6 +192,7 @@ class HCaptcha extends React.Component { this.setState({ isRemoved: false, captchaId }, () => { onReady && onReady(); + this._onReady && this._onReady(id); }); } @@ -232,7 +232,7 @@ class HCaptcha extends React.Component { }); } - handleOnLoad () { + handleOnLoad () { this.setState({ isApiReady: true }, () => { try { const element = getMountElement(this.props.scriptLocation); @@ -328,51 +328,36 @@ class HCaptcha extends React.Component { this.props.onChalExpired(); } - handleExecuteTrial (opts = null) { - this.executionCounts += 1; - - if (this.executionCounts > MAX_EXECUTION_TRIALS) { - if (opts && opts.async) { - return Promise.resolve({}); - } - - return; - } - - if (opts && opts.async) { - return new Promise(resolve => setTimeout(() => { - resolve(this.execute(opts)); - }, 500)); - } else { - setTimeout(() => { - this.execute(opts); - }, 500); - } - } - execute (opts = null) { try { const { captchaId } = this.state; const hcaptcha = this._hcaptcha; - + if (!this.isReady()) { - return this.handleExecuteTrial(opts); - } - - this.executionCounts = 0; - - if (opts && typeof opts !== "object") { - opts = null; - } - - if (opts && opts.async) { - return Promise.resolve(hcaptcha.execute(captchaId, opts)); - } else { - hcaptcha.execute(captchaId, opts); + const onReady = new Promise((resolve, reject) => { + this._onReady = (id) => { + try { + if (opts && opts.async) { + hcaptcha.execute(id, opts).then(resolve).catch(reject); + } else { + resolve(hcaptcha.execute(id, opts)); + } + } catch (e) { + reject(e); + } + }; + }); + + return opts?.async ? onReady : null; } - + + return hcaptcha.execute(captchaId, opts); } catch (error) { this.sentryHub.captureException(error); + if (opts && opts.async) { + return Promise.reject(error); + } + return null; } }