Skip to content

Commit

Permalink
Merge pull request #21 from Conflux-Chain/fix/detect-wallet
Browse files Browse the repository at this point in the history
fix: detect error
  • Loading branch information
mosshqq authored Nov 12, 2024
2 parents 5163ca1 + 24375a2 commit 182a005
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 18 deletions.
1 change: 1 addition & 0 deletions core/base/src/chains/conflux/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export class ConfluxRPCMethod extends RPCMethod {
sessionKey = 'conflux';
injectFlag = 'conflux';
declare provider: Provider;
subTimeout = 5000;

detectProvider = (config: CustomDetectConfig = {}) => {
const p = detectProvider<Provider>({ injectFlag: this.injectFlag, ...config });
Expand Down
1 change: 1 addition & 0 deletions core/base/src/chains/ethereum/Fluent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Provider>({ injectFlag: this.injectFlag, walletFlag: 'isFluent', ...config });

requestCrossNetworkPermission = () =>
Expand Down
17 changes: 2 additions & 15 deletions core/base/src/detect/index.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -35,7 +22,7 @@ export default function detectProvider<T extends Object>({
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;
Expand Down Expand Up @@ -63,7 +50,7 @@ export default function detectProvider<T extends Object>({
} else {
const message = `Non-${walletFlag} Wallet detected.`;
!silent && console.error('detect-provider:', message);
reject(new NotMatchError(message));
reject(message);
}
}
handleEthereum();
Expand Down
3 changes: 2 additions & 1 deletion core/base/src/emitter/RPCMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions core/base/src/emitter/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -42,7 +41,7 @@ class Emitter<T extends RPCMethod> {
.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);
}
});
Expand Down

0 comments on commit 182a005

Please sign in to comment.