Suppose I make changes to an element inside a frame created with window.requestAnimationFrame
and that the modified element is being observed by a MutationObserver
. Does the MutationOberserver
's change handler fire within the same frame and before the browser does rendering that comes after the frame?
(I can imagine that this may not be true with some polyfills. I'm curious to know if there's any guarantee that this is true in native implementations.)
EDIT: A possibly better question is Are microtasks guaranteed to fire within the same animation frame where they were queued?, which may indirectly answer this question, as well as explain Promise behavior, or behavior of anything else that uses microtasks.
EDIT July 19 '17: The reason I want to know about this, is because I want to know exactly how modifying DOM and doing custom rendering (f.e. WebGL) based on the DOM mutations can be kept in sync with the browser's paint.
For example, suppose a user of my custom elements uses requestAnimationFrame
, then modifies some attribute of my webgl-purposed custom elements. I would like to be able to guarantee that I can react to those changes before the browser paint that will follow after the animation frame, do my webgl render stuff that the user will be expecting, and have it show up in the next frame, not any later frame.
Based on some theory, I am aware that Promise microtasks fire after the current macrotask, and based on some testing I've done, I have verified that Promise handlers defined in an animation frame will fire before the next browser paint.
Extending this to Mutation Observers, I believe that they fire in the next microtask, and therefore they will behave the same as Promise handlers: firing before the next paint.
I think I've answered my question, but maybe confirmation would be of benefit.