Skip to content

Commit 519acf0

Browse files
committed
fix: add idle watcher interval cleanup and resource management
- Store interval ID in _idleWatcherIntervalId property - Store reset handler in _resetIdleTimer for cleanup - Prevent duplicate intervals with clearInterval check - Add cleanupIdleWatcher() method to remove interval and event listeners - No behavior changes to idle detection or FPS throttling
1 parent 971a9c6 commit 519acf0

File tree

1 file changed

+43
-11
lines changed

1 file changed

+43
-11
lines changed

js/activity.js

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1919,8 +1919,9 @@ class Activity {
19191919
// Queue and take first step.
19201920
if (!this.turtles.running()) {
19211921
this.logo.runLogoCommands();
1922-
document.getElementById("stop").style.color =
1923-
this.toolbar.stopIconColorWhenPlaying;
1922+
document.getElementById(
1923+
"stop"
1924+
).style.color = this.toolbar.stopIconColorWhenPlaying;
19241925
}
19251926
this.logo.step();
19261927
} else {
@@ -2245,8 +2246,9 @@ class Activity {
22452246
i < this.palettes.dict[this.palettes.activePalette].protoList.length;
22462247
i++
22472248
) {
2248-
const name =
2249-
this.palettes.dict[this.palettes.activePalette].protoList[i]["name"];
2249+
const name = this.palettes.dict[this.palettes.activePalette].protoList[i][
2250+
"name"
2251+
];
22502252
if (name in obj["FLOWPLUGINS"]) {
22512253
// eslint-disable-next-line no-console
22522254
console.log("deleting " + name);
@@ -2758,6 +2760,11 @@ class Activity {
27582760
let lastActivity = Date.now();
27592761
let isIdle = false;
27602762

2763+
// Prevent duplicate intervals
2764+
if (this._idleWatcherIntervalId) {
2765+
clearInterval(this._idleWatcherIntervalId);
2766+
}
2767+
27612768
// Wake up function - restores full framerate
27622769
const resetIdleTimer = () => {
27632770
lastActivity = Date.now();
@@ -2769,6 +2776,9 @@ class Activity {
27692776
}
27702777
};
27712778

2779+
// Store reset handler for cleanup
2780+
this._resetIdleTimer = resetIdleTimer;
2781+
27722782
// Track user activity
27732783
window.addEventListener("mousemove", resetIdleTimer);
27742784
window.addEventListener("mousedown", resetIdleTimer);
@@ -2777,7 +2787,7 @@ class Activity {
27772787
window.addEventListener("wheel", resetIdleTimer);
27782788

27792789
// Periodic check for idle state
2780-
setInterval(() => {
2790+
this._idleWatcherIntervalId = setInterval(() => {
27812791
// Check if music/code is playing
27822792
const isMusicPlaying = this.logo?._alreadyRunning || false;
27832793

@@ -2799,6 +2809,26 @@ class Activity {
27992809
}
28002810
};
28012811

2812+
/**
2813+
* Cleans up the Idle Watcher resources.
2814+
* Clears the interval and removes event listeners to prevent memory leaks.
2815+
* This method can be called when the activity is being destroyed or reset.
2816+
*/
2817+
this.cleanupIdleWatcher = function () {
2818+
if (this._idleWatcherIntervalId) {
2819+
clearInterval(this._idleWatcherIntervalId);
2820+
this._idleWatcherIntervalId = null;
2821+
}
2822+
2823+
if (this._resetIdleTimer) {
2824+
window.removeEventListener("mousemove", this._resetIdleTimer);
2825+
window.removeEventListener("mousedown", this._resetIdleTimer);
2826+
window.removeEventListener("keydown", this._resetIdleTimer);
2827+
window.removeEventListener("touchstart", this._resetIdleTimer);
2828+
window.removeEventListener("wheel", this._resetIdleTimer);
2829+
}
2830+
};
2831+
28022832
/*
28032833
* Creates and renders error message containers with appropriate artwork.
28042834
* Some error messages have special artwork.
@@ -5204,8 +5234,9 @@ class Activity {
52045234
}
52055235
}
52065236
staffBlocksMap[staffIndex].baseBlocks[0][0][firstnammedo][4][0] = blockId;
5207-
staffBlocksMap[staffIndex].baseBlocks[repeatId.end][0][endnammedo][4][1] =
5208-
null;
5237+
staffBlocksMap[staffIndex].baseBlocks[repeatId.end][0][
5238+
endnammedo
5239+
][4][1] = null;
52095240

52105241
blockId += 2;
52115242
} else {
@@ -5273,8 +5304,9 @@ class Activity {
52735304
prevnameddo
52745305
][4][1] = blockId;
52755306
} else {
5276-
staffBlocksMap[staffIndex].repeatBlock[prevrepeatnameddo][4][3] =
5277-
blockId;
5307+
staffBlocksMap[staffIndex].repeatBlock[
5308+
prevrepeatnameddo
5309+
][4][3] = blockId;
52785310
}
52795311
if (afternamedo !== -1) {
52805312
staffBlocksMap[staffIndex].baseBlocks[repeatId.end][0][
@@ -6149,8 +6181,8 @@ class Activity {
61496181
let customName = "custom";
61506182
if (myBlock.connections[1] !== null) {
61516183
// eslint-disable-next-line max-len
6152-
customName =
6153-
this.blocks.blockList[myBlock.connections[1]].value;
6184+
customName = this.blocks.blockList[myBlock.connections[1]]
6185+
.value;
61546186
}
61556187
// eslint-disable-next-line no-console
61566188
console.log(customName);

0 commit comments

Comments
 (0)