Skip to content

Commit b6a70e6

Browse files
committedNov 10, 2024
fix: timeouts disposal
1 parent 511ad28 commit b6a70e6

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed
 

‎src/lib/utils.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import GLib from 'gi://GLib'
22

33
export class Utils {
4-
static timerId = 0
4+
static timerIds = new Set<number>()
55

66
static random(min: number, max: number) {
77
return Math.floor(Math.random() * (max - min + 1)) + min
@@ -12,28 +12,31 @@ export class Utils {
1212
endFunc: () => boolean,
1313
delayFunc: () => number
1414
) {
15-
this.timerId = GLib.timeout_add(
15+
const timerId = GLib.timeout_add(
1616
GLib.PRIORITY_DEFAULT,
1717
delayFunc(),
1818
() => {
1919
mainFunc()
2020
if (!endFunc()) {
21-
this._cleanupTimeout()
2221
this.setInterval(mainFunc, endFunc, delayFunc)
2322
}
24-
this.timerId = 0
23+
this.timerIds.delete(timerId)
2524
return GLib.SOURCE_REMOVE
2625
}
2726
)
27+
this.timerIds.add(timerId)
2828
}
2929

30-
static _cleanupTimeout() {
31-
if (this.timerId) {
32-
GLib.Source.remove(this.timerId)
30+
static _cleanupTimeouts() {
31+
if (this.timerIds.size !== 0) {
32+
for (const timerId of this.timerIds) {
33+
GLib.Source.remove(timerId)
34+
}
35+
this.timerIds.clear()
3336
}
3437
}
3538

3639
static dispose() {
37-
this._cleanupTimeout()
40+
this._cleanupTimeouts()
3841
}
3942
}

0 commit comments

Comments
 (0)
Please sign in to comment.