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

Make Chart static properties and methods read only #8431

Merged
merged 1 commit into from
Feb 15, 2021
Merged
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
70 changes: 48 additions & 22 deletions src/core/core.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ function getCanvas(item) {
return item;
}

const instances = {};
const getChart = (key) => {
const canvas = getCanvas(key);
return Object.values(instances).filter((c) => c.canvas === canvas).pop();
};

class Chart {

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

this.config = config = new Config(config);
const initialCanvas = getCanvas(item);
const existingChart = Chart.getChart(initialCanvas);
const existingChart = getChart(initialCanvas);
if (existingChart) {
throw new Error(
'Canvas is already in use. Chart with ID \'' + existingChart.id + '\'' +
Expand Down Expand Up @@ -118,7 +124,7 @@ class Chart {
this.$context = undefined;

// Add the chart instance to the global namespace
Chart.instances[me.id] = me;
instances[me.id] = me;

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

me.notifyPlugins('destroy');

delete Chart.instances[me.id];
delete instances[me.id];
}

toBase64Image(...args) {
Expand Down Expand Up @@ -1090,27 +1096,47 @@ class Chart {
}
}

// These are available to both, UMD and ESM packages
Chart.defaults = defaults;
Chart.instances = {};
Chart.registry = registry;
Chart.version = version;

Chart.getChart = (key) => {
const canvas = getCanvas(key);
return Object.values(Chart.instances).filter((c) => c.canvas === canvas).pop();
};

// @ts-ignore
const invalidatePlugins = () => each(Chart.instances, (chart) => chart._plugins.invalidate());

Chart.register = (...items) => {
registry.add(...items);
invalidatePlugins();
};
Chart.unregister = (...items) => {
registry.remove(...items);
invalidatePlugins();
};
const enumerable = true;

// These are available to both, UMD and ESM packages. Read Only!
Object.defineProperties(Chart, {
defaults: {
enumerable,
value: defaults
},
instances: {
enumerable,
value: instances
},
registry: {
enumerable,
value: registry
},
version: {
enumerable,
value: version
},
getChart: {
enumerable,
value: getChart
},
register: {
enumerable,
value: (...items) => {
registry.add(...items);
invalidatePlugins();
}
},
unregister: {
enumerable,
value: (...items) => {
registry.remove(...items);
invalidatePlugins();
}
}
});

export default Chart;