Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a MutationObserver destroyed when the observed node is destroyed?

I observe a node by simply doing

new MutationObserver(callback)
  .observe(shape.node, {attributes: true})

As you can see I dont have any references to the observer. The observed node will get destroyed at some point.

Is the memory for the node and for the observer cleaned up? Or do they keep themselves alife? And if so: How can I prevent that from happening? I do not know when the node gets removed.

Ofc I could also observe the parent and disconnect the first observer when the parent has a "child list changed" observed but I would like to avoid that

like image 513
Fuzzyma Avatar asked Jun 12 '18 15:06

Fuzzyma


1 Answers

MutationObservers hold a weak reference to the nodes they are observing, and the nodes have a strong reference to the mutation observer. That means that in your case, the only thing referencing the observer is the node object. If the node is GCed, nothing will be referencing the observer so it will also be GCed.

like image 56
loganfsmyth Avatar answered Nov 15 '22 06:11

loganfsmyth