78 lines
No EOL
3.3 KiB
JavaScript
78 lines
No EOL
3.3 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.PayloadAssemblerManager = void 0;
|
|
const payloadAssembler_1 = require("../assemblers/payloadAssembler");
|
|
const payloadTypes_1 = require("./payloadTypes");
|
|
/**
|
|
* Orchestrates assembly of payloads.
|
|
*/
|
|
class PayloadAssemblerManager {
|
|
/**
|
|
* Initializes a new instance of the [PayloadAssemblerManager](xref:botframework-streaming.PayloadAssemblerManager) class.
|
|
*
|
|
* @param streamManager The [StreamManager](xref:botframework-streaming.StreamManager) managing the stream being assembled.
|
|
* @param onReceiveResponse Function that executes when new bytes are received on a `response` stream.
|
|
* @param onReceiveRequest Function that executes when new bytes are received on a `request` stream.
|
|
*/
|
|
constructor(streamManager, onReceiveResponse, onReceiveRequest) {
|
|
this.streamManager = streamManager;
|
|
this.onReceiveResponse = onReceiveResponse;
|
|
this.onReceiveRequest = onReceiveRequest;
|
|
this.activeAssemblers = {};
|
|
}
|
|
/**
|
|
* Retrieves the assembler's payload as a stream.
|
|
*
|
|
* @param header The Header of the Stream to retrieve.
|
|
* @returns A [SubscribableStream](xref:botframework-streaming.SubscribableStream) of the assembler's payload.
|
|
*/
|
|
getPayloadStream(header) {
|
|
if (header.payloadType === payloadTypes_1.PayloadTypes.stream) {
|
|
return this.streamManager.getPayloadStream(header);
|
|
}
|
|
else if (!this.activeAssemblers[header.id]) {
|
|
const assembler = this.createPayloadAssembler(header);
|
|
if (assembler) {
|
|
this.activeAssemblers[header.id] = assembler;
|
|
return assembler.getPayloadStream();
|
|
}
|
|
}
|
|
}
|
|
/**
|
|
* The action the assembler executes when new bytes are received on the incoming stream.
|
|
*
|
|
* @param header The stream's Header.
|
|
* @param contentStream The incoming stream being assembled.
|
|
* @param contentLength The length of the stream, if finite.
|
|
*/
|
|
onReceive(header, contentStream, contentLength) {
|
|
if (header.payloadType === payloadTypes_1.PayloadTypes.stream) {
|
|
this.streamManager.onReceive(header, contentStream, contentLength);
|
|
}
|
|
else {
|
|
if (this.activeAssemblers && this.activeAssemblers[header.id]) {
|
|
const assembler = this.activeAssemblers[header.id];
|
|
assembler.onReceive(header, contentStream, contentLength);
|
|
}
|
|
if (header.end) {
|
|
delete this.activeAssemblers[header.id];
|
|
}
|
|
}
|
|
}
|
|
createPayloadAssembler(header) {
|
|
if (header.payloadType === payloadTypes_1.PayloadTypes.request) {
|
|
return new payloadAssembler_1.PayloadAssembler(this.streamManager, {
|
|
header,
|
|
onCompleted: this.onReceiveRequest,
|
|
});
|
|
}
|
|
else if (header.payloadType === payloadTypes_1.PayloadTypes.response) {
|
|
return new payloadAssembler_1.PayloadAssembler(this.streamManager, {
|
|
header,
|
|
onCompleted: this.onReceiveResponse,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
exports.PayloadAssemblerManager = PayloadAssemblerManager;
|
|
//# sourceMappingURL=payloadAssemblerManager.js.map
|