Skip to content

Commit

Permalink
feat: support custom requestID generation
Browse files Browse the repository at this point in the history
This feature is to support generating requestID in either a string or
numeric format. Additionally this will support multiple clients
operating against the same context.

The goal here is to allow systems like client generator to
pass through this option to other downstream operators

fixes #277 , fixes #279
  • Loading branch information
zcstarr committed May 28, 2021
1 parent b8bf358 commit e512bd5
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/RequestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ export interface IRequestEvents {
"error": (err: JSONRPCError) => void;
"notification": (data: any) => void;
}
export type RequestID = string | number;

export type INextRequestID = () => RequestID;
export const defaultNextRequest = () => {
let lastId = -1;
return () => ++lastId;
}
/*
** Naive Request Manager, only use 1st transport.
* A more complex request manager could try each transport.
Expand All @@ -25,12 +32,14 @@ class RequestManager {
private requests: any;
private batchStarted: boolean = false;
private lastId: number = -1;
private nextID: INextRequestID;

constructor(transports: Transport[]) {
constructor(transports: Transport[], nextID:INextRequestID = defaultNextRequest()) {
this.transports = transports;
this.requests = {};
this.connectPromise = this.connect();
this.requestChannel = new EventEmitter();
this.nextID = nextID;
}

public connect(): Promise<any> {
Expand All @@ -45,7 +54,7 @@ class RequestManager {
}

public async request(requestObject: JSONRPCMessage, notification: boolean = false, timeout?: number | null): Promise<any> {
const internalID = (++this.lastId).toString();
const internalID = this.nextID().toString();
const id = notification ? null : internalID;
// naively grab first transport and use it
const payload = {request: this.makeRequest(requestObject.method, requestObject.params || [], id) , internalID};
Expand Down

0 comments on commit e512bd5

Please sign in to comment.