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

Remove private properties #48

Merged
merged 4 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 5 additions & 9 deletions packages/tools/src/event-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@ import type {
} from '@/types'

export class EventEmitter<T extends EventMap> {
#handlers: EventHandlersMap<T> = {}

get handlers(): EventHandlersMap<T> {
return this.#handlers
}
handlers: EventHandlersMap<T> = {}

public on<K extends EventMapKey<T>>(key: K, fn: EventHandler<T[K]>): void {
this.#handlers[key] = (this.#handlers[key] || [])?.concat(fn)
this.handlers[key] = (this.handlers[key] || [])?.concat(fn)
}

public once<K extends EventMapKey<T>>(key: K, fn: EventHandler<T[K]>): void {
Expand All @@ -25,16 +21,16 @@ export class EventEmitter<T extends EventMap> {
}

public off<K extends EventMapKey<T>>(key: K, fn: EventHandler<T[K]>): void {
this.#handlers[key] = (this.#handlers[key] || [])?.filter(f => f !== fn)
this.handlers[key] = (this.handlers[key] || [])?.filter(f => f !== fn)
}

public emit<K extends EventMapKey<T>>(key: K, data?: T[K]): void | never {
;(this.#handlers[key] || [])?.forEach((fn: EventHandler<T[K]>) => {
;(this.handlers[key] || [])?.forEach((fn: EventHandler<T[K]>) => {
fn(data)
})
}

public clear(): void {
this.#handlers = {}
this.handlers = {}
}
}
52 changes: 24 additions & 28 deletions packages/w3p/src/provider-detector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ type ProviderDetectorConfig = {
}

export class ProviderDetector<T extends keyof Record<string, string>> {
#providers: ProviderInstance<T>[]
#rawProviders: RawProvider[]
#isInitiated = false
pureProviders: ProviderInstance<T>[]
napalmpapalam marked this conversation as resolved.
Show resolved Hide resolved
rawProviders: RawProvider[]
isInitiated = false

public static errorHandlers?: ErrorHandlersMap
public static cfg: ProviderDetectorConfig

constructor() {
this.#providers = []
this.#rawProviders = []
this.pureProviders = []
this.rawProviders = []

ProviderDetector.setCfg({
isWrapDefaultProviders: true,
Expand All @@ -56,18 +56,14 @@ export class ProviderDetector<T extends keyof Record<string, string>> {
}

public async init(): Promise<ProviderDetector<T>> {
this.#detectRawProviders()
await this.#defineProviders()
this.#isInitiated = true
this.detectRawProviders()
await this.defineProviders()
this.isInitiated = true
return this
}

get isInitiated(): boolean {
return this.#isInitiated
}

public get providers(): Record<PROVIDERS | T, ProviderInstance> {
return this.#providers.reduce((acc, el) => {
return this.pureProviders.reduce((acc, el) => {
const name = el.name.toLowerCase() as PROVIDERS

acc[name] = {
Expand All @@ -79,18 +75,18 @@ export class ProviderDetector<T extends keyof Record<string, string>> {
}

public get isEnabled(): boolean {
return Boolean(this.#providers.length)
return Boolean(this.pureProviders.length)
}
napalmpapalam marked this conversation as resolved.
Show resolved Hide resolved

public getProvider(provider: PROVIDERS | T): ProviderInstance | undefined {
return this.providers[provider]
}

public addProvider(provider: ProviderInstance<T>): void {
this.#providers.push(provider)
this.pureProviders.push(provider)
}

#detectRawProviders(): void {
detectRawProviders(): void {
const ethProviders = window?.ethereum
? window?.ethereum?.providers || [window?.ethereum]
: []
Expand All @@ -104,7 +100,7 @@ export class ProviderDetector<T extends keyof Record<string, string>> {
),
)

this.#rawProviders = [
this.rawProviders = [
...(ProviderDetector.cfg?.isWrapDefaultProviders && proxyEthProviders
? proxyEthProviders
: ethProviders),
Expand All @@ -113,35 +109,35 @@ export class ProviderDetector<T extends keyof Record<string, string>> {
] as RawProvider[]
}

async #defineProviders(): Promise<void> {
if (this.#rawProviders.length) {
this.#designateProviders()
async defineProviders(): Promise<void> {
if (this.rawProviders.length) {
this.designateProviders()
} else {
await sleep(3000)
await this.#detectRawProviders()
this.#designateProviders()
await this.detectRawProviders()
this.designateProviders()
napalmpapalam marked this conversation as resolved.
Show resolved Hide resolved
}
}

#designateProviders(): void {
if (!this.#rawProviders.length) return
designateProviders(): void {
if (!this.rawProviders.length) return

const browserProviders = this.#rawProviders.map(el => {
const browserProviders = this.rawProviders.map(el => {
const appropriatedProviderName: PROVIDERS =
this.#getAppropriateProviderName(el)
this.getAppropriateProviderName(el)

return {
name: appropriatedProviderName,
instance: el,
} as ProviderInstance
})

this.#providers = browserProviders.filter(
this.pureProviders = browserProviders.filter(
(el, idx, arr) => arr.findIndex(sec => sec.name === el.name) === idx,
)
}

#getAppropriateProviderName(provider: RawProvider): PROVIDERS {
getAppropriateProviderName(provider: RawProvider): PROVIDERS {
const providerName = Object.entries(PROVIDER_CHECKS).find(el => {
const [, value] = el

Expand Down
70 changes: 35 additions & 35 deletions packages/w3p/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,40 +39,40 @@ export type CreateProviderOpts<T extends keyof Record<string, string>> = {
* ```
*/
export class Provider implements IProvider {
readonly #proxyConstructor: ProviderProxyConstructor
#selectedProvider?: PROVIDERS
#proxy?: ProviderProxy
readonly proxyConstructor: ProviderProxyConstructor
selectedProvider?: PROVIDERS
proxy?: ProviderProxy

public static chainsDetails?: Record<ChainId, Chain>

constructor(proxyConstructor: ProviderProxyConstructor) {
this.#selectedProvider = undefined
this.#proxy = undefined
this.#proxyConstructor = proxyConstructor
this.selectedProvider = undefined
this.proxy = undefined
napalmpapalam marked this conversation as resolved.
Show resolved Hide resolved
this.proxyConstructor = proxyConstructor
}

public get rawProvider() {
return this.#proxy?.rawProvider
return this.proxy?.rawProvider
}

public get chainType() {
return this.#proxy?.chainType
return this.proxy?.chainType
}

public get providerType() {
return this.#selectedProvider
return this.selectedProvider
}

public get isConnected() {
return Boolean(this.chainId && this.address)
}

public get address() {
return this.#proxy?.address
return this.proxy?.address
}

public get chainId() {
return this.#proxy?.chainId
return this.proxy?.chainId
}

public get chainDetails() {
Expand All @@ -83,40 +83,40 @@ export class Provider implements IProvider {
if (!provider.instance)
throw new errors.ProviderInjectedInstanceNotFoundError()

this.#proxy = new this.#proxyConstructor(provider.instance)
this.proxy = new this.proxyConstructor(provider.instance)

Object.entries(listeners || {}).forEach(([key, value]) => {
this.#proxy?.[key as keyof ProviderListeners]?.(
this.proxy?.[key as keyof ProviderListeners]?.(
value as (e?: ProviderEventPayload) => void,
)
})

this.#selectedProvider = provider.name
await this.#proxy?.init()
this.selectedProvider = provider.name
await this.proxy?.init()
return this
}

public async connect() {
if (!this.#proxy) throw new errors.ProviderNotInitializedError()
if (!this.proxy) throw new errors.ProviderNotInitializedError()

await this.#proxy?.connect?.()
await this.proxy?.connect?.()
}

public async switchChain(chainId: ChainId) {
await this.#proxy?.switchChain?.(chainId)
await this.proxy?.switchChain?.(chainId)
}

public async addChain(chain: Chain) {
await this.#proxy?.addChain?.(chain)
await this.proxy?.addChain?.(chain)
}

public static setChainsDetails(chains: Record<ChainId, Chain>) {
this.chainsDetails = chains
}

public async signAndSendTx(txRequestBody: TxRequestBody) {
if (this.#proxy?.signAndSendTx) {
return this.#proxy?.signAndSendTx?.(
if (this.proxy?.signAndSendTx) {
return this.proxy?.signAndSendTx?.(
txRequestBody,
) as Promise<TransactionResponse>
}
Expand All @@ -125,59 +125,59 @@ export class Provider implements IProvider {
}

public getHashFromTx(txResponse: TransactionResponse) {
return this.#proxy?.getHashFromTx?.(txResponse) ?? ''
return this.proxy?.getHashFromTx?.(txResponse) ?? ''
}

public getTxUrl(chain: Chain, txHash: string) {
return this.#proxy?.getTxUrl?.(chain, txHash) ?? ''
return this.proxy?.getTxUrl?.(chain, txHash) ?? ''
}

public getAddressUrl(chain: Chain, address: string) {
return this.#proxy?.getAddressUrl?.(chain, address) ?? ''
return this.proxy?.getAddressUrl?.(chain, address) ?? ''
}

public async signMessage(message: string) {
return this.#proxy?.signMessage?.(message) ?? ''
return this.proxy?.signMessage?.(message) ?? ''
}

public onAccountChanged(cb: (e?: ProviderEventPayload) => void): void {
this.#proxy?.onAccountChanged(cb)
this.proxy?.onAccountChanged(cb)
}

public onChainChanged(cb: (e?: ProviderEventPayload) => void): void {
this.#proxy?.onChainChanged?.(cb)
this.proxy?.onChainChanged?.(cb)
}

public onConnect(cb: (e?: ProviderEventPayload) => void): void {
this.#proxy?.onConnect(cb)
this.proxy?.onConnect(cb)
}

public onDisconnect(cb: (e?: ProviderEventPayload) => void): void {
this.#proxy?.onDisconnect(cb)
this.proxy?.onDisconnect(cb)
}

public onInitiated(cb: (e?: ProviderEventPayload) => void): void {
this.#proxy?.onInitiated(cb)
this.proxy?.onInitiated(cb)
}

public clearHandlers(): void {
this.#proxy?.clearHandlers()
this.proxy?.clearHandlers()
}

public onBeforeTxSent(cb: (e?: ProviderEventPayload) => void) {
this.#proxy?.onBeforeTxSent(cb)
this.proxy?.onBeforeTxSent(cb)
}

public onTxSent(cb: (e?: ProviderEventPayload) => void) {
this.#proxy?.onTxSent(cb)
this.proxy?.onTxSent(cb)
}

public onTxConfirmed(cb: (e?: ProviderEventPayload) => void) {
this.#proxy?.onTxConfirmed(cb)
this.proxy?.onTxConfirmed(cb)
}

public async disconnect() {
await this.#proxy?.disconnect?.()
await this.proxy?.disconnect?.()
}
}

Expand Down
Loading
Loading