svg-layout-designer-react/public/workers/message_worker.js
2022-10-12 17:16:08 +02:00

49 lines
1.1 KiB
JavaScript

/**
* Web worker handling message API.
* Return a message of the request.
*/
onmessage = async(e) => {
const url = e.data.url;
const state = e.data.state;
const request = {
ApplicationState: state
};
const dataParsed = JSON.stringify(request, GetCircularReplacerKeepDataStructure());
fetch(url, {
method: 'POST',
headers: new Headers({
// eslint-disable-next-line @typescript-eslint/naming-convention
'Content-Type': 'application/json'
}),
body: dataParsed
})
.then((response) =>
response.json()
)
.then(async(json) => {
postMessage(json.messages)
});
};
function GetCircularReplacerKeepDataStructure()
{
return (key, value) => {
if (key === 'parent') {
return;
}
if (key === 'containers') {
const containers = Array.from(value).map(([Key, Value]) => ({ Key, Value }));
return containers;
}
if (key === 'symbols') {
const symbols = Array.from(value).map(([Key, Value]) => ({ Key, Value }));
return symbols;
}
return value;
};
}