From 2cfc650d9c11dfbe2c32208d9413fd19473b9c03 Mon Sep 17 00:00:00 2001 From: Zane Starr Date: Fri, 28 May 2021 08:52:54 -0700 Subject: [PATCH] feat: support custom requestID generation 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 --- src/RequestManager.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/RequestManager.ts b/src/RequestManager.ts index 7b2aa5d..3fcd44b 100644 --- a/src/RequestManager.ts +++ b/src/RequestManager.ts @@ -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. @@ -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 { @@ -45,7 +54,7 @@ class RequestManager { } public async request(requestObject: JSONRPCMessage, notification: boolean = false, timeout?: number | null): Promise { - 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};