Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular2 understanding VM turns and events

Tags:

angular

zonejs

I've just started going into the Angular2 change detection mechanism.

I've read Thoughram's blog posts on the subject (the ones on ZoneJS and on Angular2 zones) but still there's 1 term I can't find any definition for.

Here's an excerpt from this post:

NgZone is basically a forked zone that extends its API and adds some additional functionality to its execution context. One of the things it adds to the API is the following set of custom events we can subscribe to, as they are observable streams:

onTurnStart() - Notifies subscribers just before Angular’s event turn starts. Emits an event once per browser task that is handled by Angular.

onTurnDone() - Notifies subscribers immediately after Angular’s zone is done processing the current turn and any micro tasks scheduled from that turn.

onEventDone() - Notifies subscribers immediately after the final onTurnDone() callback before ending VM event. Useful for testing to validate application state

I understand the concept of a zone and that it can be forked, the only problem I have is with VM turn and VM event, for which I can't find any definition.

What are these VM events and turns? are they part of ZoneJS, Angular or just the browser?

Thanks, Avi.

like image 853
Avi Sasson Avatar asked Aug 05 '16 07:08

Avi Sasson


2 Answers

If anyone's interested in the answer, this cleared things for me: https://jakearchibald.com/2015/tasks-microtasks-queues-and-schedules/

Of course, also read the link Dylan Meeus gave me: What are zone turns?

This is what I now understand these terms mean:

VM turn = browser task - callbacks handled by some browser event loop (e.g setTimout queue) that are run when the stack is clear.

VM event - the action of the browser handling all the pending tasks.

Microtask - As described in the post linked above, these are callbacks that, according the the HTML specification, are not supposed to be scheduled as tasks for performance reasons (e.g promises and observers), so they're scheduled as a thing called a microtask. A microtask enters its own special queue that is run at the end of each callback or task, meaning that if a task schedules a microtask, that microtask will run at the end of that task, blocking any tasks waiting in queue at that time.

like image 87
Avi Sasson Avatar answered Oct 23 '22 13:10

Avi Sasson


Since 2.0.0-beta.10 version, the three methods onTurnStart, onTurnDone and onEventDone was renamed as follows (CHANGELOG.md):

NgZone.onTurnStart => NgZone.onUnstable
NgZone.onTurnDone => NgZone.onMicrotaskEmpty
NgZone.onEventDone => NgZone.onStable

The new names are much better.

like image 3
Mohamed Gara Avatar answered Oct 23 '22 13:10

Mohamed Gara