Using hook.js in node

I am trying to write a simple demo using angular / zone.js in node, but for some reason neither beforeTask, nor afterTask.

Here is the code I'm running:

require('zone.js');

function foo () {
  Zone.current.fork({
    name: 'foo_zone',
    
    beforeTask: function () {
      console.log('~~~ ZONE START ~~~');
    },
    
    afterTask: function () {
      console.log('~~~ ZONE END ~~~');
    }
  })
    .run(function () {
      console.log('in the zone');
      console.log('Zone.current.name', Zone.current.name); // prints foo_zone
      setTimeout(() => {
        console.log('timeout is up');
      }, 1000);
    });
}
foo();
Run codeHide result

Now everything prints exactly inside the zone, including the name of the zone, but not one of them is called.

Am I missing something basic with zone.js + node.js?

(works with node v5.0.0, zone.js 0.6.23)

+4
source share
2 answers

Here is an example of storage. https://github.com/JiaLiPassion/zone-node

first you need to use the latest version of zone.js and to use zone.js in nodejs you need a zone - node.js. The following is an example of execution.

require('./zone-node.js');

function log(str) {
  Zone.root.run(function() {
    console.log(str);
  });
}
function foo() {
  Zone.current.fork({
    name: 'fooZone', 
    onScheduleTask: function(delegate, curr, target, task) {
      log('Zone begin to schedule task not async yet ' + task.source);
      return delegate.scheduleTask(target, task);
    },
    onInvokeTask: function(delegate, curr, target, task, applyThis, applyArgs) {
      log('~~~~Zone before invoke async callback~~~~' + task.source);
      delegate.invokeTask(target, task, applyThis, applyArgs);
      log('~~~~Zone after invoke async callback~~~~' + task.source);
    },
  }).run(function() {
    log('current zone, ' + Zone.current.name);
    setTimeout(function() {
      log('timeout is up, ', Zone.current.name);
    }, 100);
  });
};

foo();

nodejs, .

current zone, fooZone
Zone begin to schedule task not async yetsetTimeout
~~~~Zone before invoke async callback~~~~setTimeout
timeout is up,
~~~~Zone after invoke async callback~~~~setTimeout
+1

. , , , 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); // prints foo_zone
        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.

0

Source: https://habr.com/ru/post/1655007/


All Articles