diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index afb4189b4..2035bc16a 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -31,5 +31,6 @@ jobs: - run: npx nyc --reporter=lcov npm test env: CI: true + DISPLAY: ':0' - name: Codecov uses: codecov/codecov-action@v3 diff --git a/CHANGELOG.md b/CHANGELOG.md index f59ac85f7..72856ec07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Fixed +- improve DND monitoring memory usage + ## [1.15.0] - 2023-11-11 ### Added - new end-of-the-break sound diff --git a/app/utils/dndManager.js b/app/utils/dndManager.js index 06f2c622d..54a0f99a9 100644 --- a/app/utils/dndManager.js +++ b/app/utils/dndManager.js @@ -8,6 +8,17 @@ class DndManager extends EventEmitter { this.monitorDnd = settings.get('monitorDnd') this.timer = null this.isOnDnd = false + + if (process.platform === 'win32') { + this.windowsFocusAssist = require('windows-focus-assist') + this.windowsQuietHours = require('windows-quiet-hours') + } else if (process.platform === 'darwin') { + this.macosNotificationState = require('macos-notification-state') + } else if (process.platform === 'linux') { + this.bus = require('dbus-final').sessionBus() + this.util = require('node:util') + } + if (this.monitorDnd) { this.start() } @@ -31,10 +42,8 @@ class DndManager extends EventEmitter { } async _isDndEnabledLinux () { - const dbus = require('dbus-final') - const bus = dbus.sessionBus() try { - const obj = await bus.getProxyObject('org.freedesktop.Notifications', '/org/freedesktop/Notifications') + const obj = await this.bus.getProxyObject('org.freedesktop.Notifications', '/org/freedesktop/Notifications') const properties = obj.getInterface('org.freedesktop.DBus.Properties') const dndEnabled = await properties.Get('org.freedesktop.Notifications', 'Inhibited') if (await dndEnabled.value) { @@ -45,7 +54,7 @@ class DndManager extends EventEmitter { } try { - const obj = await bus.getProxyObject('org.xfce.Xfconf', '/org/xfce/Xfconf') + const obj = await this.bus.getProxyObject('org.xfce.Xfconf', '/org/xfce/Xfconf') const properties = obj.getInterface('org.xfce.Xfconf') const dndEnabled = await properties.GetProperty('xfce4-notifyd', '/do-not-disturb') if (await dndEnabled.value) { @@ -56,8 +65,7 @@ class DndManager extends EventEmitter { } try { - const util = require('node:util') - const exec = util.promisify(require('node:child_process').exec) + const exec = this.util.promisify(require('node:child_process').exec) const { stdout } = await exec('gsettings get org.gnome.desktop.notifications show-banners') if (stdout.replace(/[^0-9a-zA-Z]/g, '') === 'false') { return true @@ -75,12 +83,12 @@ class DndManager extends EventEmitter { if (process.platform === 'win32') { let wfa = 0 try { - wfa = require('windows-focus-assist').getFocusAssist().value + wfa = this.windowsFocusAssist.getFocusAssist().value } catch (e) { wfa = -1 } // getFocusAssist() throw an error if OS isn't windows - const wqh = require('windows-quiet-hours').getIsQuietHours() + const wqh = this.windowsQuietHours.getIsQuietHours() return wqh || (wfa !== -1 && wfa !== 0) } else if (process.platform === 'darwin') { - return require('macos-notification-state').getDoNotDisturb() + return this.macosNotificationState.getDoNotDisturb() } else if (process.platform === 'linux') { return await this._isDndEnabledLinux() }