Skip to content

Please avoid using WeakRef in your code #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wolfbeast opened this issue Jan 27, 2025 · 0 comments
Open

Please avoid using WeakRef in your code #7

wolfbeast opened this issue Jan 27, 2025 · 0 comments

Comments

@wolfbeast
Copy link

wolfbeast commented Jan 27, 2025

After running into an issue with Gitea and their devs tracing it back to github text-expander and then to here as a dependency, I'd like to point out the following:

this.#inputRef = new WeakRef(input);

this.#inputRef = new WeakRef(input);

Using WeakRef in any capacity outside of extremely specific situations (and tailor-made code) is strongly discouraged. e.g. the W3C TAG Design Principles group has a number of cautionary notes about it, and in many cases calling the WeakRef API and creating the associated finalization objects on resources is not helping with GC but rather a hindrance. It should never be incorporated in generic libraries or broadly-used dependencies.

It's also entirely unclear why you chose to use WeakRef in this situation to begin with; I think it doesn't do what you suppose it does. Calling .deref() does not immediately GC it nor does it guarantee anything about how safe the object is to garbage collect. Your assumption that the input gets garbage collected on unmount immediately is incorrect. .deref() is also not a magic wand to avoid having to properly release references to objects, as finalizers may or may not be called after .deref()-ing an object. At most they are hints to the garbage collector but any existing (stronger) ref won't be blown away by this.

Calls to WeakRef's CTOR are also not supported by all browser engines (which is what started the Gitea issue to begin with), and where they are supported, they are not at all reliable.

You may want to revert (part of) commit b220c7e and avoid calling the WeakRef API in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant