1

Context

I'm starting a student project which is a metric tool for angular2.

I'm actually working on the proof of concept, the proof that I can get metrics on a component at runtime only using a static register method myLibClass.registerComponents([Component1, Component2,...]) which would have an implementation like this one (pseudo-code):

registerComponents(components:any[]):void{
    components.forEach(component => {
        //Register metric methods on zone foreTask and afterTask
    });
}

Problem

How can I get the global zone of angular2? I want to bind my methods on ng2 zone in order to be able to check execution time for each call and NgZone does not provide the tools to do such a thing.

Another idea to metric a component from an external class is welcome too

UPDATE:

I've done some researches and I found how to get the global angular2 zone using ̀ Zone.current`.

Problem is still here, since the second part of it (the hardest one) is not achieved yet:

I want to bind my methods on ng2 zone in order to be able to check execution time for each call and NgZone does not provide the tools to do such a thing.

The only way to do this would be to fork the angular2 zone and replace the actual zone used by angular by my forked one (That's the only way to do it I see).

Is it even possible? Forking the zone is not a problem but I can't connect it on angular2.

Since I need more informations than null (as you can see on the source code of ngZone, every event fired is fired with null, this doesn't allow me to get the informations I need about the last microtask like the name of the call or the caller), I need to have a custom zone which would be used by angular2, but I can't extend NgZone because it's not using DI for instanciation and thus the zone used by angular2 will still be NgZone.

Supamiu
  • 8,501
  • 7
  • 42
  • 76
  • 1
    Have you tried to expose angular zone like `class AppModule { constructor(private zone: NgZone) { (window).ngZone = zone; } }`? – yurzui Nov 07 '16 at 05:52
  • You can also get angular's zone using `Zone.current`. I'l edit the question to add my progression on the problem. – Supamiu Nov 07 '16 at 08:45
  • `Zone.current` will be angular zone only if you call it inside angular zone – yurzui Nov 07 '16 at 09:00
  • Yes and that's what I need, because my app is an angular2 module and it'll register components to get metrics on using a static register method. But if I need to get it outside of angular2, your comment will be useful to me :) Thank you. – Supamiu Nov 07 '16 at 09:02
  • `Forking the zone is not a problem but I can't connect it on angular2.` - Look at source code https://github.com/angular/angular/blob/2.1.2/modules/%40angular/core/src/application_ref.ts#L273 Then see this answer http://stackoverflow.com/questions/39686305/changing-shared-data-between-root-modules/39687157#39687157 (`_bootstrapModuleWithZone`) – yurzui Nov 07 '16 at 10:15
  • Awesome! I did not checked this part. Please explain what you said in the commens in an answer and I'll accept it. – Supamiu Nov 07 '16 at 10:19

1 Answers1

1

If you want run application with overrided ngZone you can do the following trick.

const platform = platformBrowserDynamic();
const zone = new MyZone();
platform['_bootstrapModuleWithZone'](AppModule, [], zone);

It's used within ng1 upgrade adapter

but unfortunately it is private method

See also

Community
  • 1
  • 1
yurzui
  • 205,937
  • 32
  • 433
  • 399