Skip to main content
Version: 1.2.0

Web Workers

Web Workers are a simple means for web content to run scripts in background threads. The worker thread can perform tasks without interfering with the user interface. MDN

K2 treats web workers as first-class citizens, you can enable it by giving useWorker: true to any component and it'll transform your data in a separate thread. We're running a separate instance of web-worker per component.

Usage

import { BubbleChart } from "@k2/d3-viz";
function mapData(data, prevData, mapDataParams) {
// transform data
return transformedData;
}
<BubbleChart
{...props}
mapData={mapData}
mapDataParams={...params}
useWorker={true}
/>

Limitations

To support web workers we are using useWorker hook by alewin and there are some limitations for using this hook:

  1. The web worker doesn't have access to the document and window object
  2. mapData must be a pure function without any local dependencies as it gets serialized when using it in a web worker.
  3. mapData cannot return a function because the response gets serialized.
  4. mapDataParams cannot contain any function.