aboutsummaryrefslogtreecommitdiffstats
path: root/src/webview/badge.js
blob: 1e02fb56a45d17f80c2d8c4844ab6827ab2821fb (plain) (blame)
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
const { ipcRenderer } = require('electron');

const debug = require('debug')('Ferdi:Plugin:BadgeHandler');

export class BadgeHandler {
  constructor() {
    this.countCache = {
      direct: 0,
      indirect: 0,
    };
  }

  setBadge(direct, indirect) {
    if (this.countCache.direct === direct
        && this.countCache.indirect === indirect) return;

    // Parse number to integer
    // This will correct errors that recipes may introduce, e.g.
    // by sending a String instead of an integer
    const directInt = parseInt(direct, 10);
    const indirectInt = parseInt(indirect, 10);

    const count = {
      direct: Math.max(directInt, 0),
      indirect: Math.max(indirectInt, 0),
    };

    ipcRenderer.sendToHost('message-counts', count);
    Object.assign(this.countCache, count);

    debug('Sending badge count to host', count);
  }
}