Skip to content

Commit

Permalink
Merge pull request #220 from open-rpc/feat/add-close
Browse files Browse the repository at this point in the history
feat: add `close` method Client to close connection and cleanup event listeners
  • Loading branch information
shanejonas authored Oct 19, 2020
2 parents 3048656 + 0139391 commit 2986b68
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ const main = async () => {

main().then(() => {
console.log("DONE");
client.close();
});

```
Expand Down
10 changes: 9 additions & 1 deletion src/Client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import RequestManager from "./RequestManager";
import { JSONRPCError } from "./Error";
import { IClient, RequestArguments, NotificationArguments } from "./ClientInterface";
import { IJSONRPCNotification } from "./Request";

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

public onNotification(callback: (data: any) => void) {
public onNotification(callback: (data: IJSONRPCNotification) => void) {
this.requestManager.requestChannel.addListener("notification", callback);
}

public onError(callback: (data: JSONRPCError) => void) {
this.requestManager.requestChannel.addListener("error", callback);
}

/**
* Close connection
*/
public close() {
this.requestManager.close();
}
}

export default Client;
4 changes: 4 additions & 0 deletions src/ClientInterface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { IJSONRPCNotification } from "./Request";

interface Arguments {
readonly method: string;
readonly params?: readonly unknown[] | object;
Expand All @@ -12,4 +14,6 @@ export type JSONRPCMessage = RequestArguments | NotificationArguments;
export interface IClient {
request(args: RequestArguments): Promise<unknown>;
notify(args: NotificationArguments): Promise<unknown>;
close(): void;
onNotification(callback: (data: IJSONRPCNotification) => void): void;
}
2 changes: 2 additions & 0 deletions src/RequestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ class RequestManager {
}

public close(): void {
this.requestChannel.removeAllListeners();
this.transports.forEach((transport) => {
transport.unsubscribe();
transport.close();
});
}
Expand Down
7 changes: 7 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,11 @@ describe("client-js", () => {
c.stopBatch();
});

describe("can close", () => {
const emitter = new EventEmitter();
const rm = new RequestManager([new EventEmitterTransport(emitter, "from1", "to1")]);
const c = new Client(rm);
c.close();
});

});
8 changes: 8 additions & 0 deletions src/transports/Transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ export abstract class Transport {
public subscribe(event: TransportEventName, handler: ITransportEvents[TransportEventName]) {
this.transportRequestManager.transportEventChannel.addListener(event, handler);
}
public unsubscribe(event?: TransportEventName, handler?: ITransportEvents[TransportEventName]) {
if (!event) {
return this.transportRequestManager.transportEventChannel.removeAllListeners();
}
if (event && handler) {
this.transportRequestManager.transportEventChannel.removeListener(event, handler);
}
}
protected parseData(data: JSONRPCRequestData) {
if (data instanceof Array) {
return data.map((batch) => batch.request.request);
Expand Down

0 comments on commit 2986b68

Please sign in to comment.