/
event-queue.ts
168 lines (152 loc) · 5.42 KB
/
event-queue.ts
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
Copyright 2020 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
type DataReady = Promise<Record<string, unknown>>;
// It's an event, which has no type yet.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type ConsumeCallback = (error: Error|null, event: any) => void;
/**
* Handles the processing order of incoming Matrix events.
*
* Events can be pushed to the queue and will be processed when their
* corresponding data is ready and they are at the head of line.
* Different types of queues can be chosen for the processing order of events.
*
* Abstract Base Class. Use the factory method `create` to create new instances.
*/
export class EventQueue {
/**
* Private constructor.
*
* @constructor
* @param {"none"|"single"|"per_room"} type The type of event queue to create.
* @param {consumeCallback} consumeFn Function which is called when an event
* is consumed.
*/
private queues: { [identifer: string]: {
events: Array<{ dataReady: DataReady }>;
consuming: boolean;
}; } = {};
constructor(private type: "none"|"single"|"per_room", protected consumeFn: ConsumeCallback) { }
/**
* Push the event and its related data to the queue.
*
* @param {IMatrixEvent} event The event to enqueue.
* @param {Promise<object>} dataReady Promise containing data related to the event.
*/
// eslint-disable-next-line camelcase
public push(event: {room_id: string}, dataReady: DataReady) {
const queue = this.getQueue(event);
queue.events.push({
dataReady: dataReady
});
}
// eslint-disable-next-line camelcase
private getQueue(event: {room_id: string}) {
const identifier = this.type === "per_room" ? event.room_id : "none";
if (!this.queues[identifier]) {
this.queues[identifier] = {
events: [],
consuming: false
};
}
return this.queues[identifier];
}
/**
* Starts consuming the queue.
*
* As long as events are enqueued they will continue to be consumed.
*/
public consume() {
Object.keys(this.queues).forEach((identifier) => {
if (!this.queues[identifier].consuming) {
this.queues[identifier].consuming = true;
this.takeNext(identifier);
}
});
}
private takeNext(identifier: string) {
const events = this.queues[identifier].events;
const entry = events.shift();
if (!entry) {
this.queues[identifier].consuming = false;
return;
}
entry.dataReady.then((r) => this.consumeFn(null, r)).catch((error) => this.consumeFn(error, null));
entry.dataReady.finally(() => this.takeNext(identifier));
}
/**
* Factory for EventQueues.
*
* @param {"none"|"single"|"per_room"} opts.type Type of the queue to create.
* @param {consumeCallback} consumeFn Function which is called when an event
* is consumed.
* @return {EventQueue} The newly created EventQueue.
*/
static create(opts: { type: "none"|"single"|"per_room"}, consumeFn: ConsumeCallback) {
const type = opts.type;
/* eslint-disable @typescript-eslint/no-use-before-define */
if (type == "single") {
return new EventQueueSingle(consumeFn);
}
if (type == "per_room") {
return new EventQueuePerRoom(consumeFn);
}
if (type == "none") {
return new EventQueueNone(consumeFn);
}
/* eslint-enable @typescript-eslint/no-use-before-define */
throw Error(`Invalid EventQueue type '${type}'.`);
}
}
/**
* EventQueue for which all events are enqueued in their order of arrival.
*
* The foremost event is processed as soon as its data is available.
*/
export class EventQueueSingle extends EventQueue {
constructor(consumeFn: ConsumeCallback) {
super("single", consumeFn);
}
}
/**
* EventQueue for which one queue per room is utilized.
*
* Events at the head of line are processed as soon as their data is available.
*/
export class EventQueuePerRoom extends EventQueue {
constructor(consumeFn: ConsumeCallback) {
super("per_room", consumeFn);
}
}
/**
* Dummy EventQueue for which no queue is utilized.
*
* Every event is handled as soon as its data is available.
*/
export class EventQueueNone extends EventQueue {
constructor(consumeFn: ConsumeCallback) {
super("none", consumeFn);
}
push(event: unknown, dataReady: DataReady) {
// consume the event instantly
dataReady.then((r) => this.consumeFn(null, r)).catch((error) => this.consumeFn(error, null));
}
consume() {
// no-op for EventQueueNone
}
}
/**
* @callback consumeCallback
* @param {Error} [err] The error in case the data could not be retrieved.
* @param {object} data The data associated with the consumed event.
*/