proposal-signals / signal-utils

MIT License
69 stars 6 forks source link

React bindings sketch #72

Open ds300 opened 2 weeks ago

ds300 commented 2 weeks ago

Hi pals πŸ‘‹πŸΌ This PR is a proof of concept to add React bindings for the signal-polyfill package, based on tldraw's internal fork of signia's react bindings.

Implementing sound react bindings required a couple of features that the spec doesn't have yet:

  1. Computed.isPending – to allow us to check whether a computed's source values have changed since it was last updated. This is needed because, much like the signal graph algorithm decides whether to recalculate a computed value based on whether its sources have changed, we need to decide whether to tell react to rerender based on whether the render function's reactive sources have changed without actually invoking the render function itself (since it might use react hooks, which throw when invoked outside of a react render cycle)
  2. Computed.get(forceRecalculate) – a version of Computed.get which ignores the cache. This is needed because you cannot memoize the result of a react render and simply return that result on future renders. The render may use hooks and React will throw errors if you don't invoke the same set of hooks on every render.

These features were added in a pnpm patch file. Unfortunately I had to patch the minified code but they're still quite readable changes.