Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[react-transform] Prepend
useSignals
call if function isn't clearly…
… a component (#458) It's possible to use the `@trackSignals` comment manually opt-in to the react signals transform on a function that isn't obviously a Component or hook. Before, we would always wrap those functions in a `try/finally` clause. However, this could break signal tracking if the function was actually used as a hook. For example: ```js /** @trackSignals */ function badHookName() { try { // When this hook runs it will close any open effect. Specifically, // the effect in Component. This means, upon exiting this hook, // all effects will be closed and any signals accesed in "... other stuff ..." // in Component will be missed. const e = useSignals(); ... } finally { e.f() // Finish this hook's effect } } function Component() { try { const e = useSignals(); badHookName(); // .. other stuff ... } finally { e.f(); // Finish this hook's effect. } | ``` We fix the bug described in the comment above by just prepending `useSignals()`. This method of tracking signals in React relies on a microtick to close the effect meaning any signals accessed in "... other stuff ..." will be captured by this effect. More improvements to this scenario will come in a follow up PR. This PR also adds more tests for transforming hooks in general.
- Loading branch information