From b6a70e63902f5e01469edc0eb65cd8547e596562 Mon Sep 17 00:00:00 2001 From: Artem Prokop Date: Sun, 10 Nov 2024 19:00:14 +0100 Subject: [PATCH] fix: timeouts disposal --- src/lib/utils.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 2290956..397e69e 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -1,7 +1,7 @@ import GLib from 'gi://GLib' export class Utils { - static timerId = 0 + static timerIds = new Set() static random(min: number, max: number) { return Math.floor(Math.random() * (max - min + 1)) + min @@ -12,28 +12,31 @@ export class Utils { endFunc: () => boolean, delayFunc: () => number ) { - this.timerId = GLib.timeout_add( + const timerId = GLib.timeout_add( GLib.PRIORITY_DEFAULT, delayFunc(), () => { mainFunc() if (!endFunc()) { - this._cleanupTimeout() this.setInterval(mainFunc, endFunc, delayFunc) } - this.timerId = 0 + this.timerIds.delete(timerId) return GLib.SOURCE_REMOVE } ) + this.timerIds.add(timerId) } - static _cleanupTimeout() { - if (this.timerId) { - GLib.Source.remove(this.timerId) + static _cleanupTimeouts() { + if (this.timerIds.size !== 0) { + for (const timerId of this.timerIds) { + GLib.Source.remove(timerId) + } + this.timerIds.clear() } } static dispose() { - this._cleanupTimeout() + this._cleanupTimeouts() } }