From 6234f6884bfb0b786a235668d1cae91e1439f2c7 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Mon, 2 Oct 2023 16:08:21 +0200 Subject: [PATCH] Fix the handling of replaced polls Editing a message with a poll could replace the poll, requiring a new initialization. The previous map was replaced with a `WeakSet` because it is never read from and the map leaked memory when the poll was discarded. See https://www.woltlab.com/community/thread/301810-unable-to-click-on-polls-after-editing-post/ --- ts/WoltLabSuite/Core/Ui/Poll/Poll.ts | 11 +++++++---- .../files/js/WoltLabSuite/Core/Ui/Poll/Poll.js | 9 +++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/ts/WoltLabSuite/Core/Ui/Poll/Poll.ts b/ts/WoltLabSuite/Core/Ui/Poll/Poll.ts index 9696534135..140aaa3567 100644 --- a/ts/WoltLabSuite/Core/Ui/Poll/Poll.ts +++ b/ts/WoltLabSuite/Core/Ui/Poll/Poll.ts @@ -183,18 +183,21 @@ export class Poll { } } -const polls: Map = new Map(); +const polls = new WeakSet(); function setup(): void { document.querySelectorAll(".pollContainer").forEach((pollElement: HTMLElement) => { if (!pollElement.dataset.pollId) { throw new Error("Invalid poll element given. Missing pollID."); } + if (polls.has(pollElement)) { + return; + } + const pollID = parseInt(pollElement.dataset.pollId, 10); + new Poll(pollID); - if (!polls.has(pollID)) { - polls.set(pollID, new Poll(pollID)); - } + polls.add(pollElement); }); } diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Poll/Poll.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Poll/Poll.js index efa8140610..757a9478ab 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Poll/Poll.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Poll/Poll.js @@ -145,16 +145,17 @@ define(["require", "exports", "tslib", "../../Dom/Change/Listener", "../../Dom/U } } exports.Poll = Poll; - const polls = new Map(); + const polls = new WeakMap(); function setup() { document.querySelectorAll(".pollContainer").forEach((pollElement) => { if (!pollElement.dataset.pollId) { throw new Error("Invalid poll element given. Missing pollID."); } - const pollID = parseInt(pollElement.dataset.pollId, 10); - if (!polls.has(pollID)) { - polls.set(pollID, new Poll(pollID)); + if (polls.has(pollElement)) { + return; } + const pollID = parseInt(pollElement.dataset.pollId, 10); + polls.set(pollElement, new Poll(pollID)); }); } function setupAll() { -- 2.20.1