From 24375a258e49d3ff837b30be94590badee051cd5 Mon Sep 17 00:00:00 2001 From: mosi Date: Tue, 12 Nov 2024 17:37:15 +0800 Subject: [PATCH] fix: detect error --- core/base/src/chains/conflux/index.ts | 1 + core/base/src/chains/ethereum/Fluent/index.ts | 1 + core/base/src/detect/index.ts | 17 ++--------------- core/base/src/emitter/RPCMethod.ts | 3 ++- core/base/src/emitter/index.ts | 3 +-- 5 files changed, 7 insertions(+), 18 deletions(-) diff --git a/core/base/src/chains/conflux/index.ts b/core/base/src/chains/conflux/index.ts index 50bbc78..51503ee 100644 --- a/core/base/src/chains/conflux/index.ts +++ b/core/base/src/chains/conflux/index.ts @@ -7,6 +7,7 @@ export class ConfluxRPCMethod extends RPCMethod { sessionKey = 'conflux'; injectFlag = 'conflux'; declare provider: Provider; + subTimeout = 5000; detectProvider = (config: CustomDetectConfig = {}) => { const p = detectProvider({ injectFlag: this.injectFlag, ...config }); diff --git a/core/base/src/chains/ethereum/Fluent/index.ts b/core/base/src/chains/ethereum/Fluent/index.ts index 6fd4818..fee3599 100644 --- a/core/base/src/chains/ethereum/Fluent/index.ts +++ b/core/base/src/chains/ethereum/Fluent/index.ts @@ -7,6 +7,7 @@ import type { CustomDetectConfig } from '../../../emitter/RPCMethod'; class FluentRPCMethod extends EthereumRPCMethod { sessionKey = 'fluent-isFluent'; injectFlag = 'fluent'; + subTimeout = 5000; detectProvider = (config: CustomDetectConfig = {}) => detectProvider({ injectFlag: this.injectFlag, walletFlag: 'isFluent', ...config }); requestCrossNetworkPermission = () => diff --git a/core/base/src/detect/index.ts b/core/base/src/detect/index.ts index 48bbada..2ddf3c3 100644 --- a/core/base/src/detect/index.ts +++ b/core/base/src/detect/index.ts @@ -1,16 +1,3 @@ -export class NotDetectedError extends Error { - constructor(public message: string) { - super(message); - this.message = message; - } -} -export class NotMatchError extends Error { - constructor(public message: string) { - super(message); - this.message = message; - } -} - export interface DetectProviderConfig { silent?: boolean; interval?: number; @@ -35,7 +22,7 @@ export default function detectProvider({ let provider = (await getProvider(injectFlag, interval, timeout)) as T; if (!provider) { - return reject(new NotDetectedError(`Unable to detect window.${injectFlag}.`)); + return reject(`Unable to detect window.${injectFlag}.`); } let mustBeSpecifiedWallet = false; @@ -63,7 +50,7 @@ export default function detectProvider({ } else { const message = `Non-${walletFlag} Wallet detected.`; !silent && console.error('detect-provider:', message); - reject(new NotMatchError(message)); + reject(message); } } handleEthereum(); diff --git a/core/base/src/emitter/RPCMethod.ts b/core/base/src/emitter/RPCMethod.ts index 0a82d29..c51a0fa 100644 --- a/core/base/src/emitter/RPCMethod.ts +++ b/core/base/src/emitter/RPCMethod.ts @@ -30,11 +30,12 @@ abstract class RPCMethod { detectAndSetProvider = (config?: CustomDetectConfig) => { const p = this.detectProvider(config); - p.then((provider) => (this.provider = provider)).catch((err) => console.warn(err)); + p.then((provider) => (this.provider = provider)).catch((err) => console.warn(err, Date.now())); return p; }; subProvider = () => { + if (this.provider) return Promise.resolve(this.provider); return this.detectAndSetProvider({ timeout: this.subTimeout, interval: this.subInterval, diff --git a/core/base/src/emitter/index.ts b/core/base/src/emitter/index.ts index 24c692f..0818e1e 100644 --- a/core/base/src/emitter/index.ts +++ b/core/base/src/emitter/index.ts @@ -1,7 +1,6 @@ import mitt from 'mitt'; import Unit from '../unit'; import type RPCMethod from './RPCMethod'; -import { NotDetectedError } from '../detect'; export type State = { status: 'in-detecting' | 'not-installed' | 'not-active' | 'in-activating' | 'active'; @@ -42,7 +41,7 @@ class Emitter { .catch((e) => { this.handleStatusChanged('not-installed'); this.resolveDetect(); - if (e instanceof NotDetectedError) { + if (typeof e === 'string' && e.startsWith('Unable to detect window.')) { this.RPCMethod.subProvider().then(this.setProvider); } });