Skip to content

Commit 5baafac

Browse files
authored
Make Chart static properties and methods read only (#8431)
1 parent c4bc737 commit 5baafac

File tree

1 file changed

+48
-22
lines changed

1 file changed

+48
-22
lines changed

src/core/core.controller.js

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ function getCanvas(item) {
6666
return item;
6767
}
6868

69+
const instances = {};
70+
const getChart = (key) => {
71+
const canvas = getCanvas(key);
72+
return Object.values(instances).filter((c) => c.canvas === canvas).pop();
73+
};
74+
6975
class Chart {
7076

7177
// eslint-disable-next-line max-statements
@@ -74,7 +80,7 @@ class Chart {
7480

7581
this.config = config = new Config(config);
7682
const initialCanvas = getCanvas(item);
77-
const existingChart = Chart.getChart(initialCanvas);
83+
const existingChart = getChart(initialCanvas);
7884
if (existingChart) {
7985
throw new Error(
8086
'Canvas is already in use. Chart with ID \'' + existingChart.id + '\'' +
@@ -118,7 +124,7 @@ class Chart {
118124
this.$context = undefined;
119125

120126
// Add the chart instance to the global namespace
121-
Chart.instances[me.id] = me;
127+
instances[me.id] = me;
122128

123129
if (!context || !canvas) {
124130
// The given item is not a compatible context2d element, let's return before finalizing
@@ -840,7 +846,7 @@ class Chart {
840846

841847
me.notifyPlugins('destroy');
842848

843-
delete Chart.instances[me.id];
849+
delete instances[me.id];
844850
}
845851

846852
toBase64Image(...args) {
@@ -1090,27 +1096,47 @@ class Chart {
10901096
}
10911097
}
10921098

1093-
// These are available to both, UMD and ESM packages
1094-
Chart.defaults = defaults;
1095-
Chart.instances = {};
1096-
Chart.registry = registry;
1097-
Chart.version = version;
1098-
1099-
Chart.getChart = (key) => {
1100-
const canvas = getCanvas(key);
1101-
return Object.values(Chart.instances).filter((c) => c.canvas === canvas).pop();
1102-
};
1103-
11041099
// @ts-ignore
11051100
const invalidatePlugins = () => each(Chart.instances, (chart) => chart._plugins.invalidate());
11061101

1107-
Chart.register = (...items) => {
1108-
registry.add(...items);
1109-
invalidatePlugins();
1110-
};
1111-
Chart.unregister = (...items) => {
1112-
registry.remove(...items);
1113-
invalidatePlugins();
1114-
};
1102+
const enumerable = true;
1103+
1104+
// These are available to both, UMD and ESM packages. Read Only!
1105+
Object.defineProperties(Chart, {
1106+
defaults: {
1107+
enumerable,
1108+
value: defaults
1109+
},
1110+
instances: {
1111+
enumerable,
1112+
value: instances
1113+
},
1114+
registry: {
1115+
enumerable,
1116+
value: registry
1117+
},
1118+
version: {
1119+
enumerable,
1120+
value: version
1121+
},
1122+
getChart: {
1123+
enumerable,
1124+
value: getChart
1125+
},
1126+
register: {
1127+
enumerable,
1128+
value: (...items) => {
1129+
registry.add(...items);
1130+
invalidatePlugins();
1131+
}
1132+
},
1133+
unregister: {
1134+
enumerable,
1135+
value: (...items) => {
1136+
registry.remove(...items);
1137+
invalidatePlugins();
1138+
}
1139+
}
1140+
});
11151141

11161142
export default Chart;

0 commit comments

Comments
 (0)