UNPKG

4.92 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const CLDebug_1 = require("../CLDebug");
5const MESSAGE_TIMEOUT = 10000;
6class InputQueue {
7 static AddInput(botState, request, conversationReference, callback) {
8 return tslib_1.__awaiter(this, void 0, void 0, function* () {
9 if (!request.id) {
10 return null;
11 }
12 // Add to queue
13 yield InputQueue.InputQueueAdd(request.id, callback);
14 // Process queue
15 yield InputQueue.InputQueueProcess(botState);
16 });
17 }
18 // Add message to queue
19 static InputQueueAdd(conversationId, callback) {
20 return tslib_1.__awaiter(this, void 0, void 0, function* () {
21 const now = new Date().getTime();
22 const queuedInput = {
23 conversationId: conversationId,
24 timestamp: now,
25 callback: callback
26 };
27 this.messageQueue.push(queuedInput);
28 CLDebug_1.CLDebug.Log(`ADD QUEUE: ${conversationId} ${this.messageQueue.length}`, CLDebug_1.DebugType.MessageQueue);
29 });
30 }
31 // Attempt to process next message in the queue
32 static InputQueueProcess(botState) {
33 return tslib_1.__awaiter(this, void 0, void 0, function* () {
34 const now = new Date().getTime();
35 const messageProcessing = yield botState.GetMessageProcessing();
36 // Is a message being processed
37 if (messageProcessing) {
38 // Remove if it's been expired
39 const age = now - messageProcessing.timestamp;
40 if (age > MESSAGE_TIMEOUT) {
41 CLDebug_1.CLDebug.Log(`EXPIRED: ${messageProcessing.conversationId} ${this.messageQueue.length}`, CLDebug_1.DebugType.MessageQueue);
42 yield botState.MessageProcessingPopAsync();
43 let queuedInput = this.messageQueue.find(mq => mq.conversationId == messageProcessing.conversationId);
44 if (queuedInput) {
45 // Fire the callback with failure
46 queuedInput.callback(true, queuedInput.conversationId);
47 }
48 else {
49 CLDebug_1.CLDebug.Log(`EXPIRE-WARNING: Couldn't find queud message`, CLDebug_1.DebugType.MessageQueue);
50 }
51 CLDebug_1.CLDebug.Log(`EXPIRE-POP: ${messageProcessing.conversationId} ${this.messageQueue.length}`, CLDebug_1.DebugType.MessageQueue);
52 }
53 }
54 // If no message being processed, try next message
55 if (!messageProcessing) {
56 yield InputQueue.InputQueueProcessNext(botState);
57 }
58 });
59 }
60 // Process next message
61 static InputQueueProcessNext(botState) {
62 return tslib_1.__awaiter(this, void 0, void 0, function* () {
63 let curProcessing = yield botState.GetMessageProcessing();
64 // If no message being process, and item in queue, process teh next one
65 if (!curProcessing && this.messageQueue.length > 0) {
66 let messageProcessing = this.messageQueue.shift();
67 if (messageProcessing) {
68 yield botState.SetMessageProcessing(messageProcessing);
69 // Fire the callback with success
70 CLDebug_1.CLDebug.Log(`PROCESS-CALLBACK: ${messageProcessing.conversationId} ${this.messageQueue.length}`, CLDebug_1.DebugType.MessageQueue);
71 messageProcessing.callback(false, messageProcessing.conversationId);
72 }
73 else {
74 CLDebug_1.CLDebug.Log(`PROCESS-ERR: No Message`, CLDebug_1.DebugType.MessageQueue);
75 }
76 }
77 else {
78 CLDebug_1.CLDebug.Log(`PROCESS-NEXT: Empty`, CLDebug_1.DebugType.MessageQueue);
79 }
80 });
81 }
82 // Done processing message, remove from queue
83 static MessageHandled(botState, conversationId) {
84 return tslib_1.__awaiter(this, void 0, void 0, function* () {
85 if (!conversationId) {
86 CLDebug_1.CLDebug.Log(`HANDLE: Missing conversation id`, CLDebug_1.DebugType.MessageQueue);
87 }
88 let messageProcessing = yield botState.MessageProcessingPopAsync();
89 // Check for consistency
90 if (messageProcessing && messageProcessing.conversationId === conversationId) {
91 CLDebug_1.CLDebug.Log(`HANDLE-POP: ${messageProcessing.conversationId} ${this.messageQueue.length}`, CLDebug_1.DebugType.MessageQueue);
92 }
93 // Process next message in the queue
94 yield InputQueue.InputQueueProcessNext(botState);
95 });
96 }
97}
98InputQueue.messageQueue = [];
99exports.InputQueue = InputQueue;
100//# sourceMappingURL=InputQueue.js.map
\No newline at end of file