Currently, js.Func's are very prone to bugs. Developers have to manually call Release() on allocated js.Func's after they are sure there are no references. But, releasing funcs too early leads to errors when called again from the js side. On the other hand, never releasing funcs (although safer) results in memory leaks.
Unlike OS files, socket connections, and other manually freed resources, the lifetime of js.Func's are sometimes unclear, making them a good candidate for automatic collection.
Solution
In 2021, JavaScript added a new FinalizationRegistry class where a function may be called when an object is cleaned up by the garbage collector. Using this, we can clean up the func's resources when they are gc'd on the JavaScript side.
Compatibility
syscall/js is not covered by the go compatibility promise.
Alternatively, to not break current apps, Func.Release() can be turned into a no-op or work as usual alongside the new auto gc.
Most modern versions of major browsers support FinalizationRegistry, which is the current precedent for implementing changes to the wasm backend (see comment). In the event of incompatibility, the js.Func can be leaked, still allowing the program to run as normally as possible.
I propose we remove the js.Func.Release() method.
Problem
Currently, js.Func's are very prone to bugs. Developers have to manually call Release() on allocated js.Func's after they are sure there are no references. But, releasing funcs too early leads to errors when called again from the js side. On the other hand, never releasing funcs (although safer) results in memory leaks.
Unlike OS files, socket connections, and other manually freed resources, the lifetime of js.Func's are sometimes unclear, making them a good candidate for automatic collection.
Solution
In 2021, JavaScript added a new FinalizationRegistry class where a function may be called when an object is cleaned up by the garbage collector. Using this, we can clean up the func's resources when they are gc'd on the JavaScript side.
Compatibility
syscall/js is not covered by the go compatibility promise.
Alternatively, to not break current apps, Func.Release() can be turned into a no-op or work as usual alongside the new auto gc.
Most modern versions of major browsers support FinalizationRegistry, which is the current precedent for implementing changes to the wasm backend (see comment). In the event of incompatibility, the js.Func can be leaked, still allowing the program to run as normally as possible.