File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -109,6 +109,7 @@ const main = async () => {
109109
110110main ().then (() => {
111111 console .log (" DONE" );
112+ client .close ();
112113});
113114
114115```
Original file line number Diff line number Diff line change 11import RequestManager from "./RequestManager" ;
22import { JSONRPCError } from "./Error" ;
33import { 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
92100export default Client ;
Original file line number Diff line number Diff line change 1+ import { IJSONRPCNotification } from "./Request" ;
2+
13interface Arguments {
24 readonly method : string ;
35 readonly params ?: readonly unknown [ ] | object ;
@@ -12,4 +14,6 @@ export type JSONRPCMessage = RequestArguments | NotificationArguments;
1214export 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}
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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 ) ;
You can’t perform that action at this time.
0 commit comments