Skip to content

Commit

Permalink
fix(data): fix worker mixing data for multiple charts
Browse files Browse the repository at this point in the history
  • Loading branch information
hatemhosny committed Sep 1, 2024
1 parent 416ce1a commit 5160018
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 16 deletions.
18 changes: 8 additions & 10 deletions src/lib/utils/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { loadData } from '../load-data';
// eslint-disable-next-line import/no-internal-modules
import workerSrc from '../../../tmp/racing-bars.worker.js';
import { getDates, getNextDate } from './dates';
import { createWorkerFromContent } from './utils';
import { createWorkerFromContent, generateId } from './utils';

const worker = createWorkerFromContent(workerSrc);

Expand All @@ -22,22 +22,20 @@ export async function prepareData(
}
data = dataTransform(await data);
}
const messageId = generateId();
worker.postMessage({
type: 'prepare-data',
data,
options: removeFnOptions(store.getState().options),
baseUrl: location.href,
messageId,
});
const preparedData = await new Promise<Data[]>((resolve) => {
worker.addEventListener(
'message',
(event) => {
if (event.data.type === 'data-prepared') {
resolve(event.data.data);
}
},
{ once: true },
);
worker.addEventListener('message', (event) => {
if (event.data.type === 'data-prepared' && event.data.messageId === messageId) {
resolve(event.data.data);
}
});
});
storeDataCollections(preparedData, store, changingOptions);
return preparedData;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { prepareData } from './prepare-data';
const worker: Worker = self as any as Worker;

worker.addEventListener('message', async (event) => {
const { type, data, options, baseUrl } = event.data;
const { type, data, options, baseUrl, messageId } = event.data;
if (type === 'prepare-data') {
const result = await prepareData(data, options, baseUrl);
worker.postMessage({ type: 'data-prepared', data: result });
worker.postMessage({ type: 'data-prepared', data: result, messageId });
}
});
2 changes: 1 addition & 1 deletion website/docs/guides/multiple-charts.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Example:

<div className="gallery">
<RacingBars
dataUrl="/data/population.csv"
dataUrl="/data/brands.csv"
dataType="csv"
title="Chart 1"
labelsPosition="outside"
Expand Down
3 changes: 0 additions & 3 deletions website/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ icon: `https://flagsapi.com/${d.code}/flat/64.png`,
<div className="gallery">
<RacingBars
style={{width: 800, height: 450}}

dataUrl="/data/population.csv"
dataType="csv"
dataTransform={transformFn}
Expand All @@ -57,7 +56,5 @@ icon: `https://flagsapi.com/${d.code}/flat/64.png`,
...d,
icon: \`https://flagsapi.com/\${d.code}/flat/64.png\`,
}))`}}

/>

</div>

0 comments on commit 5160018

Please sign in to comment.