Closed viridia closed 9 months ago
So the solution here, I think, is that we need to make it so that event handlers have lifetimes which are bound by the PresenterState - that is, the event handler lives no longer than the view that created it. Next time we do a data update, the views are recreated, along with the event handlers attached to them.
This would mean that closure captures would be guaranteed to outlive the event handlers (I think).
Fixed, removed use_local, now uses 'atoms'.
I'm not sure that this problem can be solved, but it sure is painful. When you have a component that has one or more closures, you have to clone every captured prop or local state accessor:
The problem arises because neither props or state accessors implement
Copy
, and therefore the only alternative is marking the closures asMove
which requires a distinct instance for each closure. So if you have N closures you need N clones.BTW, the reason that local state accessors don't implement
Copy
is that they contain anArc
, which can only be cloned. Props, on the other hand, can either be copied or not depending on the type, but the lifetime ofProps
isn't long enough (it's long-lived; a copy of Props is cached in the PresenterState - but may not be as long as the event handlers.)This seems like a fundamental limitation of Rust closures. The question is whether we can come up with a better developer experience - I don't have any good ideas at this point.