Chart.js/test/BasicChartWebWorker.js
Dan Onoshko ce375a6876
feat: add ESM support (#10525)
* feat: add ESM support

* build: rename UMD bundle

* chore: edit supbackages description

* style: disable es/no-import-meta linter rule

* test: dynamic import in cjs module

* docs: edit integrations page

* docs: review fixes

* chore: remove useless regex in webpack config

* ci: test size-limit only for ESM bundle
2022-08-04 18:43:26 -04:00

28 lines
968 B
JavaScript

// This file is a basic example of using a chart inside a web worker.
// All it creates a new chart from a transferred OffscreenCanvas and then assert that the correct platform type was
// used.
// Receives messages with data of type: { type: 'initialize', canvas: OffscreenCanvas }
// Sends messages with data of types: { type: 'success' } | { type: 'error', errorMessage: string }
// eslint-disable-next-line no-undef
importScripts('../src/chart.umd.js');
onmessage = function(event) {
try {
const {type, canvas} = event.data;
if (type !== 'initialize') {
throw new Error('invalid message type received by worker: ' + type);
}
const chart = new Chart(canvas);
if (!(chart.platform instanceof Chart.platforms.BasicPlatform)) {
throw new Error('did not use basic platform for chart in web worker');
}
postMessage({type: 'success'});
} catch (error) {
postMessage({type: 'error', errorMessage: error.stack});
}
};