Skip to content

Commit

Permalink
implement scheduling without timers
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielferreira-imi committed Aug 13, 2024
1 parent 0966117 commit f9e4cff
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 43 deletions.
2 changes: 0 additions & 2 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
67 changes: 26 additions & 41 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand All @@ -37,8 +38,6 @@ class HCaptcha extends React.Component {
this.apiScriptRequested = false;
this.sentryHub = null;

this.executionCounts = 0;

this.state = {
isApiReady: false,
isRemoved: false,
Expand Down Expand Up @@ -193,6 +192,7 @@ class HCaptcha extends React.Component {

this.setState({ isRemoved: false, captchaId }, () => {
onReady && onReady();
this._onReady && this._onReady(id);
});
}

Expand Down Expand Up @@ -232,7 +232,7 @@ class HCaptcha extends React.Component {
});
}

handleOnLoad () {
handleOnLoad () {
this.setState({ isApiReady: true }, () => {
try {
const element = getMountElement(this.props.scriptLocation);
Expand Down Expand Up @@ -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;
}
}

Expand Down

0 comments on commit f9e4cff

Please sign in to comment.