From c52f8a0480a4c7c4e5bd06084dab67b659f20402 Mon Sep 17 00:00:00 2001 From: Julian Waller Date: Fri, 9 Aug 2024 23:22:30 +0100 Subject: [PATCH] fix: debounce mdns restart --- satellite/package.json | 1 + satellite/src/mdnsAnnouncer.ts | 23 ++++++++++++++++------- yarn.lock | 1 + 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/satellite/package.json b/satellite/package.json index e4e108c..f6c5809 100644 --- a/satellite/package.json +++ b/satellite/package.json @@ -54,6 +54,7 @@ "@napi-rs/canvas": "^0.1.53", "@xencelabs-quick-keys/node": "^1.0.0", "conf": "^13.0.1", + "debounce-fn": "^6.0.0", "electron-about-window": "^1.15.2", "electron-store": "^10.0.0", "eventemitter3": "^5.0.1", diff --git a/satellite/src/mdnsAnnouncer.ts b/satellite/src/mdnsAnnouncer.ts index 78b939e..d8ef17f 100644 --- a/satellite/src/mdnsAnnouncer.ts +++ b/satellite/src/mdnsAnnouncer.ts @@ -2,6 +2,7 @@ import type Conf from 'conf' import type { SatelliteConfig } from './config.js' import { Bonjour, Service } from '@julusian/bonjour-service' import os from 'os' +import debounceFn from 'debounce-fn' export class MdnsAnnouncer { readonly #appConfig: Conf @@ -12,15 +13,23 @@ export class MdnsAnnouncer { constructor(appConfig: Conf) { this.#appConfig = appConfig - this.#appConfig.onDidChange('mdnsEnabled', () => this.restart()) - this.#appConfig.onDidChange('restPort', () => this.restart()) - this.#appConfig.onDidChange('restEnabled', () => this.restart()) + this.#appConfig.onDidChange('mdnsEnabled', () => this.#restart()) + this.#appConfig.onDidChange('installationName', () => this.#restart()) + this.#appConfig.onDidChange('restPort', () => this.#restart()) + this.#appConfig.onDidChange('restEnabled', () => this.#restart()) } - restart() { - this.stop() - this.start() - } + readonly #restart = debounceFn( + () => { + this.stop() + this.start() + }, + { + wait: 50, + before: false, + after: true, + }, + ) start() { if (!this.#appConfig.get('mdnsEnabled')) return diff --git a/yarn.lock b/yarn.lock index 76a3907..22611d4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6426,6 +6426,7 @@ __metadata: "@xencelabs-quick-keys/node": "npm:^1.0.0" conf: "npm:^13.0.1" cross-env: "npm:^7.0.3" + debounce-fn: "npm:^6.0.0" electron: "npm:31.2.1" electron-about-window: "npm:^1.15.2" electron-builder: "npm:^24.13.3"