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