Skip to content

fix(aio): revert resize to HostListener; delete device svc#16143

Closed
wardbell wants to merge 1 commit intoangular:masterfrom
IdeaBlade:aio-fix-resize-logic
Closed

fix(aio): revert resize to HostListener; delete device svc#16143
wardbell wants to merge 1 commit intoangular:masterfrom
IdeaBlade:aio-fix-resize-logic

Conversation

@wardbell
Copy link
Copy Markdown
Contributor

@wardbell wardbell commented Apr 19, 2017

Angular change detection bug -> no page update on resize.
Reverting to @HostListener('window:resize', ['$event.target.innerWidth']) cures it.
Delete DeviceService which no longer serves a purpose.
Adjusted affected AppComponent and LiveExample tests.

To see bug, checkout the 2nd commit from PR #15887 (or any subsequent commit before this PR) and do the following:

  • Make browser narrower than 1032 (the trigger width)
  • Nav to any guide page
  • Note that there are no top menu items
  • Make browser wider than 1032.
  • Note that there are still no top menu items.
  • Click anywhere on the page. Now the top menu and side nav appear ... as they should have after resize.

Set a breakpoint in AppComponent#onResize

  onResize(width) {
    this.isSideBySide = width > this.deviceService.sideBySideWidth;
  }

This will confirm that the event is caught and propagated via DeviceService to the method. The value of this.isSideBySide becomes true, the template binding should trigger nav panels to appear. But they don't. ChangeDetectorRef.detectChanges() and setTimeout tricks are fruitless.

I gave up wondering why. Reversion to the following gives the expected behavior.

  @HostListener('window:resize', ['$event.target.innerWidth'])
  onResize(width) {
    this.isSideBySide = width > this.sideBySideWidth;
  }

Project timeline dictates moving forward and leaving the lingering questions unanswered.

Please check if the PR fulfills these requirements

What kind of change does this PR introduce? (check one with "x")

[x] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Other... Please describe:

@wardbell wardbell self-assigned this Apr 19, 2017
@mary-poppins
Copy link
Copy Markdown

The angular.io preview for 6c539976ad4e1cecb88472ddb555a1eefa2e659b is available here.

@wardbell wardbell changed the title fix(air): revert resize to HostListener; delete device svc fix(aio): revert resize to HostListener; delete device svc Apr 19, 2017
Angular change detection bug -> no page update on resize.
Reverting to `@HostListener('window:resize', ['$event.target.innerWidth'])` cures it.
Delete DeviceService which no longer serves a purpose.
Adjusted affected AppComponent and LiveExample tests.
@wardbell wardbell force-pushed the aio-fix-resize-logic branch from 6c53997 to 06aef2d Compare April 19, 2017 07:17
@wardbell wardbell added the action: review The PR is still awaiting reviews from at least one requested reviewer label Apr 19, 2017
@mary-poppins
Copy link
Copy Markdown

The angular.io preview for 06aef2d is available here.

@@ -62,6 +51,8 @@ describe('LiveExampleComponent', () => {
liveExampleDe.nativeElement.liveExampleContent = liveExampleContent;

fixture.detectChanges();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary?

}
}

onResize(width?: number) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't you use @HostListener here? Would that work?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. I tried it. Angular does not activate that decorator on a service. :( I think it should but that would be a new feature.

@IgorMinar
Copy link
Copy Markdown
Contributor

I reduced the original issue to angular/zone.js#741, but I do agree with @gkalpak that the host listener solution is WAY cleaner.

@IgorMinar IgorMinar self-requested a review April 19, 2017 13:28
@IgorMinar IgorMinar added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Apr 19, 2017
@JiaLiPassion
Copy link
Copy Markdown
Contributor

@wardbell , @IgorMinar , I just tried angular/aio, I think maybe the problem is angular/aio/package.json still use [email protected], after upgrade to [email protected], the problem is gone. Because in [email protected], window.onProperty is also patched by zone.js with this PR,angular/zone.js#687.

@gkalpak
Copy link
Copy Markdown
Member

gkalpak commented Apr 19, 2017

@JiaLiPassion, angular/zone#687 still doesn't seem to patch window.onresize (which is what we care about here).

@JiaLiPassion
Copy link
Copy Markdown
Contributor

JiaLiPassion commented Apr 19, 2017

@gkalpak , I just tried with the example here like this.

<!DOCTYPE html>
<html>
<head>
  <script src="./zone.js"></script>
  <script>
    Zone.current.fork({name: 'resize'}).run(function() {
      window.onresize = function() {
        console.log('Zone name is ', Zone.current.name);
      }
    });
  </script>
</head>
<body>
</body>
</html>>

The output is

Zone name is resize

And I also tried angular/aio, and debug at DeviceService

export class DeviceService {

  // Show sidenav next to the main doc when display width on current device is greater than this.
  readonly sideBySideWidth = 1032;

  displayWidth = new ReplaySubject<number>(1);

  constructor() {

    if (window) {
      window.onresize = () => this.onResize();
      this.onResize();
    } else {
      // when no window, pretend the display is wide.
      this.onResize(this.sideBySideWidth + 1);
    }
  }

window.onresize is patched by zone.js and the Zone.current.name is angular.

Here is the screenshot.

screenshot2017-04-20 0 12 38

@gkalpak
Copy link
Copy Markdown
Member

gkalpak commented Apr 19, 2017

@JiaLiPassion, you are right. I assumed that patchOnProperty would only patch the properties passed to it as a second argument, but it turns out is patches all on... properties it can find on the target.

In that case, upgrading to zone.js 0.8.5 should indeed solve the issue.

@JiaLiPassion
Copy link
Copy Markdown
Contributor

@gkalpak , yes, it will loop all properties which starts with on, and try to patch them.
I have tried with the commit before this PR, with zone.js 0.8.4, resize not work, after upgrade to 0.8.5, it worked.

Please check it. Thanks!

@IgorMinar
Copy link
Copy Markdown
Contributor

@JiaLiPassion great work! Thank you! We'll still go ahead with merge in this PR which simplifies our code greatly, but it's good to know that the newer version of zones doesn't suffer from this problem.

@JiaLiPassion
Copy link
Copy Markdown
Contributor

@IgorMinar , sure, glad to help!

@mhevery mhevery closed this in f99cb96 Apr 20, 2017
@IgorMinar
Copy link
Copy Markdown
Contributor

IgorMinar commented Apr 20, 2017 via email

@wardbell wardbell deleted the aio-fix-resize-logic branch April 21, 2017 18:34
asnowwolf pushed a commit to asnowwolf/angular that referenced this pull request Aug 11, 2017
…6143)

Angular change detection bug -> no page update on resize.
Reverting to `@HostListener('window:resize', ['$event.target.innerWidth'])` cures it.
Delete DeviceService which no longer serves a purpose.
Adjusted affected AppComponent and LiveExample tests.

PR Close angular#16143
juleskremer pushed a commit to juleskremer/angular that referenced this pull request Aug 28, 2017
…6143)

Angular change detection bug -> no page update on resize.
Reverting to `@HostListener('window:resize', ['$event.target.innerWidth'])` cures it.
Delete DeviceService which no longer serves a purpose.
Adjusted affected AppComponent and LiveExample tests.

PR Close angular#16143
@angular-automatic-lock-bot
Copy link
Copy Markdown

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators Sep 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker cla: yes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants