Skip to content

refactor: use WeakMap for block data storage in inspector#166

Merged
dermatz merged 1 commit intomainfrom
refactor/add-weakmap-for-blockData
Apr 11, 2026
Merged

refactor: use WeakMap for block data storage in inspector#166
dermatz merged 1 commit intomainfrom
refactor/add-weakmap-for-blockData

Conversation

@dermatz
Copy link
Copy Markdown
Member

@dermatz dermatz commented Apr 11, 2026

This pull request refactors how block metadata is associated with DOM elements in the MageForge Inspector. Instead of attaching data directly to elements as a custom property, it now uses a shared WeakMap (blockDataMap). This approach avoids polluting the DOM element namespace and allows for better memory management.

Block metadata storage refactor:

  • Introduced a new blockDataMap (a WeakMap) in inspector/blockData.js to store block metadata associated with DOM elements, enabling automatic garbage collection and preventing namespace pollution.
  • Updated all usages to retrieve block data from blockDataMap instead of the _mageforgeBlockData property in inspector.js, picker.js, and ui.js. [1] [2] [3]
  • Updated imports in relevant files to import blockDataMap from the new module. [1] [2] [3]

Copilot AI review requested due to automatic review settings April 11, 2026 14:57
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors MageForge Inspector’s frontend block-metadata association to use a shared WeakMap (blockDataMap) instead of attaching a custom property on DOM elements, reducing DOM namespace pollution and enabling automatic cleanup when elements are removed.

Changes:

  • Added a new inspector/blockData.js module exporting a shared WeakMap for element→block metadata.
  • Updated picker logic to store block metadata via blockDataMap.set(element, data) rather than element._mageforgeBlockData.
  • Updated UI/panel rendering to read metadata via blockDataMap.get(element).

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
src/view/frontend/web/js/inspector/blockData.js Introduces shared WeakMap storage for block metadata keyed by DOM elements.
src/view/frontend/web/js/inspector/picker.js Stores detected block metadata into blockDataMap for the hovered/selected element.
src/view/frontend/web/js/inspector/ui.js Reads block metadata from blockDataMap when building badge content.
src/view/frontend/web/js/inspector.js Reads block metadata from blockDataMap when updating the panel data.

@dermatz dermatz merged commit 5c383fd into main Apr 11, 2026
17 checks passed
@dermatz dermatz deleted the refactor/add-weakmap-for-blockData branch April 11, 2026 15:02
@github-actions github-actions bot mentioned this pull request Apr 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants