-
Hi there, I'm trying to get a handle on how react-spring works and I'm working through some of the examples. Looking at the MessageHub example, I see two Weakmaps being created right off the bat (lines 11 and 12) for refs and timeouts. I understand why these data structures exist, but I'm not clear on why Weakmaps are advantageous in this situation. I haven't used Weakmaps much and I don't have any intution for why they are useful in this situation. I also wonder why are they initialized within a Thanks a bunch! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I think @drcmda wrote that example, so he could give more insight, PS: If you're interested in trying v9 out, the example you linked was updated here. |
Beta Was this translation helpful? Give feedback.
-
If anyone comes across this and has the same question, I learned more about With the messageHub, the messages get cleared out on a timer. When the timer expires, they get removed from the data structure. But if the example had used an ordinary Pretty cool stuff. If someone sees this and sees an aspect of this that I've missed, I'd love to hear about it. I found this article that helped me finally understand it. |
Beta Was this translation helpful? Give feedback.
If anyone comes across this and has the same question, I learned more about
WeakMap
s and I think I get it.With the messageHub, the messages get cleared out on a timer. When the timer expires, they get removed from the data structure. But if the example had used an ordinary
Map
orObject
, there would still be a reference to thatitem
object in therefMap
and thecancelMap
(each areWeakmap
s indexed by theitem
), but I don't actually care about that reference anymore, and it can be garbage collected automatically. That's what Weakmaps do. As soon as theremoveItem
setter is called either in lines 32 or 43, the weakmaps lose their corresponding entry as well. In this case, using theWeakmap
…