Skip to content

Commit

Permalink
chore: optimize pro update boot (#4590)
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteZhang1024 authored May 15, 2024
1 parent 2158a52 commit ff53e1e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
16 changes: 12 additions & 4 deletions packages/kit-bg/src/services/ServiceHardware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
import { isPassphraseWallet } from '@onekeyhq/shared/src/engine/engineUtils';
import debugLogger from '@onekeyhq/shared/src/logger/debugLogger';
import platformEnv from '@onekeyhq/shared/src/platformEnv';
import { createPromiseWithTimeout } from '@onekeyhq/shared/src/utils/promiseUtils';
import { equalsIgnoreCase } from '@onekeyhq/shared/src/utils/stringUtils';
import type {
EOnekeyDomain,
Expand Down Expand Up @@ -433,10 +434,11 @@ class ServiceHardware extends ServiceBase {
maxTryCount = 10,
bootloaderMode = false,
) {
const hardwareSDK = await this.getSDKInstance();
return new Promise((resolve) => {
let tryCount = 0;
deviceUtils.startDeviceScan(
(response) => {
async (response) => {
tryCount += 1;
if (tryCount > maxTryCount) {
deviceUtils.stopScan();
Expand All @@ -451,9 +453,16 @@ class ServiceHardware extends ServiceBase {
: (response.payload ?? []).find((d) =>
equalsIgnoreCase(d.connectId, connectId),
);

if (deviceExist) {
deviceUtils.stopScan();
resolve(true);
const res = await createPromiseWithTimeout(
hardwareSDK.getFeatures(connectId),
2000,
);
if (res?.success) {
deviceUtils.stopScan();
resolve(true);
}
}
},
() => {},
Expand All @@ -470,7 +479,6 @@ class ServiceHardware extends ServiceBase {
// eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve) => {
const hardwareSDK = await this.getSDKInstance();
// restart count down
await wait(8000);
let tryCount = 0;
// polling device when restart success
Expand Down
23 changes: 22 additions & 1 deletion packages/shared/src/utils/promiseUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,25 @@ const createAnyPromise = <T>(promises: Promise<T>[]): Promise<T> =>
(val) => Promise.resolve(val),
);

export { createDelayPromise, createAnyPromise };
function createPromiseWithTimeout<T>(
promise: Promise<T>,
timeout: number,
): Promise<T> {
return new Promise<T>((resolve, reject) => {
const timer = setTimeout(() => {
reject(new Error('Operation timed out'));
}, timeout);

promise
.then((value) => {
clearTimeout(timer);
resolve(value);
})
.catch((err) => {
clearTimeout(timer);
reject(err);
});
});
}

export { createDelayPromise, createAnyPromise, createPromiseWithTimeout };

0 comments on commit ff53e1e

Please sign in to comment.