Skip to content

Commit

Permalink
fix: change sub to detect
Browse files Browse the repository at this point in the history
  • Loading branch information
mosshqq committed Nov 11, 2024
1 parent df17d4b commit faddf0a
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 43 deletions.
4 changes: 3 additions & 1 deletion core/base/src/chains/conflux/Fluent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import Emitter from '../../../emitter';
import detectProvider from '../../../detect';
import { ConfluxRPCMethod } from '../index';
import type { Provider } from '../type';
import type { CustomDetectConfig } from '../../../emitter/RPCMethod';

class FluentRPCMethod extends ConfluxRPCMethod {
sessionKey = 'conflux-isFluent';
detectProvider = () => detectProvider<Provider>({ injectFlag: this.injectFlag, walletFlag: 'isFluent' });

detectProvider = (config: CustomDetectConfig = {}) => detectProvider<Provider>({ injectFlag: this.injectFlag, walletFlag: 'isFluent', ...config });

getAccounts = () =>
this.provider!.request({
Expand Down
3 changes: 2 additions & 1 deletion core/base/src/chains/conflux/Portal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import Emitter from '../../../emitter';
import detectProvider from '../../../detect';
import { ConfluxRPCMethod } from '../index';
import type { Provider } from '../type';
import type { CustomDetectConfig } from '../../../emitter/RPCMethod';

class PortalRPCMethod extends ConfluxRPCMethod {
sessionKey = 'conflux-isConfluxPortal';
detectProvider = () => detectProvider<Provider>({ injectFlag: this.injectFlag, walletFlag: 'isConfluxPortal' });
detectProvider = (config: CustomDetectConfig = {}) => detectProvider<Provider>({ injectFlag: this.injectFlag, walletFlag: 'isConfluxPortal', ...config });
}

export default new Emitter(new PortalRPCMethod());
6 changes: 3 additions & 3 deletions core/base/src/chains/conflux/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Emitter from '../../emitter';
import RPCMethod from '../../emitter/RPCMethod';
import RPCMethod, { type CustomDetectConfig } from '../../emitter/RPCMethod';
import detectProvider from '../../detect';
import type { Provider, TransactionParameters, AddChainParameter, WatchAssetParams, TypedSignParams } from './type';

Expand All @@ -8,8 +8,8 @@ export class ConfluxRPCMethod extends RPCMethod {
injectFlag = 'conflux';
declare provider: Provider;

detectProvider = () => {
const p = detectProvider<Provider>({ injectFlag: this.injectFlag });
detectProvider = (config: CustomDetectConfig = {}) => {
const p = detectProvider<Provider>({ injectFlag: this.injectFlag, ...config });

// Conflux Portal Wallet support
p.then((provider) => {
Expand Down
3 changes: 2 additions & 1 deletion core/base/src/chains/ethereum/Coinbase/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import Emitter from '../../../emitter';
import detectProvider from '../../../detect';
import { EthereumRPCMethod } from '../index';
import type { Provider } from '../type';
import type { CustomDetectConfig } from '../../../emitter/RPCMethod';

class CoinbaseRPCMethod extends EthereumRPCMethod {
sessionKey = 'ethereum-isCoinbaseWallet';
detectProvider = () => detectProvider<Provider>({ injectFlag: this.injectFlag, walletFlag: 'isCoinbaseWallet' });
detectProvider = (config: CustomDetectConfig = {}) => detectProvider<Provider>({ injectFlag: this.injectFlag, walletFlag: 'isCoinbaseWallet', ...config });
}

export default new Emitter(new CoinbaseRPCMethod());
3 changes: 2 additions & 1 deletion core/base/src/chains/ethereum/Fluent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import Emitter from '../../../emitter';
import detectProvider from '../../../detect';
import { EthereumRPCMethod } from '../index';
import type { Provider } from '../type';
import type { CustomDetectConfig } from '../../../emitter/RPCMethod';

class FluentRPCMethod extends EthereumRPCMethod {
sessionKey = 'fluent-isFluent';
injectFlag = 'fluent';
detectProvider = () => detectProvider<Provider>({ injectFlag: this.injectFlag, walletFlag: 'isFluent' });
detectProvider = (config: CustomDetectConfig = {}) => detectProvider<Provider>({ injectFlag: this.injectFlag, walletFlag: 'isFluent', ...config });

requestCrossNetworkPermission = () =>
this.provider!.request({
Expand Down
3 changes: 2 additions & 1 deletion core/base/src/chains/ethereum/Halo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import Emitter from '../../../emitter';
import detectProvider from '../../../detect';
import { EthereumRPCMethod } from '../index';
import type { Provider } from '../type';
import type { CustomDetectConfig } from '../../../emitter/RPCMethod';

class HaloRPCMethod extends EthereumRPCMethod {
sessionKey = 'haloEthereum';
injectFlag = 'haloEthereum';
detectProvider = () => detectProvider<Provider>({ injectFlag: this.injectFlag });
detectProvider = (config: CustomDetectConfig = {}) => detectProvider<Provider>({ injectFlag: this.injectFlag, ...config });
}

export default new Emitter(new HaloRPCMethod());
4 changes: 3 additions & 1 deletion core/base/src/chains/ethereum/MetaMask/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import Emitter from '../../../emitter';
import detectProvider from '../../../detect';
import { EthereumRPCMethod } from '../index';
import type { Provider } from '../type';
import type { CustomDetectConfig } from '../../../emitter/RPCMethod';

class MetaMaskRPCMethod extends EthereumRPCMethod {
sessionKey = 'ethereum-isMetaMask';
detectProvider = () => detectProvider<Provider>({ injectFlag: this.injectFlag, walletFlag: 'isMetaMask', isSingleWalletFlag: true });
detectProvider = (config: CustomDetectConfig = {}) =>
detectProvider<Provider>({ injectFlag: this.injectFlag, walletFlag: 'isMetaMask', isSingleWalletFlag: true, ...config });
}

export default new Emitter(new MetaMaskRPCMethod());
3 changes: 2 additions & 1 deletion core/base/src/chains/ethereum/OKX/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import Emitter from '../../../emitter';
import detectProvider from '../../../detect';
import { EthereumRPCMethod } from '../index';
import type { Provider } from '../type';
import type { CustomDetectConfig } from '../../../emitter/RPCMethod';

class OKXRPCMethod extends EthereumRPCMethod {
sessionKey = 'okxwallet-isOkxWallet';
injectFlag = 'okxwallet';
detectProvider = () => detectProvider<Provider>({ injectFlag: this.injectFlag, walletFlag: 'isOkxWallet' });
detectProvider = (config: CustomDetectConfig = {}) => detectProvider<Provider>({ injectFlag: this.injectFlag, walletFlag: 'isOkxWallet', ...config });
}

export default new Emitter(new OKXRPCMethod());
3 changes: 2 additions & 1 deletion core/base/src/chains/ethereum/TokenPocket/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import Emitter from '../../../emitter';
import detectProvider from '../../../detect';
import { EthereumRPCMethod } from '../index';
import type { Provider } from '../type';
import type { CustomDetectConfig } from '../../../emitter/RPCMethod';

class TokenPocketRPCMethod extends EthereumRPCMethod {
sessionKey = 'ethereum-isTokenPocket';
detectProvider = () => detectProvider<Provider>({ injectFlag: this.injectFlag, walletFlag: 'isTokenPocket' });
detectProvider = (config: CustomDetectConfig = {}) => detectProvider<Provider>({ injectFlag: this.injectFlag, walletFlag: 'isTokenPocket', ...config });
}

export default new Emitter(new TokenPocketRPCMethod());
4 changes: 2 additions & 2 deletions core/base/src/chains/ethereum/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Emitter from '../../emitter';
import RPCMethod from '../../emitter/RPCMethod';
import RPCMethod, { type CustomDetectConfig } from '../../emitter/RPCMethod';
import detectProvider from '../../detect';
import type { Provider, TransactionParameters, AddChainParameter, WatchAssetParams, TypedSignParams } from './type';

Expand All @@ -8,7 +8,7 @@ export class EthereumRPCMethod extends RPCMethod {
injectFlag = 'ethereum';
declare provider: Provider;

detectProvider = () => detectProvider<Provider>({ injectFlag: this.injectFlag, defaultWalletFlag: 'isMetaMask' });
detectProvider = (config: CustomDetectConfig = {}) => detectProvider<Provider>({ injectFlag: this.injectFlag, defaultWalletFlag: 'isMetaMask', ...config });

requestAccounts = () => this.provider!.request({ method: 'eth_requestAccounts' });

Expand Down
20 changes: 11 additions & 9 deletions core/base/src/detect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ export class NotMatchError extends Error {
}
}

export interface DetectProviderConfig {
silent?: boolean;
interval?: number;
timeout?: number;
walletFlag?: string;
isSingleWalletFlag?: boolean;
injectFlag: string;
defaultWalletFlag?: string;
}

export default function detectProvider<T extends Object>({
silent = false,
interval = 10,
Expand All @@ -19,15 +29,7 @@ export default function detectProvider<T extends Object>({
isSingleWalletFlag,
injectFlag = 'ethereum',
defaultWalletFlag,
}: {
silent?: boolean;
interval?: number;
timeout?: number;
walletFlag?: string;
isSingleWalletFlag?: boolean;
injectFlag: string;
defaultWalletFlag?: string;
}): Promise<T> {
}: DetectProviderConfig): Promise<T> {
return new Promise((resolve, reject) => {
async function handleEthereum() {
let provider = (await getProvider(injectFlag, interval, timeout)) as T;
Expand Down
33 changes: 12 additions & 21 deletions core/base/src/emitter/RPCMethod.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import type { DetectProviderConfig } from '../detect';

export type CustomDetectConfig = Omit<DetectProviderConfig, 'walletFlag' | 'isSingleWalletFlag' | 'injectFlag' | 'defaultWalletFlag'>;

abstract class RPCMethod {
abstract sessionKey: string;
abstract injectFlag: string;
abstract provider: any;
account!: string;
abstract detectProvider(): Promise<any>;
abstract detectProvider(config?: CustomDetectConfig): Promise<any>;
abstract requestAccounts(): Promise<Array<string>>;
abstract getAccounts(): Promise<Array<string>>;
abstract getChainId(): Promise<string>;
Expand All @@ -21,32 +25,19 @@ abstract class RPCMethod {
crossNetworkChain?: string;
retryLimit = 2;
detectTimeout = 1500;
subTimeout = 1000;
subInterval = 100;

detectAndSetProvider = () => {
const p = this.detectProvider();
detectAndSetProvider = (config?: CustomDetectConfig) => {
const p = this.detectProvider(config);
p.then((provider) => (this.provider = provider)).catch((err) => console.warn(err));
return p;
};

subProvider = () => {
if (this.provider) return Promise.resolve(this.provider);
return new Promise((resolve, reject) => {
let hasValue = false;
let value = window[this.injectFlag as any] as any;
Object.defineProperty(window, this.injectFlag, {
get: () => value,
set: (provider: any) => {
value = provider;
if (this.provider || hasValue) {
reject();
return;
}
if (value) {
hasValue = true;
this.detectAndSetProvider().then(resolve, reject);
}
},
});
return this.detectAndSetProvider({
timeout: this.subTimeout,
interval: this.subInterval,
});
};

Expand Down

0 comments on commit faddf0a

Please sign in to comment.