/*! * SAPUI5 * Copyright (c) 2025 SAP SE or an SAP affiliate company. All rights reserved. */ import { Request } from "./request"; class ResponseListener { requestExecutor: RequestExecutor; resolve: (response: Response) => void; reject: (error: unknown) => void; promise: Promise; constructor(requestExecutor: RequestExecutor) { this.requestExecutor = requestExecutor; this.promise = new Promise((_resolve, _reject) => { this.resolve = _resolve; this.reject = _reject; }); } notify() { if (this.requestExecutor.status !== RequestExecutorStatus.COMPLETED) { throw "program error"; } if (!this.requestExecutor.executionResult.hasError) { // success this.resolve(this.requestExecutor.executionResult.response); } else { // error this.reject(this.requestExecutor.executionResult.error); } } } export enum RequestExecutorStatus { INITIAL = "INITIAL", PENDING = "PENDING", COMPLETED = "COMPLETED", // may be successful or with errors } interface ExecutionResult { error?: unknown; response?: Response; hasError?: boolean; } export class RequestExecutor { request: Request; copiedRequest: Request; time: number; status: RequestExecutorStatus = RequestExecutorStatus.INITIAL; executionResult: ExecutionResult; responseListeners: Array> = []; constructor(request: Request) { this.request = request; this.copiedRequest = request.clone(); } public getRequest(): Request { return this.copiedRequest; } public delete() { this.clearResponseListeners(); } public createResponseListener(): Promise { const responseListener = new ResponseListener(this); if (this.status === RequestExecutorStatus.COMPLETED) { responseListener.notify(); } else { this.responseListeners.push(responseListener); } return responseListener.promise; } public clearResponseListeners() { this.responseListeners = []; } public execute() { this.time = new Date().getTime(); this.status = RequestExecutorStatus.PENDING; if (this.executionResult) { throw "program error"; } this.request.execute().then( (response: Response) => { // success this.status = RequestExecutorStatus.COMPLETED; this.executionResult = { hasError: false, response: response }; this.notifyResponseListeners(); }, (error) => { // error this.status = RequestExecutorStatus.COMPLETED; this.executionResult = { hasError: true, error: error }; this.notifyResponseListeners(); } ); } private notifyResponseListeners() { for (const responseListener of this.responseListeners) { responseListener.notify(); } this.clearResponseListeners(); } }