From 32af7e6f0980b8c9c853dce8e5a665cdade243e4 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Fri, 4 Feb 2022 16:23:39 +0530 Subject: [PATCH] Make more changes - make setter a method - lazily create promise --- src/matrix/net/RequestScheduler.ts | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/matrix/net/RequestScheduler.ts b/src/matrix/net/RequestScheduler.ts index e2118c5e..cee4142f 100644 --- a/src/matrix/net/RequestScheduler.ts +++ b/src/matrix/net/RequestScheduler.ts @@ -31,7 +31,7 @@ class Request implements IHomeServerRequest { private responseCodeReject: (result: any) => void; private _requestResult?: IHomeServerRequest; private readonly _responsePromise: Promise; - private readonly _responseCodePromise: Promise; + private _responseCodePromise: Promise; constructor(methodName: string, args: any[]) { this.methodName = methodName; @@ -40,10 +40,6 @@ class Request implements IHomeServerRequest { this.responseResolve = resolve; this.responseReject = reject; }); - this._responseCodePromise = new Promise((resolve, reject) => { - this.responseCodeResolve = resolve; - this.responseCodeReject = reject; - }); } abort(): void { @@ -60,13 +56,22 @@ class Request implements IHomeServerRequest { } responseCode(): Promise { + if (this.requestResult) { + return this.requestResult.responseCode(); + } + if (!this._responseCodePromise) { + this._responseCodePromise = new Promise((resolve, reject) => { + this.responseCodeResolve = resolve; + this.responseCodeReject = reject; + }); + } return this._responseCodePromise; } - set requestResult(result) { + setRequestResult(result) { this._requestResult = result; this._requestResult?.response().then(response => this.responseResolve(response)); - this._requestResult?.responseCode().then(response => this.responseCodeResolve(response)); + this._requestResult?.responseCode().then(response => this.responseCodeResolve?.(response)); } get requestResult() { @@ -135,7 +140,7 @@ export class RequestScheduler { request.methodName ].apply(this._hsApi, request.args); // so the request can be aborted - request.requestResult = requestResult; + request.setRequestResult(requestResult); return; } catch (err) { if (