Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore v4: optimize pro update boot #4590

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 };
Loading