1

Issue

We use a Angular cell component which contains a button. I noticed that sometimes (approximately 1 out of 10 times) the button event is handled outside of the zone which leads to issues.

What I've found out so far

agInit sometimes runs outside of the zone. I found that out by using the following snippet:

agInit(params: RendererParams): void {
  const inZone = NgZone.isInAngularZone();
  console.log("AgInit in Zone?: " + inZone);
}

If agInit is not in the angular zone, the button callback is also not in the zone.

I also noticed that the callstack is slightly different in the case where agInit runs inside our outside the zone:

enter image description here

Here is a link to a full diff of both stack traces: https://www.diffchecker.com/e54rZEsC

I also noticed that in the error case, the callstack start from Utils.debounce in agGrid.

To further investigate the issue, I forked the ngzone (see snippet below). No errors were logged.

ngDoBootstrap(applicationRef: ApplicationRef) {
  const debugSpec: ZoneSpec = {
    name: "debugSpec",
    onHandleError: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, error: any) => {
      console.log(error);
      return true;
    }
  };

  Zone.current.fork(debugSpec).run(() => {
    applicationRef.bootstrap(AppComponent);
  });
}

Workaround

I can always manually run the callback within the zone. That works.

chriskohler
  • 155
  • 1
  • 9
  • What is the issue you are facing when it runs outside the zone? – Shravan Mar 04 '20 at 10:11
  • @Shravan yes. If agInit runs outside the zone, the button callback runs outside the zone which leads to issues. – chriskohler Mar 04 '20 at 10:14
  • @Shravan sorry, misread your question. In general could be any issue. In our case we navigate to another page and since the navigation is triggered outside the zone the page is not initialised correctly. – chriskohler Mar 04 '20 at 10:37
  • But how it relates to ag-grid if you have an issue with component initialization and life-circle itself? Also please provide stackblitz or plinkr sample - it would be much simpler to help. – un.spike Mar 04 '20 at 12:17
  • Because with ag-grid you can't use the angular lifecycle hooks but the ag-grid hooks like agInit. See: https://github.com/ag-grid/ag-grid/blob/master/packages/ag-grid-angular/src/angularFrameworkComponentWrapper.ts#L74 I tried to create a stackblitz. Problem is that it's already hard to reproduce locally (only works 1/10) and I couldn't reproduce with a simple example. – chriskohler Mar 04 '20 at 12:37

1 Answers1

0

I found the the problem. Since it's hard to reproduce I am not 100% sure though. I think it is a AgGrid bug.

Why?

  1. I noticed that if it is outside the zone its called by Utils.debounce
  2. Then i check where ag grid calls it. In around 6 places. One of them was in a ResizeObserver callback
  3. Angular zone monkeypatches setTimeout, but not ResizeObserver
  4. I could toggle between ResizeObserver and and polyfill which relies on setTimeout by using suppressBrowserResizeObserverin the gridSettings
  5. I wrote an automation to refresh the browser in an endless loop and stop when the error happens
  6. I could always with max 10 refresh reproduce the error with ResizeObserver enabled
  7. I could never reproduce the error using the polyfill

Will open an issue on github.

How i fixed the issue

I added the following import:

import 'zone.js/dist/zone-patch-resize-observer';
chriskohler
  • 155
  • 1
  • 9
  • Related issue: https://github.com/ag-grid/ag-grid/issues/3709 – chriskohler Mar 04 '20 at 18:19
  • Solved now? With zone resize listener? Btw ag-grid doesn’t support tickets via github anymore. – un.spike Mar 05 '20 at 07:13
  • yes, importing zone-patch-resize-observer solved the issue. Ok, good to know. Here it says issues can be still reported on github though: https://github.com/ag-grid/ag-grid#issue-reporting – chriskohler Mar 05 '20 at 07:19
  • Sorry, tickets - supported, but support wouldn't be provided – un.spike Mar 05 '20 at 07:22
  • 1
    Thank you for your help. I also added my solution in the answer. As stated in the github issue, I don't think ag grid should fix it, but at the same time can't rely on a system which is not working by default in Angular. It should be documented. – chriskohler Mar 05 '20 at 07:40