Skip to content

Commit

Permalink
chore: refactory async-promise-executor
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-sanderson committed Nov 7, 2024
1 parent 54ddde7 commit 1029708
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 58 deletions.
45 changes: 21 additions & 24 deletions packages/suite-desktop-core/e2e/support/regtest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,30 @@ export const generateBlock = () =>
method: 'GET',
});

export const waitForCoinjoinBackend = () =>
// Todo: refactor to not use async-promise-executor
// eslint-disable-next-line no-async-promise-executor
new Promise<void>(async (resolve, reject) => {
const limit = 60;
const error = '';

console.log('waiting for coinjoin backend');

for (let i = 0; i < limit; i++) {
if (i === limit - 1) {
console.log(`waiting for coinjoin backend: ${error}\n`);
}
export const waitForCoinjoinBackend = async () => {
const limit = 60;
const error = '';

console.log('waiting for coinjoin backend');

await new Promise(resolve => setTimeout(() => resolve(undefined), 1000));
for (let i = 0; i < limit; i++) {
if (i === limit - 1) {
console.log(`waiting for coinjoin backend: ${error}\n`);
}

try {
const res = await fetch('http://localhost:19121/');
if (res.status === 200) {
console.log('coinjoin backend is online');
await new Promise(resolve => setTimeout(() => resolve(undefined), 1000));

return resolve();
}
} catch {
process.stdout.write('.');
try {
const res = await fetch('http://localhost:19121/');
if (res.status === 200) {
console.log('coinjoin backend is online');

return;
}
} catch {
process.stdout.write('.');
}
}

reject(error);
});
throw error;
};
64 changes: 30 additions & 34 deletions packages/trezor-user-env-link/src/websocket-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,43 +267,39 @@ export class WebsocketClient extends TypedEmitter<WebsocketClientEvents> {
this.removeAllListeners();
}

waitForTrezorUserEnv() {
// Todo: refactor to not use async-promise-executor
// eslint-disable-next-line no-async-promise-executor
return new Promise<void>(async (resolve, reject) => {
// unfortunately, it can take incredibly long for trezor-user-env to start, we should
// do something about it
const limit = 300;
let error = '';

console.log('waiting for trezor-user-env');

for (let i = 0; i < limit; i++) {
if (i === limit - 1) {
console.log(`cant connect to trezor-user-env: ${error}\n`);
async waitForTrezorUserEnv() {
// unfortunately, it can take incredibly long for trezor-user-env to start, we should
// do something about it
const limit = 300;
let error = '';

console.log('waiting for trezor-user-env');

for (let i = 0; i < limit; i++) {
if (i === limit - 1) {
console.log(`cant connect to trezor-user-env: ${error}\n`);
}
await delay(1000);

try {
const res = await fetch(USER_ENV_URL.DASHBOARD);
if (res.status === 200) {
console.log('trezor-user-env is online');

return;
}
await delay(1000);

try {
const res = await fetch(USER_ENV_URL.DASHBOARD);
if (res.status === 200) {
console.log('trezor-user-env is online');

return resolve();
}
} catch (err) {
error = err.message;
// using process.stdout.write instead of console.log since the latter always prints also newline
// but in karma, this code runs in browser and process is not available.
if (typeof process !== 'undefined') {
process.stdout.write('.');
} else {
console.log('.');
}
} catch (err) {
error = err.message;
// using process.stdout.write instead of console.log since the latter always prints also newline
// but in karma, this code runs in browser and process is not available.
if (typeof process !== 'undefined') {
process.stdout.write('.');
} else {
console.log('.');
}
}
}

reject(error);
});
throw error;
}
}

0 comments on commit 1029708

Please sign in to comment.