Skip to content

MessageHub example and Weakmaps #1080

Answered by bstro
bstro asked this question in Support
Jul 14, 2020 · 2 comments · 1 reply
Discussion options

You must be logged in to vote

If anyone comes across this and has the same question, I learned more about WeakMaps 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 or Object, there would still be a reference to that item object in the refMap and the cancelMap (each are Weakmaps indexed by the item), 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 the removeItem setter is called either in lines 32 or 43, the weakmaps lose their corresponding entry as well. In this case, using the Weakmap

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@bstro
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by bstro
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants