-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.js
More file actions
122 lines (106 loc) · 2.39 KB
/
util.js
File metadata and controls
122 lines (106 loc) · 2.39 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import fs from 'fs'
export function convertCryptocompareObject (array) {
let [exchange, primary, secondory, direction, tick, timestamp, p1, price, u1] = array
return {
exchange: exchange,
pair: [primary, secondory],
direction: direction === 1 ? 'UP' : 'DOWN',
lastTrade: [p1, u1],
price: price,
tick: tick | 0,
timestamp: new Date(1000 * timestamp)
}
}
export function dateToUnix (date) {
return Math.floor((date || new Date()).getTime() / 1000)
}
export function unixToDate (unix) {
return new Date(unix * 1000)
}
class IProgress {
split () {
if (typeof this.value === 'object' && !Array.isArray(this.value)) {
let result = { _: this }
for (let key in this.value) {
if (this.value.hasOwnProperty(key)) {
result[key] = new SubProgress(key, this).split()
}
}
return result
} else {
return this
}
}
valueOf () {
return this.value
}
toString () {
return this.value
}
}
export class Progress extends IProgress {
constructor (name, defaultValue = {}) {
super()
this.name = name
this.hasValue = false
this._value = defaultValue
this.storeQueued = false
}
get value () {
if (!this.hasValue) {
this.read()
}
return this._value
}
set value (value) {
this._value = value
this.hasValue = true
this.store()
}
read () {
if (fs.existsSync(this.name + '.progress')) {
try {
this._value = JSON.parse(fs.readFileSync(this.name + '.progress'))
} catch (error) {
console.error('Error while reading progress', error)
}
}
this.hasValue = true
return this
}
store () {
if (this.storeQueued) return
this.storeQueued = true
setTimeout(() => {
fs.writeFile(this.name + '.progress', JSON.stringify(this._value), (error) => {
if (error) {
console.error('Error while saving progress', error)
}
this.storeQueued = false
})
}, 100)
return this
}
}
export class SubProgress extends IProgress {
constructor (name, parent) {
super()
this.name = name
this.parent = parent
}
get value () {
return this.parent.value[this.name]
}
set value (value) {
this.parent.value[this.name] = value
this.parent.store()
}
read () {
this.parent.read()
return this
}
store () {
this.parent.store()
return this
}
}