-
Notifications
You must be signed in to change notification settings - Fork 0
/
version_cacher.js
43 lines (41 loc) · 1.19 KB
/
version_cacher.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
35
36
37
38
39
40
41
42
43
const r = require("./rethink_connection")
class VersionCacher {
constructor() {
this.versions = []
this.update()
}
async update() {
let all = await r.table("versions").coerceTo("array").run()
all = all.sort((a, b) => {
return b.release_id - a.release_id
})
this.versions = all
r.table("versions").changes().run((err, cursor) => {
if (err) {
console.error(err)
process.exit(1)
}
cursor.each((err, row) => {
if (err) {
throw err
}
if (!row.old_val && row.new_val) {
const newVal = row.new_val
this.versions.push(newVal)
}
})
})
}
getLatest() {
const len = this.versions.length
let stable, beta
for (let i = 0; i < len; i++) {
const item = this.versions[i]
if (stable && beta) break
if (item.beta && !beta) beta = item
if (!item.beta && !stable) stable = item
}
return {stable, beta}
}
}
module.exports = new VersionCacher()