forked from diceguyd30/queso_to_go_template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
waiting.js
47 lines (43 loc) · 1.4 KB
/
waiting.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
const timeOrNow = (time = undefined) => {
if (time === undefined) {
return (new Date()).toISOString();
} else {
return time;
}
};
const hasOwn = (object, property) => {
return Object.prototype.hasOwnProperty.call(object, property);
};
// Waiting prototype
const Waiting = {
create: (now = undefined) => {
return Waiting.from({ waitTime: 1, weightMin: 1, weightMsec: 0, lastOnlineTime: timeOrNow(now) });
},
from(jsonObject) {
const defaults = { weightMin: jsonObject.waitTime, weightMsec: 0 };
return Object.assign(Object.create(Waiting), { ...defaults, ...jsonObject });
},
fromV1(waitTime, now = undefined) {
return Waiting.from({ waitTime, weightMin: waitTime, weightMsec: 0, lastOnlineTime: timeOrNow(now) });
},
addOneMinute(multiplier, now = undefined) {
const addMin = Math.floor(multiplier);
const addMsec = Math.round((multiplier % 1) * 60000);
this.weightMsec += addMsec;
// minute overflow
while (this.weightMsec >= 60000) {
this.weightMsec -= 60000;
this.weightMin += 1;
}
this.weightMin += addMin;
this.waitTime += 1;
this.lastOnlineTime = timeOrNow(now);
},
weight() {
// round to nearest minute
return this.weightMin + (this.weightMsec >= 30000 ? 1 : 0);
},
};
module.exports = {
Waiting,
};