mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-24 08:23:12 +09:00
Remove error prone refCount logic
This can simply use the length of the callbacks array instead.
This commit is contained in:
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.
|
// TODO: replace this super duper expensive method with listening to a default mod pack id event.
|
||||||
class DefaultModPackStore {
|
class DefaultModPackStore {
|
||||||
refCount = 0;
|
|
||||||
interval: undefined | ReturnType<typeof setInterval>;
|
interval: undefined | ReturnType<typeof setInterval>;
|
||||||
modPack: undefined | ModPack;
|
modPack: undefined | ModPack;
|
||||||
callbacks: (() => void)[] = [];
|
callbacks: (() => void)[] = [];
|
||||||
@@ -27,20 +26,18 @@ class DefaultModPackStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
subscribe(control: Control, callback: () => void) {
|
subscribe(control: Control, callback: () => void) {
|
||||||
if (this.refCount === 0) {
|
if (this.callbacks.length === 0) {
|
||||||
const update = () => { this.fetchDefaultModPack(control); };
|
const update = () => { this.fetchDefaultModPack(control); };
|
||||||
update();
|
update();
|
||||||
this.interval = setInterval(update, 60e3);
|
this.interval = setInterval(update, 60e3);
|
||||||
}
|
}
|
||||||
this.callbacks.push(callback);
|
this.callbacks.push(callback);
|
||||||
this.refCount += 1;
|
|
||||||
return () => {
|
return () => {
|
||||||
const index = this.callbacks.indexOf(callback);
|
const index = this.callbacks.indexOf(callback);
|
||||||
if (index !== -1) {
|
if (index !== -1) {
|
||||||
this.callbacks.splice(index, 1);
|
this.callbacks.splice(index, 1);
|
||||||
}
|
}
|
||||||
this.refCount -= 1;
|
if (this.callbacks.length === 0) {
|
||||||
if (this.refCount === 0) {
|
|
||||||
clearInterval(this.interval);
|
clearInterval(this.interval);
|
||||||
this.interval = undefined;
|
this.interval = undefined;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user