pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame^] | 1 | import { EventSchedulerDatabase } from './database'; |
| 2 | import client from './client.js'; |
| 3 | |
| 4 | class 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 | |
| 37 | export default EventScheduler; |