-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse.js
More file actions
136 lines (109 loc) · 2.96 KB
/
response.js
File metadata and controls
136 lines (109 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// response.js
const Task = require('./task');
const AvailableTask = require('./availableTask');
class Response {
constructor(message) {
this.message = message;
this.stream = null;
this._isHtml = false;
this.availableTasks = [];
this.updateLastMessage = false;
this.nextTasks = [];
this.isVirtual = false;
this.meta = {}
}
getFullMessage() {
return this.message;
}
setStream(stream) {
this.stream = stream;
}
isStream() {
return this.stream != null;
}
async *getStream() {
if (this.stream) {
for await (const chunk of this.stream) {
this.message += chunk;
yield chunk;
}
this.isComplete = true;
} else {
yield this.message;
this.isComplete = true;
}
}
isStreamComplete() {
return this.isComplete;
}
setAsHtml() {
this._isHtml = true;
}
isHtml() {
return this._isHtml;
}
addAvailableTask(availableTask) {
this.availableTasks.push(availableTask);
}
getAvailableTasks() {
return this.availableTasks;
}
hasAvailableTasks() {
return this.availableTasks.length > 0;
}
setUpdateLastMessage(flag) {
this.updateLastMessage = flag;
}
shouldUpdateLastMessage() {
return this.updateLastMessage;
}
addNextTask(task) {
this.nextTasks.push(task);
}
getNextTasks() {
return this.nextTasks;
}
hasNextTasks() {
return this.nextTasks.length > 0;
}
setMeta(meta) {
this.meta = { ...this.meta, ...meta };
}
getMeta() {
return this.meta;
}
static fromJSON(config) {
const response = new Response(config.message || '');
if (config.isHtml) {
response.setAsHtml();
}
if (config.stream) {
response.setStream(config.stream);
}
if (config.meta) {
response.meta = config.meta;
}
if (config.updateLastMessage) {
response.setUpdateLastMessage(config.updateLastMessage);
}
if (config.availableTasks && Array.isArray(config.availableTasks)) {
config.availableTasks.forEach(taskConfig => {
const task = new Task(taskConfig.task);
const availableTask = new AvailableTask(
taskConfig.name,
task,
taskConfig.control || null,
taskConfig.options || null
);
response.addAvailableTask(availableTask);
});
}
if (config.nextTasks && Array.isArray(config.nextTasks)) {
config.nextTasks.forEach(taskConfig => {
response.addNextTask(new Task(taskConfig));
});
}
return response;
}
}
module.exports = Response;