diff options
Diffstat (limited to 'src/webview/badge.js')
-rw-r--r-- | src/webview/badge.js | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/webview/badge.js b/src/webview/badge.js new file mode 100644 index 000000000..1e02fb56a --- /dev/null +++ b/src/webview/badge.js | |||
@@ -0,0 +1,33 @@ | |||
1 | const { ipcRenderer } = require('electron'); | ||
2 | |||
3 | const debug = require('debug')('Ferdi:Plugin:BadgeHandler'); | ||
4 | |||
5 | export class BadgeHandler { | ||
6 | constructor() { | ||
7 | this.countCache = { | ||
8 | direct: 0, | ||
9 | indirect: 0, | ||
10 | }; | ||
11 | } | ||
12 | |||
13 | setBadge(direct, indirect) { | ||
14 | if (this.countCache.direct === direct | ||
15 | && this.countCache.indirect === indirect) return; | ||
16 | |||
17 | // Parse number to integer | ||
18 | // This will correct errors that recipes may introduce, e.g. | ||
19 | // by sending a String instead of an integer | ||
20 | const directInt = parseInt(direct, 10); | ||
21 | const indirectInt = parseInt(indirect, 10); | ||
22 | |||
23 | const count = { | ||
24 | direct: Math.max(directInt, 0), | ||
25 | indirect: Math.max(indirectInt, 0), | ||
26 | }; | ||
27 | |||
28 | ipcRenderer.sendToHost('message-counts', count); | ||
29 | Object.assign(this.countCache, count); | ||
30 | |||
31 | debug('Sending badge count to host', count); | ||
32 | } | ||
33 | } | ||