Remove error prone refCount logic

This can simply use the length of the callbacks array instead.
This commit is contained in:
Hornwitser committed 2026-03-19 16:10:54 +01:00
1 parent 019737286f
commit b3dc262862
1 file changed
+2 -5
@@ -3,7 +3,6 @@ import type { Control } from "../util/websocket";
// TODO: replace this super duper expensive method with listening to a default mod pack id event.
class DefaultModPackStore {
refCount = 0;
interval: undefined | ReturnType<typeof setInterval>;
modPack: undefined | ModPack;
callbacks: (() => void)[] = [];
@@ -27,20 +26,18 @@ class DefaultModPackStore {
}
subscribe(control: Control, callback: () => void) {
if (this.refCount === 0) {
if (this.callbacks.length === 0) {
const update = () => { this.fetchDefaultModPack(control); };
update();
this.interval = setInterval(update, 60e3);
}
this.callbacks.push(callback);
this.refCount += 1;
return () => {
const index = this.callbacks.indexOf(callback);
if (index !== -1) {
this.callbacks.splice(index, 1);
}
this.refCount -= 1;
if (this.refCount === 0) {
if (this.callbacks.length === 0) {
clearInterval(this.interval);
this.interval = undefined;
}