Currently, various watchers in the HSApplicationModule (e.g., _addWatcher) store their JavaScript callbacks directly as JSValue. This can lead to unexpected behavior or crashes if the JavaScript garbage collector collects the callback, since the watcher holds a strong reference to a JSValue that might no longer be valid.
To improve stability and prevent potential memory issues, these watchers should store callbacks using JSManagedValue. JSManagedValue allows the JavaScriptCore garbage collector to manage the lifecycle of the JS objects safely while still keeping a weak reference from Swift, ensuring that callbacks remain valid for the lifetime of the watcher without creating retain cycles.
This change would make the watcher system more robust and safer in scenarios where callbacks are added and removed dynamically.
Currently, various watchers in the HSApplicationModule (e.g., _addWatcher) store their JavaScript callbacks directly as JSValue. This can lead to unexpected behavior or crashes if the JavaScript garbage collector collects the callback, since the watcher holds a strong reference to a JSValue that might no longer be valid.
To improve stability and prevent potential memory issues, these watchers should store callbacks using JSManagedValue. JSManagedValue allows the JavaScriptCore garbage collector to manage the lifecycle of the JS objects safely while still keeping a weak reference from Swift, ensuring that callbacks remain valid for the lifetime of the watcher without creating retain cycles.
This change would make the watcher system more robust and safer in scenarios where callbacks are added and removed dynamically.