-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
123 lines (102 loc) · 2.36 KB
/
index.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
'use strict';
var prefix = 'GreatHall';
var modesEnum = {
event: 1 << 0,
overwatch: 1 << 1,
};
var currentModes = modesEnum.event | modesEnum.overwatch;
function BusLog(xLog, resp) {
var self = this;
self._resp = resp;
self._xLog = xLog;
self._subs = {
verb: null,
info: null,
warn: null,
err: null,
dbg: null,
};
this._subscribe();
}
BusLog.prototype._subscribe = function () {
var self = this;
if (Object.keys(self._subs).some((level) => !!self._subs[level])) {
return;
}
self._xLog.getLevels().forEach(function (level) {
self._subs[level] = function (msg) {
self.log(level, msg);
};
self._xLog.on(level, self._subs[level]);
});
};
BusLog.prototype._unsubscribe = function () {
Object.keys(this._subs)
.filter((level) => !!this._subs[level])
.forEach((level) => {
this._xLog.removeListener(level, this._subs[level]);
this._subs[level] = null;
});
};
BusLog.prototype.log = function (mode, msg) {
if (!this._resp || !this._resp.isConnected()) {
return;
}
if (
currentModes & modesEnum.overwatch &&
(msg.overwatch || mode === 'err') &&
this._resp.hasCommand &&
this._resp.hasCommand('overwatch.exception')
) {
this._resp.command.send('overwatch.exception', {
error: {
...(msg.overwatch || {err: msg.message}),
mod: msg.module,
time: msg.time,
},
});
}
if (currentModes & modesEnum.event) {
this._resp.events.send('widget.text.' + mode, {
prefix: prefix,
mod: msg.module,
text: msg.message,
});
}
if (currentModes === 0) {
this._unsubscribe();
}
};
BusLog.prototype.progress = function (topic, position, length) {
if (!this._resp || !this._resp.isConnected()) {
return;
}
this._resp.events.send('widget.progress', {
prefix: prefix,
mod: this._xLog.getModule(),
topic: topic,
position: position,
length: length,
});
};
module.exports = function (xLog, resp) {
return new BusLog(xLog, resp);
};
module.exports.addModes = function (modes) {
if (modes === 0) {
currentModes = ~0;
} else {
currentModes |= modes;
}
};
module.exports.delModes = function (modes) {
if (modes === 0) {
currentModes = 0;
} else {
currentModes &= ~modes;
}
};
module.exports.getModes = function () {
return currentModes;
};
module.exports.modes = modesEnum;