Skip to content

Commit

Permalink
logs
Browse files Browse the repository at this point in the history
  • Loading branch information
hazae41 committed Apr 28, 2023
1 parent 26af5c3 commit 34f4533
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
17 changes: 17 additions & 0 deletions src/libs/pools/pools.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Mutex } from "@hazae41/mutex"
import { Pool } from "@hazae41/piscine"
import { useEffect } from "react"

export namespace Pools {

Expand All @@ -13,4 +14,20 @@ export namespace Pools {
return await pool.lock(takeLocked)
}

}

export function usePoolChange<T>(pool: Pool<T> | undefined, callback: (pool: Pool<T>) => void) {
useEffect(() => {
if (!pool) return

const onCreatedOrDeleted = () => callback(pool)

pool.events.addEventListener("created", onCreatedOrDeleted, { passive: true })
pool.events.addEventListener("deleted", onCreatedOrDeleted, { passive: true })

return () => {
pool.events.removeEventListener("created", onCreatedOrDeleted)
pool.events.removeEventListener("deleted", onCreatedOrDeleted)
}
}, [pool, callback])
}
4 changes: 0 additions & 4 deletions src/libs/tor/sessions/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ export namespace EthereumSocketSession {

const client = new Rpc.Client()

console.log("new socket")

const onCloseOrError = () => {
socket.removeEventListener("close", onCloseOrError)
socket.removeEventListener("error", onCloseOrError)
Expand Down Expand Up @@ -117,8 +115,6 @@ export function createEthereumSessionsPool(chains: EthereumChainMap<EthereumChai
circuit.events.addEventListener("close", onCloseOrError, { passive: true })
circuit.events.addEventListener("error", onCloseOrError, { passive: true })

console.log("done", pool.size)

return sessions2
} catch (e: unknown) {
/**
Expand Down
6 changes: 3 additions & 3 deletions src/mods/entities/wallets/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function getBalanceSchema(address: string | undefined, session: EthereumS
const fetcher = async (init: Rpc.RpcRequestInit, more: FetcherMore) => {
const { signal } = more

console.log(session.circuit.id)
console.log(`Fetching ${init.method} with`, session.circuit.id)

const response = await session.client.fetchWithSocket<string>(session.socket, init, signal)

Expand Down Expand Up @@ -109,7 +109,7 @@ export function getNonceSchema(address: string | undefined, session: EthereumSoc
const fetcher = async (init: Rpc.RpcRequestInit, more: FetcherMore) => {
const { signal } = more

console.log(session.circuit.id)
console.log(`Fetching ${init.method} with`, session.circuit.id)

const response = await session.client.fetchWithSocket<string>(session.socket, init, signal)

Expand Down Expand Up @@ -139,7 +139,7 @@ export function getGasPriceSchema(session: EthereumSocketSession | undefined) {
const fetcher = async <T extends unknown[]>(init: Rpc.RpcRequestInit<T>, more: FetcherMore) => {
const { signal } = more

console.log(session.circuit.id)
console.log(`Fetching ${init.method} with`, session.circuit.id)

const response = await session.client.fetchWithSocket<string>(session.socket, init, signal)

Expand Down
9 changes: 8 additions & 1 deletion src/mods/tor/circuits/context.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { usePoolChange } from "@/libs/pools/pools";
import { ChildrenProps } from "@/libs/react/props/children";
import { Circuit, createCircuitPool } from "@hazae41/echalote";
import { Mutex } from "@hazae41/mutex";
import { Pool } from "@hazae41/piscine";
import { createContext, useContext, useMemo } from "react";
import { createContext, useCallback, useContext, useMemo } from "react";
import { useTor } from "../context";

export const CircuitsContext =
Expand All @@ -23,6 +24,12 @@ export function CircuitsProvider(props: ChildrenProps) {
return new Mutex(createCircuitPool(tor, { capacity: 3 }))
}, [tor])

const onPoolChange = useCallback((pool: Pool<Circuit>) => {
console.log(`Circuits pool: ${pool.size}/${pool.capacity}`)
}, [])

usePoolChange(circuits?.inner, onPoolChange)

return <CircuitsContext.Provider value={circuits}>
{children}
</CircuitsContext.Provider>
Expand Down
9 changes: 8 additions & 1 deletion src/mods/tor/sessions/context.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { usePoolChange } from "@/libs/pools/pools";
import { ChildrenProps } from "@/libs/react/props/children";
import { EthereumSessions, createEthereumSessionsPool } from "@/libs/tor/sessions/session";
import { Mutex } from "@hazae41/mutex";
import { Pool } from "@hazae41/piscine";
import { createContext, useContext, useMemo } from "react";
import { createContext, useCallback, useContext, useMemo } from "react";
import { useCircuits } from "../circuits/context";

export const SessionsContext =
Expand Down Expand Up @@ -34,6 +35,12 @@ export function SessionsProvider(props: ChildrenProps) {
}, circuits))
}, [circuits])

const onPoolChange = useCallback((pool: Pool<EthereumSessions>) => {
console.log(`Sessions pool: ${pool.size}/${pool.capacity}`)
}, [])

usePoolChange(sessions?.inner, onPoolChange)

return <SessionsContext.Provider value={sessions}>
{children}
</SessionsContext.Provider>
Expand Down

1 comment on commit 34f4533

@vercel
Copy link

@vercel vercel bot commented on 34f4533 Apr 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.