Skip to content

Commit

Permalink
added timeout to polling
Browse files Browse the repository at this point in the history
  • Loading branch information
n13 committed Jan 29, 2024
1 parent 1c0f541 commit cda40a2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
24 changes: 17 additions & 7 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import { v4 as uuidv4 } from "uuid";


export const poll = function (fn, data, interval) {
export const poll = function (fn, data, interval, totalTimeout) {
interval = interval || 100;
totalTimeout = totalTimeout || 120000;

const startTime = Date.now();

var checkCondition = async (resolve, reject) => {
var result = await fn(data);
if (result) {
resolve(result);
} else {
setTimeout(checkCondition, interval, resolve, reject);
try {
var result = await fn(data);
if (result) {
resolve(result);
} else {
if (Date.now() - startTime < totalTimeout) {
setTimeout(checkCondition, interval, resolve, reject);
} else {
reject(new Error("Timeout"));
}
}
} catch (error) {
reject(error);
}
};

Expand Down
16 changes: 12 additions & 4 deletions src/web-transport/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ const checkLoginData = function (transaction, loginCode, loginContract) {
};

class WebTransportLink {
constructor(esrUtil, pollingInterval = 1000, transactionCheckInterval = 500) {
constructor(
esrUtil,
pollingInterval = 1000,
transactionCheckInterval = 500,
pollTimeout = 10 * 60 * 1000, // 10 minutes
transactionCheckTimeout = 30 * 1000 // 30 seconds
) {
if (!esrUtil || !esrUtil.rpc) {
throw new Error("Invalid esrUtil or esrUtil.rpc not found " + esrUtil);
}
Expand All @@ -31,6 +37,8 @@ class WebTransportLink {
this.rpc = esrUtil.rpc;
this.pollingInterval = pollingInterval;
this.transactionCheckInterval = transactionCheckInterval;
this.pollTimeout = pollTimeout;
this.transactionCheckTimeout = transactionCheckTimeout;
this.login = this.login.bind(this);
this.restore = this.restore.bind(this);
this.logout = this.logout.bind(this);
Expand Down Expand Up @@ -92,7 +100,7 @@ class WebTransportLink {
throw new UALHyphaWalletError(
"No transaction has been passed to sign transaction"
);
const { pollingInterval, transactionCheckInterval } = this;
const { pollingInterval, transactionCheckInterval, pollTimeout, transactionCheckTimeout } = this;
const uid = getTransactionUID(transaction);
const callbackUrl = `${CALLBACK_HOST}/transaction?uid=${uid}&tx_id={{tx}}`;
const esr = await this.esrUtil.encodeESR(
Expand All @@ -118,9 +126,9 @@ class WebTransportLink {

await this.dialog.showDialog(dialog);

const txId = await poll(this.checkForConfirmation, uid, pollingInterval);
const txId = await poll(this.checkForConfirmation, uid, pollingInterval, pollTimeout);

const transactionInfo = await poll(this.checkTransactionId, txId, transactionCheckInterval);
const transactionInfo = await poll(this.checkTransactionId, txId, transactionCheckInterval, transactionCheckTimeout);

this.dialog.hide();
return transactionInfo;
Expand Down

0 comments on commit cda40a2

Please sign in to comment.