Make more changes

- make setter a method
- lazily create promise
This commit is contained in:
RMidhunSuresh 2022-02-04 16:23:39 +05:30
parent 3d8b9cce41
commit 32af7e6f09

View File

@ -31,7 +31,7 @@ class Request implements IHomeServerRequest {
private responseCodeReject: (result: any) => void; private responseCodeReject: (result: any) => void;
private _requestResult?: IHomeServerRequest; private _requestResult?: IHomeServerRequest;
private readonly _responsePromise: Promise<any>; private readonly _responsePromise: Promise<any>;
private readonly _responseCodePromise: Promise<any>; private _responseCodePromise: Promise<any>;
constructor(methodName: string, args: any[]) { constructor(methodName: string, args: any[]) {
this.methodName = methodName; this.methodName = methodName;
@ -40,10 +40,6 @@ class Request implements IHomeServerRequest {
this.responseResolve = resolve; this.responseResolve = resolve;
this.responseReject = reject; this.responseReject = reject;
}); });
this._responseCodePromise = new Promise((resolve, reject) => {
this.responseCodeResolve = resolve;
this.responseCodeReject = reject;
});
} }
abort(): void { abort(): void {
@ -60,13 +56,22 @@ class Request implements IHomeServerRequest {
} }
responseCode(): Promise<number> { responseCode(): Promise<number> {
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; return this._responseCodePromise;
} }
set requestResult(result) { setRequestResult(result) {
this._requestResult = result; this._requestResult = result;
this._requestResult?.response().then(response => this.responseResolve(response)); 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() { get requestResult() {
@ -135,7 +140,7 @@ export class RequestScheduler {
request.methodName request.methodName
].apply(this._hsApi, request.args); ].apply(this._hsApi, request.args);
// so the request can be aborted // so the request can be aborted
request.requestResult = requestResult; request.setRequestResult(requestResult);
return; return;
} catch (err) { } catch (err) {
if ( if (