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

add beforeDestroy hook #9933

Merged
merged 5 commits into from Dec 5, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added docs/developers/destroy_flowchart.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions docs/developers/plugins.md
Expand Up @@ -147,3 +147,10 @@ Plugins can interact with the chart throughout the render process. The rendering
Plugins can interact with the chart during the event handling process. The event handling flow is documented in the flowchart below. Each of the green processes is a plugin notification. If a plugin makes changes that require a re-render, the plugin can set `args.changed` to `true` to indicate that a render is needed. The built-in tooltip plugin uses this method to indicate when the tooltip has changed.

![Chart.js event handling flowchart](./event_flowchart.png)

### Chart destroy

Plugins are notified during the destroy process. These hooks can be used to destroy things that the plugin made and used during its life.
The `destroy` hook has been deprecated since Chart.js version 3.7.0, use the `afterDestroy` hook instead.

![Chart.js destroy flowchart](./destroy_flowchart.png)
4 changes: 4 additions & 0 deletions src/core/core.controller.js
Expand Up @@ -891,6 +891,7 @@ class Chart {
}

destroy() {
this.notifyPlugins('beforeDestroy');
const {canvas, ctx} = this;

this._stop();
Expand All @@ -904,9 +905,12 @@ class Chart {
this.ctx = null;
}

// TODO V4: delete destroy hook and reference to it in plugin flowchart
this.notifyPlugins('destroy');

delete instances[this.id];

this.notifyPlugins('afterDestroy');
}

toBase64Image(...args) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/core.plugins.js
Expand Up @@ -41,7 +41,7 @@ export default class PluginService {
const descriptors = filter ? this._descriptors(chart).filter(filter) : this._descriptors(chart);
const result = this._notify(descriptors, chart, hook, args);

if (hook === 'destroy') {
if (hook === 'afterDestroy') {
this._notify(descriptors, chart, 'stop');
this._notify(this._init, chart, 'uninstall');
}
Expand Down
15 changes: 15 additions & 0 deletions types/index.esm.d.ts
Expand Up @@ -1053,13 +1053,28 @@ export interface Plugin<TType extends ChartType = ChartType, O = AnyObject> exte
* @param {object} options - The plugin options.
*/
resize?(chart: Chart, args: { size: { width: number, height: number } }, options: O): void;
/**
* Called before the chart is being destroyed.
* @param {Chart} chart - The chart instance.
* @param {object} args - The call arguments.
* @param {object} options - The plugin options.
*/
beforeDestroy?(chart: Chart, args: EmptyObject, options: O): void;
/**
* Called after the chart has been destroyed.
* @param {Chart} chart - The chart instance.
* @param {object} args - The call arguments.
* @param {object} options - The plugin options.
* @deprecated since version 3.7.0 in favour of afterDestroy
*/
destroy?(chart: Chart, args: EmptyObject, options: O): void;
/**
* Called after the chart has been destroyed.
* @param {Chart} chart - The chart instance.
* @param {object} args - The call arguments.
* @param {object} options - The plugin options.
*/
afterDestroy?(chart: Chart, args: EmptyObject, options: O): void;
/**
* Called after chart is destroyed on all plugins that were installed for that chart. This hook is also invoked for disabled plugins (options === false).
* @param {Chart} chart - The chart instance.
Expand Down