moribvndvs / ng2-idle

Responding to idle users in Angular (not AngularJS) applications.
https://moribvndvs.github.io/ng2-idle
Apache License 2.0
315 stars 128 forks source link

Angular 8 : Change detection #140

Closed JessFarmerVA closed 7 months ago

JessFarmerVA commented 4 years ago

Describe the bug Change detection is not running onIdleEnd.

To Reproduce

    idle.onIdleEnd.subscribe(() => {
      this.counter = 30;
      this.isIdle = false;
      this.status = 'Welcome back';
      this.reset();
    });

    idle.onTimeout.subscribe(() => {
      this.status = 'Signing out';
    });

    idle.onTimeoutWarning.subscribe(() => {
      this.isIdle = true;
      this.status = 'Signing out123';
      console.log('PREPARING TO SIGN OUT!!!   timer: ', this.counter--);
    });

Change detection runs for onTimeoutWarning, and onTimeout, but not on onIdleEnd. The DOM does not update with the new status variable.

Expected behavior Change detection should run and the DOM should update when I change variables inside onIdleEnd.

Please tell us about your environment Desktop: Browser: Chrome, IE, Edge

Additional context I looked at the code, my thoughts are it may have something to do with zone.run()

wingerlang commented 3 years ago

I've found this bug as well, any plan to patch and fix it?

Adelost commented 3 years ago

Me too. @JessFarmerVA, did you find any good workaround?

One ugly workaround I found is to use setInterval to force change detection to occur: E.g. by putting setInterval(() => {}, 1000) in the constructor of your root component.

But doing this of course causes the change detection to run many times unnecessarily, and could hamper overall performance depending on what intervall value you use.

JessFarmerVA commented 3 years ago

@Adelost I ended up not using onIdleEnd and only used onTimeoutWarning and onTimeout... I managed to get my desired functionality with just those two..

However, I do remember finding a solution that involved importing something that could force change detection, not using setInterval. I believe it was tick() or something. I remember there was a few different way to force change Detection, and the one that worked was kind of ugly looking. Unfortunately I do not remember what it was though, sorry.