Skip to content

Commit

Permalink
perf(mdc): remove event listeners when components are destroyed
Browse files Browse the repository at this point in the history
  • Loading branch information
jgroth authored and adrianschmidt committed Sep 10, 2019
1 parent f0fd170 commit 770b014
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 27 deletions.
51 changes: 32 additions & 19 deletions src/components/dialog/dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ export class Dialog {

private id: string;

constructor() {
this.handleMdcOpened = this.handleMdcOpened.bind(this);
this.handleMdcClosed = this.handleMdcClosed.bind(this);
this.handleMdcClosing = this.handleMdcClosing.bind(this);
}

public componentWillLoad() {
this.id = createRandomString();
}
Expand All @@ -90,25 +96,9 @@ export class Dialog {
deactivate();
};

this.mdcDialog.listen('MDCDialog:opened', () => {
// When the opening-animation has completed, dispatch a
// resize-event so that any content that depends on
// javascript for layout has a chance to update to the
// final layout of the dialog. /Ads
dispatchResizeEvent();
});

this.mdcDialog.listen('MDCDialog:closed', () => {
if (this.open) {
this.close.emit();
}

this.open = false;
});

this.mdcDialog.listen('MDCDialog:closing', () => {
this.closing.emit();
});
this.mdcDialog.listen('MDCDialog:opened', this.handleMdcOpened);
this.mdcDialog.listen('MDCDialog:closed', this.handleMdcClosed);
this.mdcDialog.listen('MDCDialog:closing', this.handleMdcClosing);

this.mdcDialog.scrimClickAction = this.closingActions.scrimClick
? 'close'
Expand All @@ -119,6 +109,9 @@ export class Dialog {
}

public componentDidUnload() {
this.mdcDialog.unlisten('MDCDialog:opened', this.handleMdcOpened);
this.mdcDialog.unlisten('MDCDialog:closed', this.handleMdcClosed);
this.mdcDialog.unlisten('MDCDialog:closing', this.handleMdcClosing);
this.mdcDialog.destroy();
}

Expand Down Expand Up @@ -168,6 +161,26 @@ export class Dialog {
}
}

private handleMdcOpened() {
// When the opening-animation has completed, dispatch a
// resize-event so that any content that depends on
// javascript for layout has a chance to update to the
// final layout of the dialog. /Ads
dispatchResizeEvent();
}

private handleMdcClosed() {
if (this.open) {
this.close.emit();
}

this.open = false;
}

private handleMdcClosing() {
this.closing.emit();
}

private renderHeading() {
if (this.heading) {
return <h2 class="mdc-dialog__title">{this.heading.trim()}</h2>;
Expand Down
1 change: 1 addition & 0 deletions src/components/slider/slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class Slider {
}

public componentDidUnload() {
this.mdcSlider.unlisten('MDCSlider:change', this.changeHandler);
this.mdcSlider.destroy();
}

Expand Down
26 changes: 18 additions & 8 deletions src/components/snackbar/snackbar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { MDCSnackbar } from '@lime-material-16px/snackbar';
import {
MDCSnackbar,
MDCSnackbarCloseEvent,
} from '@lime-material-16px/snackbar';
import {
Component,
Element,
Expand Down Expand Up @@ -55,21 +58,20 @@ export class Snackbar {

private mdcSnackbar: MDCSnackbar;

constructor() {
this.handleMdcClosing = this.handleMdcClosing.bind(this);
}

public componentDidLoad() {
this.mdcSnackbar = new MDCSnackbar(
this.host.shadowRoot.querySelector('.mdc-snackbar')
);

this.mdcSnackbar.listen('MDCSnackbar:closing', event => {
if (event.detail.reason === 'action') {
this.action.emit();
} else {
this.hide.emit();
}
});
this.mdcSnackbar.listen('MDCSnackbar:closing', this.handleMdcClosing);
}

public componentDidUnload() {
this.mdcSnackbar.unlisten('MDCSnackbar:closing', this.handleMdcClosing);
this.mdcSnackbar.destroy();
}

Expand Down Expand Up @@ -109,6 +111,14 @@ export class Snackbar {
);
}

private handleMdcClosing(event: MDCSnackbarCloseEvent) {
if (event.detail.reason === 'action') {
this.action.emit();
} else {
this.hide.emit();
}
}

private renderAction(actionText) {
if (actionText) {
return (
Expand Down

0 comments on commit 770b014

Please sign in to comment.