-
Notifications
You must be signed in to change notification settings - Fork 0
/
CardCache.js
84 lines (76 loc) · 1.95 KB
/
CardCache.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
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
const fs = require('fs')
class CardCache {
constructor(filename = './cards.cache') {
this.filename = filename
this.dataReady = false
this.data = {}
this._readData()
}
async getCardInfo(cardName) {
await this._awaitData()
const normalizedName = cardName.toLowerCase()
if (!this.data[normalizedName] || this._tooOld(this.data[normalizedName].time)) {
return null
}
return this.data[normalizedName].data
}
async setCardInfo(cardName, cardInfo) {
await this._awaitData()
const normalizedName = cardName.toLowerCase()
this.data[normalizedName] = {
time: new Date().getTime(),
data: cardInfo,
}
await this._writeData()
}
async _writeData() {
await new Promise((resolve, reject) => fs.writeFile(this.filename, JSON.stringify(this.data), err => {
if (err) {
console.log(`Problem writing card cache file ${this.filename}: ${err.message}`)
reject(err)
}
resolve()
}))
}
_readData() {
new Promise((resolve, reject) => {
fs.access(this.filename, (err) => {
if (err) {
reject(err)
}
fs.readFile(this.filename, (err, data) => {
if (err) {
reject(err)
}
resolve(data)
})
})
})
.then(data => {
this.dataReady = true
this.data = JSON.parse(data)
})
.catch((err) => {
this.dataReady = true
console.log(`Could not read cache file ${this.filename}. Starting with empty cache`)
})
}
async _awaitData() {
if (this.dataReady) {
return
}
await new Promise((resolve, reject) => {
const iv = setInterval(() => {
if (this.dataReady) {
clearInterval(iv)
resolve()
}
}, 10)
})
}
_tooOld(time) {
//return (new Date().getTime() + 24 * 3600 * 1000) < time
return time + 24 * 3600 * 1000 < new Date().getTime()
}
}
module.exports = CardCache