84 lines
No EOL
3.1 KiB
JavaScript
84 lines
No EOL
3.1 KiB
JavaScript
"use strict";
|
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
});
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.RequestManager = void 0;
|
|
/**
|
|
* A streaming pending request.
|
|
*/
|
|
class PendingRequest {
|
|
}
|
|
/**
|
|
* Orchestrates and manages pending streaming requests.
|
|
*/
|
|
class RequestManager {
|
|
constructor() {
|
|
this._pendingRequests = {};
|
|
}
|
|
/**
|
|
* Gets the count of the pending requests.
|
|
*
|
|
* @returns Number with the pending requests count.
|
|
*/
|
|
pendingRequestCount() {
|
|
return Object.keys(this._pendingRequests).length;
|
|
}
|
|
/**
|
|
* Signal fired when all response tasks have completed.
|
|
*
|
|
* @param requestId The ID of the StreamingRequest.
|
|
* @param response The [IReceiveResponse](xref:botframework-streaming.IReceiveResponse) in response to the request.
|
|
* @returns A Promise that when completed returns `true` if the `requestId`'s pending response task was completed, otherwise `false`.
|
|
*/
|
|
signalResponse(requestId, response) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
const pendingRequest = this._pendingRequests[requestId];
|
|
if (pendingRequest) {
|
|
pendingRequest.resolve(response);
|
|
delete this._pendingRequests[requestId];
|
|
return true;
|
|
}
|
|
return false;
|
|
});
|
|
}
|
|
/**
|
|
* Constructs and returns a response for this request.
|
|
*
|
|
* @param requestId The ID of the StreamingRequest being responded to.
|
|
* @returns The response to the specified request.
|
|
*/
|
|
getResponse(requestId) {
|
|
let pendingRequest = this._pendingRequests[requestId];
|
|
if (pendingRequest) {
|
|
return Promise.reject(`requestId '${requestId}' already exists in RequestManager`);
|
|
}
|
|
pendingRequest = new PendingRequest();
|
|
pendingRequest.requestId = requestId;
|
|
const promise = new Promise((resolve, reject) => {
|
|
pendingRequest.resolve = resolve;
|
|
pendingRequest.reject = reject;
|
|
});
|
|
this._pendingRequests[requestId] = pendingRequest;
|
|
return promise;
|
|
}
|
|
/**
|
|
* Rejects all requests pending a response.
|
|
*
|
|
* @param reason The reason for rejection.
|
|
*/
|
|
rejectAllResponses(reason) {
|
|
Object.entries(this._pendingRequests).forEach(([requestId, { reject }]) => {
|
|
reject(reason);
|
|
delete this._pendingRequests[requestId];
|
|
});
|
|
}
|
|
}
|
|
exports.RequestManager = RequestManager;
|
|
//# sourceMappingURL=requestManager.js.map
|