Skip to content

Commit e7aec8c

Browse files
authored
add beforeDestroy hook (#9933)
* add `beforeDestroy` hook * add documentation of destroy lifecycle, add `afterDestroy` hook and deprecate destroy
1 parent d1daf1a commit e7aec8c

File tree

5 files changed

+27
-1
lines changed

5 files changed

+27
-1
lines changed
18.2 KB
Loading

docs/developers/plugins.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,10 @@ Plugins can interact with the chart throughout the render process. The rendering
147147
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.
148148

149149
![Chart.js event handling flowchart](./event_flowchart.png)
150+
151+
### Chart destroy
152+
153+
Plugins are notified during the destroy process. These hooks can be used to destroy things that the plugin made and used during its life.
154+
The `destroy` hook has been deprecated since Chart.js version 3.7.0, use the `afterDestroy` hook instead.
155+
156+
![Chart.js destroy flowchart](./destroy_flowchart.png)

src/core/core.controller.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,7 @@ class Chart {
891891
}
892892

893893
destroy() {
894+
this.notifyPlugins('beforeDestroy');
894895
const {canvas, ctx} = this;
895896

896897
this._stop();
@@ -904,9 +905,12 @@ class Chart {
904905
this.ctx = null;
905906
}
906907

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

909911
delete instances[this.id];
912+
913+
this.notifyPlugins('afterDestroy');
910914
}
911915

912916
toBase64Image(...args) {

src/core/core.plugins.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export default class PluginService {
4141
const descriptors = filter ? this._descriptors(chart).filter(filter) : this._descriptors(chart);
4242
const result = this._notify(descriptors, chart, hook, args);
4343

44-
if (hook === 'destroy') {
44+
if (hook === 'afterDestroy') {
4545
this._notify(descriptors, chart, 'stop');
4646
this._notify(this._init, chart, 'uninstall');
4747
}

types/index.esm.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,13 +1053,28 @@ export interface Plugin<TType extends ChartType = ChartType, O = AnyObject> exte
10531053
* @param {object} options - The plugin options.
10541054
*/
10551055
resize?(chart: Chart, args: { size: { width: number, height: number } }, options: O): void;
1056+
/**
1057+
* Called before the chart is being destroyed.
1058+
* @param {Chart} chart - The chart instance.
1059+
* @param {object} args - The call arguments.
1060+
* @param {object} options - The plugin options.
1061+
*/
1062+
beforeDestroy?(chart: Chart, args: EmptyObject, options: O): void;
10561063
/**
10571064
* Called after the chart has been destroyed.
10581065
* @param {Chart} chart - The chart instance.
10591066
* @param {object} args - The call arguments.
10601067
* @param {object} options - The plugin options.
1068+
* @deprecated since version 3.7.0 in favour of afterDestroy
10611069
*/
10621070
destroy?(chart: Chart, args: EmptyObject, options: O): void;
1071+
/**
1072+
* Called after the chart has been destroyed.
1073+
* @param {Chart} chart - The chart instance.
1074+
* @param {object} args - The call arguments.
1075+
* @param {object} options - The plugin options.
1076+
*/
1077+
afterDestroy?(chart: Chart, args: EmptyObject, options: O): void;
10631078
/**
10641079
* Called after chart is destroyed on all plugins that were installed for that chart. This hook is also invoked for disabled plugins (options === false).
10651080
* @param {Chart} chart - The chart instance.

0 commit comments

Comments
 (0)