Skip to content

Instantly share code, notes, and snippets.

@JiaLiPassion
Last active March 25, 2019 11:51
Show Gist options
  • Save JiaLiPassion/9a4e2971e1ef2ee920379ee6a50b939f to your computer and use it in GitHub Desktop.
Save JiaLiPassion/9a4e2971e1ef2ee920379ee6a50b939f to your computer and use it in GitHub Desktop.
let macroTaskCount = 0;
let microTaskCount = 0;
const zone = Zone.current.fork({
name: 'monitor',
onScheduleTask: (delegate, curr, target, task) => {
if (task.type === 'macroTask') {
macroTaskCount ++;
log('new macroTask will be scheduled: ' + macroTaskCount);
} else if (task.type === 'microTask') {
microTaskCount ++;
log('new microTask will be scheduled: ' + microTaskCount);
}
return delegate.scheduleTask(target, task);
},
onInvokeTask: (delegate, curr, target, task, applyThis, applyArgs) => {
const r = delegate.invokeTask(target, task, applyThis, applyArgs);
if (task.type === 'macroTask') {
macroTaskCount --;
log('macroTask is invoked: ' + macroTaskCount);
} else if (task.type === 'microTask') {
microTaskCount --;
log('microTask is invoked: ' + microTaskCount);
}
return r;
},
});
zone.run(() => {
for (let i = 0; i < 5; i ++) {
setTimeout(() => {});
}
for (let i = 0; i < 5; i ++) {
Promise.resolve(1).then(() => {});
}
});
// macroTask will be scheduled, 1
// macroTask will be scheduled, 2
// macroTask will be scheduled, 3
// macroTask will be scheduled, 4
// macroTask will be scheduled, 5
// microTask will be scheduled, 1
// microTask will be scheduled, 2
// microTask will be scheduled, 3
// microTask will be scheduled, 4
// microTask will be scheduled, 5
// microTask is invoked, 4
// microTask is invoked, 3
// microTask is invoked, 2
// microTask is invoked, 1
// microTask is invoked, 0
// macroTask is invoked, 4
// macroTask is invoked, 3
// macroTask is invoked, 2
// macroTask is invoked, 1
// macroTask is invoked, 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment