whatwg / dom

DOM Standard
https://dom.spec.whatwg.org/
Other
1.56k stars 290 forks source link

`mutationObserver.disconnect({ flush: true })` #1283

Open LeaVerou opened 4 months ago

LeaVerou commented 4 months ago

What problem are you trying to solve?

Calling mutationObserver.disconnect() while forgetting to handle any pending records is a common footgun.

What solutions exist today?

Having to write boilerplate like:

let records = mutationObserver.takeRecords();
if (records.length > 0) {
    callback(records);
}
mutationObserver.disconnect();

Every time you want to stop observing is quite repetitive, and it cannot be done with just a reference to the mutation observer, since it requires a reference to its callback too.

How would you solve it?

Add a dictionary argument to disconnect() with a flush option (name TBB) that does exactly this.

Anything else?

No response

WebReflection commented 4 months ago

as one that has been using MO forever (polyfills included) I never even thought the leaky records would be a problem but I fully agree with this issue concerns and I also need to likely update all my libraries heavily based on MO to guarantee something not observed anymore won't be processed after a disconnect happens.

Thanks for considering this bug and raising its importance too, if needed.