fable-compiler / fable-react

Fable bindings and helpers for React and React Native
MIT License
273 stars 67 forks source link

FunctionComponent.Of extend func cache key with 'Props type name #242

Closed DunetsNM closed 1 month ago

DunetsNM commented 1 month ago

User code that invokes FunctionComponent.Of can also be in generic context which makes caching by source FileName+LineNumber unsuitable: one function gets cached and resolved at runtime for different runtime types which results in runtime exceptions.

I propose to add 'Props type name to the cache key to fix that.

You still can write code were wrong function will be picked up at runtime for the same 'Props argument type (e.g. if different render func values are passed from the outside), but it's less likely to happen.

Ultimate solution would be to expose the fact that function is prepared and cached to the users and let them to specify their own unique cache key - or piggyback on existing displayName, which is also not ideal.

DunetsNM commented 1 month ago

this fixes #241

MangelMaxime commented 1 month ago

To avoid impacting too much the bundle size, we could extract the method body into a function and pass it the typeof<'Props>.FullName + all the required information as an argument.

What do you think?

DunetsNM commented 1 month ago

@MangelMaxime I moved the fat part to Cache.GetOrAdd to avoid creating more public/inlineable impl functions

MangelMaxime commented 1 month ago

Thank you