feat: pass provider instance to RPCSession constructor in RPCClient and RPCServer

This commit is contained in:
tone
2025-11-15 12:15:50 +08:00
parent bb85abd77e
commit 821379f44f
4 changed files with 12 additions and 0 deletions

View File

@@ -85,6 +85,7 @@ export class RPCClient {
resolve(new RPCSession(
new RPCConnection(connection!),
this.rpcHandler,
this,
));
} else {
reject(new Error('Server rejected handshake request'));

View File

@@ -4,6 +4,7 @@ import { RPCPacket } from "./RPCPacket";
import { makeCallPacket, makeCallResponsePacket, parseCallPacket, parseCallResponsePacket } from "./RPCCommon";
import { RPCProvider } from "./RPCProvider";
import { RPCError, RPCErrorCode } from "./RPCError";
import { RPCSession } from "./RPCSession";
interface RPCConnectionEvents {
call: RPCPacket;
@@ -29,6 +30,7 @@ export class RPCConnection extends EventEmitter<RPCConnectionEvents> {
closed: boolean = false;
private callResponseEmitter = new CallResponseEmitter();
private rpcSession!: RPCSession;
constructor(public socket: SocketConnection) {
super();
@@ -75,6 +77,10 @@ export class RPCConnection extends EventEmitter<RPCConnectionEvents> {
})
}
public setRPCSession(session: RPCSession) {
this.rpcSession = session;
}
/** @throws */
public async callRequest(options: {
fnPath: string;

View File

@@ -78,6 +78,7 @@ export class RPCServer extends EventEmitter<RPCServerEvents> {
this.emit('connect', new RPCSession(
new RPCConnection(socketConnection),
this.rpcHandler,
this,
));
}

View File

@@ -2,6 +2,8 @@ import { ToDeepPromise } from "@/utils/utils";
import { RPCConnection } from "./RPCConnection";
import { RPCHandler } from "./RPCHandler";
import { RPCProvider } from "./RPCProvider";
import { RPCClient } from "./RPCClient";
import { RPCServer } from "./RPCServer";
export class RPCSession {
@@ -9,7 +11,9 @@ export class RPCSession {
constructor(
public readonly connection: RPCConnection,
public readonly rpcHandler: RPCHandler,
public readonly provider: RPCClient | RPCServer,
) {
connection.setRPCSession(this);
connection.onCallRequest(rpcHandler.getProvider.bind(rpcHandler));
}