Skip to content

Fix: #198 - Error thrown in ngOnDestroy if plot not yet initialized #246

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

Merged
merged 1 commit into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions projects/plotly/src/lib/plotly.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,9 @@ describe('PlotlyComponent', () => {
expect(component.loadTheme).not.toHaveBeenCalled();
expect(component.themeLoader.load).not.toHaveBeenCalledOnceWith('plotly_dark');
});

it('should not cause errors if ngOnDestroy is called before plotly is initialized', () => {
// note that this test intentionally does not call ngOnInit/whenStable
expect(() => component.ngOnDestroy()).not.toThrow();
});
});
8 changes: 5 additions & 3 deletions projects/plotly/src/lib/plotly.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,11 @@ export class PlotlyComponent implements OnInit, OnChanges, OnDestroy, DoCheck {
this.resizeHandler = undefined;
}

const figure = this.createFigure();
this.purge.emit(figure);
PlotlyService.remove(this.plotlyInstance!);
if (this.plotlyInstance) {
const figure = this.createFigure();
this.purge.emit(figure);
PlotlyService.remove(this.plotlyInstance);
}
}

ngOnChanges(changes: SimpleChanges): void {
Expand Down