blob: 9210883ced4cc5a74c9dfac86a4afe0d4b7b4231 [file] [log] [blame]
pineafan4edb7762022-06-26 19:21:04 +01001import { EventSchedulerDatabase } from './database';
2import client from './client.js';
3
4class EventScheduler {
5 database: any;
6 next: {timestamp: Date, data: object, responded: boolean} | {};
7
8 constructor() {
9 this.database = EventSchedulerDatabase;
10 this.next = {};
11 }
12
13 async create(timestamp: Date, data: object) {
14 await this.database.create(timestamp, data);
15 if (this.next === {}) {
16 this.next = this.next = await this.getNext();
17 return
18 }
19 if (timestamp.getTime() < (this.next as {timestamp: Date}).timestamp.getTime()) {
20 this.next = {timestamp: timestamp, data: data, responded: false};
21 }
22 }
23
24 async getNext() {
25 let entry = await this.database.getNext();
26 if (entry) {
27 this.next = entry;
28 }
29 return this.next;
30 }
31
32 async delete(timestamp: Date, data: object) {
33 await this.database.delete(timestamp, data);
34 } // TODO: add a loop
35}
36
37export default EventScheduler;