Skip to content

Commit

Permalink
Merge pull request hatemhosny#179 from hatemhosny/fix-worker-multiple…
Browse files Browse the repository at this point in the history
…-messages

Fix worker multiple messages
  • Loading branch information
hatemhosny authored Sep 1, 2024
2 parents 416ce1a + b30dbb5 commit 7be19ff
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 16 deletions.
20 changes: 10 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,22 @@ 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 },
);
const onMessage = (event: MessageEvent) => {
if (event.data.type === 'data-prepared' && event.data.messageId === messageId) {
resolve(event.data.data);
worker.removeEventListener('message', onMessage);
}
};
worker.addEventListener('message', onMessage);
});
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>
2 changes: 2 additions & 0 deletions website/static/_headers
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/*
Access-Control-Allow-Origin: *

0 comments on commit 7be19ff

Please sign in to comment.