1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | const CLDebug_1 = require("../CLDebug");
|
5 | const MESSAGE_TIMEOUT = 10000;
|
6 | class 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 |
|
13 | yield InputQueue.InputQueueAdd(request.id, callback);
|
14 |
|
15 | yield InputQueue.InputQueueProcess(botState);
|
16 | });
|
17 | }
|
18 |
|
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 |
|
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 |
|
37 | if (messageProcessing) {
|
38 |
|
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 |
|
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 |
|
55 | if (!messageProcessing) {
|
56 | yield InputQueue.InputQueueProcessNext(botState);
|
57 | }
|
58 | });
|
59 | }
|
60 |
|
61 | static InputQueueProcessNext(botState) {
|
62 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
63 | let curProcessing = yield botState.GetMessageProcessing();
|
64 |
|
65 | if (!curProcessing && this.messageQueue.length > 0) {
|
66 | let messageProcessing = this.messageQueue.shift();
|
67 | if (messageProcessing) {
|
68 | yield botState.SetMessageProcessing(messageProcessing);
|
69 |
|
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 |
|
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 |
|
90 | if (messageProcessing && messageProcessing.conversationId === conversationId) {
|
91 | CLDebug_1.CLDebug.Log(`HANDLE-POP: ${messageProcessing.conversationId} ${this.messageQueue.length}`, CLDebug_1.DebugType.MessageQueue);
|
92 | }
|
93 |
|
94 | yield InputQueue.InputQueueProcessNext(botState);
|
95 | });
|
96 | }
|
97 | }
|
98 | InputQueue.messageQueue = [];
|
99 | exports.InputQueue = InputQueue;
|
100 |
|
\ | No newline at end of file |