File: BasicChartWebWorker.js

package info (click to toggle)
node-chart.js 3.9.1%2B~0.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,004 kB
  • sloc: javascript: 55,371; sh: 76; makefile: 10
file content (27 lines) | stat: -rw-r--r-- 964 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// 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.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});
  }
};