. , , , zone.js repo, , beforeTask afterTask, , API ZoneSpec ( , zone.fork), beforeTask afterTask . , , / .
API "onInvokeTask", :
onInvokeTask?: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task, applyThis: any, applyArgs: any) => any;
, ZoneSpec, - :
require('zone.js');
function foo () {
Zone.current.fork({
name: 'foo_zone',
onInvokeTask: function (parentZoneDelegate, currentZone, targetZone, task, applyThis, applyArgs) {
console.log('~~~ ZONE START ~~~');
parentZoneDelegate.invokeTask(targetZone, task);
console.log('~~~ ZONE END ~~~');
},
})
.run(function () {
setTimeout(function() {
console.log('in the zone');
console.log('Zone.current.name', Zone.current.name);
setTimeout(() => {
console.log('timeout is up');
}, 1000);
}, 0);
});
}
foo();
:
~~~ ZONE START ~~~
in the zone
Zone.current.name foo_zone
~~~ ZONE END ~~~
~~~ ZONE START ~~~
timeout is up
~~~ ZONE END ~~~
:
beforeTask afterTask , , , , onInvokeTask, , , , , parentZoneDelegate.invokeTask(targetZone, task);
setTimeout(function() { ... }, 0), , ( setTimeout, ), hook onInvokeTask , . , ~~~ ZONE START ~~~ ~~~ ZONE END ~~~ timeout is up.