Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(elements): elements should run in correct zone (#24181) #37814

Closed
wants to merge 1 commit into from

Conversation

remackgeek
Copy link
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.io application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

When an angular element is created outside the angular zone, such as in setTimeout(), automatic change detection no longer works

Issue Number: #24181

What is the new behavior?

ComponentNgElementStrategy captures the zone it was created in and insures all methods are run in that zone.

Does this PR introduce a breaking change?

  • Yes
  • [x ] No

Other information

@kseamon
Copy link
Contributor

kseamon commented Jun 29, 2020

As I had commented way back, what this is still missing is exiting the NgZone in outputs - we don't want to leak our app's NgZone to the parent app (which would cause unrelated activities in the parent app to trigger change detection within the wrapped child application).

@gkalpak
Copy link
Member

gkalpak commented Jun 29, 2020

For reference: There is some relevant discussion in #24185.

@gkalpak
Copy link
Member

gkalpak commented Jun 29, 2020

@kseamon, can you give an example of the problem (I don't quite understand how we can "leak" our Zone).
Also, is this issue something that is introduced by this PR? Or is it possible that this "Zone leaking" can happen today (without the PR)?

@kseamon
Copy link
Contributor

kseamon commented Jun 29, 2020

It would look something like this:

// Parent app
ngElementNode.addEventListener('customEvent', () => {
  setTimeout(() => {
    doSomethingUnrelated();
    // Whoops we just triggered CD within the Angular Element.
  });
});

It would be possible for this to happen before if the code inside of the Angular Element manually entered the NgZone. This PR just makes it more likely to be an issue, and I think it's something that the framework should take care of for us (just like entering the NgZone in the first place).

@gkalpak
Copy link
Member

gkalpak commented Jun 30, 2020

Since this change is unrelated to this PR, I consider it outside of scope for this PR. Feel free to create an issue about so we can discuss there.

On a quick note:

I wouldn't expect Zones to be leaked the way you described (neither with nor without the changes in this PR). If I am not mistaken, what matters is the Zone in which you subscribe to the observable and this happens in the CustomElement's connectedCallback() and is not affected by the strategy's runInZone().

If anything, we might need to enter the Zone in the event listener callback, if the element is created in a way that causes its connectedCallback() method to be called outside of the Angular Zone 😛

I only took a quick look, so I may have missed something. We should take a closer look and look at concrete examples (both with and without CustomElement polyfills).

@gkalpak gkalpak requested review from gkalpak and removed request for andrewseguin June 30, 2020 13:56
@gkalpak gkalpak added area: elements Issues related to Angular Elements action: review The PR is still awaiting reviews from at least one requested reviewer target: patch This PR is targeted for the next patch release type: bug/fix labels Jun 30, 2020
@ngbot ngbot bot modified the milestone: needsTriage Jun 30, 2020
Copy link
Member

@gkalpak gkalpak left a comment

Choose a reason for hiding this comment

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

Thx for working on this, @remackgeek!

I think this is heading into the right direction. I have left a few minor comments.
Can you also please rebase on latest master and squash the commits into one?

BTW, it would be great to have a more realistic test via an actual example app (either in packages/examples/ or in integration/ (similar to or as part of integration/ng_elements/)), but that might be too much to ask for this PR 😁

packages/elements/src/component-factory-strategy.ts Outdated Show resolved Hide resolved
packages/elements/src/component-factory-strategy.ts Outdated Show resolved Hide resolved
packages/elements/src/component-factory-strategy.ts Outdated Show resolved Hide resolved
packages/elements/src/component-factory-strategy.ts Outdated Show resolved Hide resolved
packages/elements/src/component-factory-strategy.ts Outdated Show resolved Hide resolved
packages/elements/test/component-factory-strategy_spec.ts Outdated Show resolved Hide resolved
packages/elements/test/component-factory-strategy_spec.ts Outdated Show resolved Hide resolved
packages/elements/test/component-factory-strategy_spec.ts Outdated Show resolved Hide resolved
packages/elements/test/component-factory-strategy_spec.ts Outdated Show resolved Hide resolved
@remackgeek remackgeek force-pushed the elements-zone branch 2 times, most recently from c318b26 to 5d2f4e4 Compare July 4, 2020 02:34
@remackgeek remackgeek requested a review from gkalpak July 4, 2020 02:37
Copy link
Member

@gkalpak gkalpak left a comment

Choose a reason for hiding this comment

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

LGTM - Thx, @remackgeek
Can you please update goldens/size-tracking/aio-payloads.json#L15 to the new size?

For reference, this PR increases the angular.io main bundle by 295B (451100B --> 451395B). I inspected the bundle and confirmed that the only difference is the updated ComponentFactoryNgStrategy class.

@gkalpak gkalpak added action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Jul 6, 2020
Copy link
Member

@jelbourn jelbourn left a comment

Choose a reason for hiding this comment

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

LGTM

Reviewed-for: size-tracking

@petebacondarwin
Copy link
Member

Can you rebase and fix the size-tracking conflict so that we can approve for size-tracking?

@remackgeek remackgeek force-pushed the elements-zone branch 3 times, most recently from b7b462a to 02514cf Compare July 17, 2020 01:35
Default change detection fails in some cases for @angular/elements where
component events are called from the wrong zone.

This fixes the issue by running all ComponentNgElementStrategy methods
in the same zone it was created in.

Fixes angular#24181
@remackgeek
Copy link
Contributor Author

Rebased, broken size tracking changed to match actuals (not sure what else to put there).

Copy link
Member

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

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

Reviewed-for: size-tracking

@petebacondarwin
Copy link
Member

Awesome thanks!

@petebacondarwin petebacondarwin added action: presubmit The PR is in need of a google3 presubmit and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Jul 17, 2020
@kyliau
Copy link
Contributor

kyliau commented Jul 17, 2020

@AndrewKushnir
Copy link
Contributor

AndrewKushnir commented Jul 21, 2020

Presubmit #2 + Global Presubmit.

@AndrewKushnir AndrewKushnir removed the action: presubmit The PR is in need of a google3 presubmit label Jul 22, 2020
@gkalpak gkalpak added the action: merge The PR is ready for merge by the caretaker label Jul 22, 2020
@mhevery mhevery added target: major This PR is targeted for the next major release and removed target: patch This PR is targeted for the next patch release labels Jul 23, 2020
@mhevery mhevery closed this in 8df888d Jul 23, 2020
@mhevery mhevery added target: patch This PR is targeted for the next patch release and removed target: major This PR is targeted for the next major release labels Jul 23, 2020
mhevery pushed a commit to mhevery/angular that referenced this pull request Jul 23, 2020
Default change detection fails in some cases for @angular/elements where
component events are called from the wrong zone.

This fixes the issue by running all ComponentNgElementStrategy methods
in the same zone it was created in.

Fixes angular#24181

PR Close angular#37814
Splaktar pushed a commit to angular-hispano/angular that referenced this pull request Aug 8, 2020
Default change detection fails in some cases for @angular/elements where
component events are called from the wrong zone.

This fixes the issue by running all ComponentNgElementStrategy methods
in the same zone it was created in.

Fixes angular#24181

PR Close angular#37814
@angular-automatic-lock-bot
Copy link

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 Aug 23, 2020
profanis pushed a commit to profanis/angular that referenced this pull request Sep 5, 2020
Default change detection fails in some cases for @angular/elements where
component events are called from the wrong zone.

This fixes the issue by running all ComponentNgElementStrategy methods
in the same zone it was created in.

Fixes angular#24181

PR Close angular#37814
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 area: elements Issues related to Angular Elements cla: yes target: patch This PR is targeted for the next patch release type: bug/fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants