-
Notifications
You must be signed in to change notification settings - Fork 772
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
Conversation
@crisbeto - would you mind reviewing these? |
src/lib/flexbox/api/layout-gap.ts
Outdated
@@ -15,7 +15,7 @@ import { | |||
Self, | |||
AfterContentInit, | |||
Optional, | |||
OnDestroy, | |||
OnDestroy, NgZone, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
src/lib/flexbox/api/layout-gap.ts
Outdated
this._zone.runOutsideAngular(() => { | ||
|
||
let onMutationCallback = (mutations) => { | ||
let validatedChanges = (it: MutationRecord) => { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed.
src/lib/flexbox/api/layout-gap.ts
Outdated
}; | ||
|
||
// update gap styles only for child 'added' or 'removed' events | ||
if (mutations.filter(validatedChanges).length) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
src/lib/flexbox/api/layout-gap.ts
Outdated
if (mutations.filter(validatedChanges).length) { | ||
this._updateWithValue(); | ||
if (typeof MutationObserver !== 'undefined') { | ||
this._observer = new MutationObserver(onMutationCallback.bind(this)); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed.
src/lib/flexbox/api/layout-gap.ts
Outdated
this._zone.runOutsideAngular(() => { | ||
|
||
let onMutationCallback = (mutations) => { | ||
let validatedChanges = (it: MutationRecord) => { |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
fe9d020
to
23f4f1d
Compare
@crisbeto - nice feedback; as usually. Changes complete. |
There was a problem hiding this 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') { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
true. minor nit. agreed ?
There was a problem hiding this comment.
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.
* Runs the underlying `MutationObserver` outside the Angular zone. * Runs the debounce timeout outside the Angular zone. via angular/flex-layout#370.
* Runs the underlying `MutationObserver` outside the Angular zone. * Runs the debounce timeout outside the Angular zone. via angular/flex-layout#370.
* Runs the underlying `MutationObserver` outside the Angular zone. * Runs the debounce timeout outside the Angular zone. via angular/flex-layout#370.
* Runs the underlying `MutationObserver` outside the Angular zone. * Runs the debounce timeout outside the Angular zone. via angular/flex-layout#370.
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Inside the ngZone, the mutation observer and _updateWithValue() style changes causes an infinite recursion. MutationObserver handlers should be executed outside the ngZone.
Fixes #329.