-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
127 lines (108 loc) · 3.03 KB
/
example.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
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
123
124
125
126
127
import { Mobtime, Message, nodeWebsocket } from "./sdk/index.js";
const millisecondsToMMSS = totalMilliseconds => {
const totalSeconds = Math.floor(totalMilliseconds / 1000);
const minutes = Math.floor(totalSeconds / 60).toString();
const seconds = (totalSeconds - minutes * 60).toString();
return `${minutes.padStart(2, "0")}:${seconds.padStart(2, "0")}`;
};
const timerFlag = process.argv.find(a => a.startsWith("--timer="));
if (!timerFlag) {
console.error("must specify mobtime id with --timer=<timerid>");
process.exit(0);
}
const timerId = timerFlag.split("=")[1];
const domainFlag = process.argv.find(a => a.startsWith("--domain="));
const domain = domainFlag ? domainFlag.split("=")[1] : "mobti.me";
const secure = !process.argv.find(a => a === "--insecure");
const logGroup = (title, logs) => {
console.log(`== ${title} ===============`);
console.log();
logs.forEach(data => console.log(...[].concat(data)));
console.log();
};
/**
* @param {Mobtime} mobtime
* @return {PromiseLike<any>}
*/
const example = mobtime => {
let tickHandle = null;
mobtime.on(Message.MOB_UPDATE, () => {
logGroup("Mob", [
["new", mobtime.mob().newItems()],
["changed", mobtime.mob().changedItems()],
["removed", mobtime.mob().removedItems()],
[
"list",
mobtime
.mob()
.items()
.map(m => m.name),
],
]);
});
mobtime.on(Message.GOALS_UPDATE, () => {
logGroup("Goals", [
["new", mobtime.goals().newItems()],
["changed", mobtime.goals().changedItems()],
["removed", mobtime.goals().removedItems()],
[
"list",
mobtime
.goals()
.items()
.map(g => `[${g.completed ? "x" : " "}] ${g.text}`),
],
]);
});
mobtime.on(Message.SETTINGS_UPDATE, () => {
logGroup("Settings", [
["changed", mobtime.settings().changedItems()],
["all", mobtime.settings().items()],
]);
});
let lastTime = "";
const timerTick = () => {
if (!mobtime.timer().isRunning()) return;
const total = mobtime.timer().remainingMilliseconds();
const time = millisecondsToMMSS(total);
if (time === lastTime) return;
console.log(`Timer: ${time}`);
lastTime = time;
if (total <= 0) {
mobtime
.timer()
.complete()
.commit();
}
};
mobtime.on(Message.TIMER_START, () => {
console.log(
"Timer Started: ",
millisecondsToMMSS(mobtime.timer().items().duration),
);
});
mobtime.on(Message.TIMER_UPDATE, () => {});
mobtime.on(Message.TIMER_PAUSE, () => {
console.log("timer:paused");
});
mobtime.on(Message.TIMER_COMPLETE, () => {
clearInterval(tickHandle);
timerTick();
console.log("timer:completed");
});
tickHandle = setInterval(timerTick, 250);
console.log("timer connected");
};
new Mobtime()
.usingSocket(
nodeWebsocket(timerId, {
secure,
domain,
}),
)
.then(mobtime => mobtime.ready())
.then(example)
.catch(err => {
console.error(err);
process.exit(-1);
});