# weak-target **Repository Path**: mirrors_WebReflection/weak-target ## Basic Information - **Project Name**: weak-target - **Description**: A base class to weakly target any reference. - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-07-31 - **Last Updated**: 2026-01-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # weak-target A base class to weakly target any reference. Inspired by [this post](https://frontendmasters.com/blog/patterns-for-memory-efficient-dom-manipulation/) so that the example code would be instead: ```js import WeakTarget from 'weak-target'; class Counter extends WeakTarget { constructor(target) { super(target); this.timer = 0; this.count = 0; this.start(); } // if present, invoked when `target` is garbage collected destructor() { console.log('Garabage Collector ran and element is GONE – clean up interval'); this.stop(); } start() { if (this.timer) return; this.count = 0; const tick = () => { const { target } = this; // always check if the target exists because WeakRef could be freed // before the FinalizationRegistry callback gets a chance to run if (target) { console.log('Element is still in memory, updating count.'); target.textContent = `Counter: ${++this.count}`; } }; tick(); this.timer = setInterval(tick, 1000); } stop() { if (this.timer) { clearInterval(this.timer); this.timer = 0; } } } ``` **[Live Demo](https://webreflection.github.io/weak-target/test/)** - - - That's it, the class orchestrate with ease the ability to retrieve a `target` reference that might has gone. If there is a `destructor` method attached, this will get invoked whenever that happens, either by accessing `target` explicitly, in case the FinalizationRegistry hasn't called the callback yet, or implicitly, when such callback gets executed. The `destructor` is not meant to be invoked directly and it will be invoked only *once*, like it is for the `constructor`.