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(fxLayoutGap): mutation observer should run outside the ngZone #370

Merged
merged 1 commit into from
Aug 9, 2017

Conversation

ThomasBurleson
Copy link
Contributor

Inside the ngZone, the mutation observer and _updateWithValue() style changes causes an infinite recursion. MutationObserver handlers should be executed outside the ngZone.

Fixes #329.

@ThomasBurleson
Copy link
Contributor Author

@crisbeto - would you mind reviewing these?

@ThomasBurleson ThomasBurleson added this to the v2.0.0-beta.9 milestone Aug 8, 2017
@@ -15,7 +15,7 @@ import {
Self,
AfterContentInit,
Optional,
OnDestroy,
OnDestroy, NgZone,
Copy link
Member

Choose a reason for hiding this comment

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

This one should be on the next line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

this._zone.runOutsideAngular(() => {

let onMutationCallback = (mutations) => {
let validatedChanges = (it: MutationRecord) => {
Copy link
Member

Choose a reason for hiding this comment

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

Since this is only used once it can be inlined, but I guess that's down to preference. Same goes for the onMutationCallback.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed.

};

// update gap styles only for child 'added' or 'removed' events
if (mutations.filter(validatedChanges).length) {
Copy link
Member

Choose a reason for hiding this comment

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

Instead of using mutation.filter(...).length, you can use mutations.some(...) which will exit earlier.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

if (mutations.filter(validatedChanges).length) {
this._updateWithValue();
if (typeof MutationObserver !== 'undefined') {
this._observer = new MutationObserver(onMutationCallback.bind(this));
Copy link
Member

Choose a reason for hiding this comment

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

The bind(this) shouldn't be necessary since it's an arrow function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed.

this._zone.runOutsideAngular(() => {

let onMutationCallback = (mutations) => {
let validatedChanges = (it: MutationRecord) => {
Copy link
Member

Choose a reason for hiding this comment

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

I believe this is a MutationRecord[], not a single MutationRecord.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

improved.

Inside the ngZone, the mutation observer and _updateWithValue() style changes causes an infinite recursion. MutationObserver handlers should be executed outside the ngZone.

Fixes #329.
@ThomasBurleson
Copy link
Contributor Author

@crisbeto - nice feedback; as usually. Changes complete.

Copy link
Member

@crisbeto crisbeto left a comment

Choose a reason for hiding this comment

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

LGTM

this._updateWithValue();
this._zone.runOutsideAngular(() => {

if (typeof MutationObserver !== 'undefined') {
Copy link
Member

Choose a reason for hiding this comment

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

Not a big deal, but you can move the MutationObserver support check outside of the runOutsideAngular to avoid hitting runOutsideAngular completely if it's unsupported.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

true. minor nit. agreed ?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah it's pretty minor, no need to change it.

@ThomasBurleson ThomasBurleson added pr: lgtm This PR has been approved by the reviewer pr: needs presubmit labels Aug 8, 2017
crisbeto added a commit to crisbeto/material2 that referenced this pull request Aug 8, 2017
* Runs the underlying `MutationObserver` outside the Angular zone.
* Runs the debounce timeout outside the Angular zone.

via angular/flex-layout#370.
mmalerba pushed a commit to angular/components that referenced this pull request Aug 8, 2017
* Runs the underlying `MutationObserver` outside the Angular zone.
* Runs the debounce timeout outside the Angular zone.

via angular/flex-layout#370.
@mmalerba mmalerba merged commit 9fb0877 into master Aug 9, 2017
crisbeto added a commit to crisbeto/material2 that referenced this pull request Aug 9, 2017
* Runs the underlying `MutationObserver` outside the Angular zone.
* Runs the debounce timeout outside the Angular zone.

via angular/flex-layout#370.
mmalerba pushed a commit to angular/components that referenced this pull request Aug 9, 2017
* Runs the underlying `MutationObserver` outside the Angular zone.
* Runs the debounce timeout outside the Angular zone.

via angular/flex-layout#370.
@ThomasBurleson ThomasBurleson deleted the thomas/issue-329 branch September 13, 2017 22:13
@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 Sep 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes pr: lgtm This PR has been approved by the reviewer pr: needs presubmit
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Strange bug: when using fxLayoutGap on container and *ngFor on child
4 participants