Sitelet https://github.com/open-rpc/client-js/commit/0155c8109edf1ffc6508de3125072afc418de8b4
Skip to content

Commit 0155c81

Browse files
committed
feat: add close method Client to close connection and cleanup event listeners
1 parent 3048656 commit 0155c81

5 files changed

Lines changed: 24 additions & 1 deletion

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ const main = async () => {
109109

110110
main().then(() => {
111111
console.log("DONE");
112+
client.close();
112113
});
113114

114115
```

‎src/Client.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import RequestManager from "./RequestManager";
22
import { JSONRPCError } from "./Error";
33
import { IClient, RequestArguments, NotificationArguments } from "./ClientInterface";
4+
import { IJSONRPCNotification } from "./Request";
45

56
/**
67
* OpenRPC Client JS is a browser-compatible JSON-RPC client with multiple transports and
@@ -80,13 +81,20 @@ class Client implements IClient {
8081
return this.requestManager.request(requestObject, true);
8182
}
8283

83-
public onNotification(callback: (data: any) => void) {
84+
public onNotification(callback: (data: IJSONRPCNotification) => void) {
8485
this.requestManager.requestChannel.addListener("notification", callback);
8586
}
8687

8788
public onError(callback: (data: JSONRPCError) => void) {
8889
this.requestManager.requestChannel.addListener("error", callback);
8990
}
91+
92+
/**
93+
* Close connection
94+
*/
95+
public close() {
96+
this.requestManager.close();
97+
}
9098
}
9199

92100
export default Client;

‎src/ClientInterface.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { IJSONRPCNotification } from "./Request";
2+
13
interface Arguments {
24
readonly method: string;
35
readonly params?: readonly unknown[] | object;
@@ -12,4 +14,6 @@ export type JSONRPCMessage = RequestArguments | NotificationArguments;
1214
export interface IClient {
1315
request(args: RequestArguments): Promise<unknown>;
1416
notify(args: NotificationArguments): Promise<unknown>;
17+
close(): void;
18+
onNotification(callback: (data: IJSONRPCNotification) => void): void;
1519
}

‎src/RequestManager.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ class RequestManager {
5959
}
6060

6161
public close(): void {
62+
this.requestChannel.removeAllListeners();
6263
this.transports.forEach((transport) => {
64+
transport.unsubscribe();
6365
transport.close();
6466
});
6567
}

‎src/transports/Transport.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ export abstract class Transport {
3535
public subscribe(event: TransportEventName, handler: ITransportEvents[TransportEventName]) {
3636
this.transportRequestManager.transportEventChannel.addListener(event, handler);
3737
}
38+
public unsubscribe(event?: TransportEventName, handler?: ITransportEvents[TransportEventName]) {
39+
if (!event) {
40+
return this.transportRequestManager.transportEventChannel.removeAllListeners();
41+
}
42+
if (event && handler) {
43+
this.transportRequestManager.transportEventChannel.removeListener(event, handler);
44+
}
45+
}
3846
protected parseData(data: JSONRPCRequestData) {
3947
if (data instanceof Array) {
4048
return data.map((batch) => batch.request.request);

0 commit comments

Comments
 (0)