-
Notifications
You must be signed in to change notification settings - Fork 0
/
interval.js
34 lines (28 loc) · 890 Bytes
/
interval.js
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
const DB = require('./db.js')
const EventEmitter = require('node:events')
class Interval {
#db = DB.instance
constructor() {
this.emitter = new EventEmitter()
this.type = Interval.name
this.startTime = Date.now()
this.uid = require('uid').uid()
const stmt = this.#db.prepare("INSERT INTO intervals (uid, start_time, is_active) VALUES ($uid, $time, TRUE)")
stmt.run({
$uid: this.uid,
$time: this.startTime
})
stmt.finalize()
}
stop() {
this.stopTime = Date.now()
const stmt = this.#db.prepare("UPDATE intervals SET stop_time = $time, is_active = FALSE WHERE uid = $uid")
stmt.run({
$uid: this.uid,
$time: this.stopTime
})
stmt.finalize()
this.emitter.emit('interval-stopped')
}
}
module.exports = Interval