]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Do not notify plugins after their uninstall function has been called (#12098) master
authorBojidar Marinov <bojidar.marinov.bg@gmail.com>
Sat, 12 Jul 2025 16:29:51 +0000 (19:29 +0300)
committerGitHub <noreply@github.com>
Sat, 12 Jul 2025 16:29:51 +0000 (12:29 -0400)
Fixes #12032

src/core/core.plugins.js
test/specs/core.plugin.tests.js

index 6de82758944517112d4db2c51a8e85c758810681..c73a2a1c2a245ce9f7a5f36c4fe9ff6a8ae70654 100644 (file)
@@ -19,7 +19,7 @@ import {callback as callCallback, isNullOrUndef, valueOrDefault} from '../helper
 
 export default class PluginService {
   constructor() {
-    this._init = [];
+    this._init = undefined;
   }
 
   /**
@@ -38,12 +38,17 @@ export default class PluginService {
       this._notify(this._init, chart, 'install');
     }
 
+    if (this._init === undefined) { // Do not trigger events before install
+      return;
+    }
+
     const descriptors = filter ? this._descriptors(chart).filter(filter) : this._descriptors(chart);
     const result = this._notify(descriptors, chart, hook, args);
 
     if (hook === 'afterDestroy') {
       this._notify(descriptors, chart, 'stop');
       this._notify(this._init, chart, 'uninstall');
+      this._init = undefined; // Do not trigger events after uninstall
     }
     return result;
   }
index 4b06e6f432b5d1d79042f1ee15592d9b0433128e..76f7b7109b1f83aa8c93131b51ea6b0ab28771d8 100644 (file)
@@ -480,5 +480,31 @@ describe('Chart.plugins', function() {
       await jasmine.triggerMouseEvent(chart, 'pointerleave', {x: 0, y: 0});
       expect(results).toEqual(['beforetest', 'aftertest', 'beforemouseout', 'aftermouseout']);
     });
+
+    it('should not call plugins after uninstall', async function() {
+      const results = [];
+      const chart = window.acquireChart({
+        options: {
+          events: ['test'],
+          plugins: {
+            testPlugin: {
+              events: ['test']
+            }
+          }
+        },
+        plugins: [{
+          id: 'testPlugin',
+          reset: () => results.push('reset'),
+          afterDestroy: () => results.push('afterDestroy'),
+          uninstall: () => results.push('uninstall'),
+        }]
+      });
+      chart.reset();
+      expect(results).toEqual(['reset']);
+      chart.destroy();
+      expect(results).toEqual(['reset', 'afterDestroy', 'uninstall']);
+      chart.reset();
+      expect(results).toEqual(['reset', 'afterDestroy', 'uninstall']);
+    });
   });
 });