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

51 lines
1,014 B
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, GetCircularReplacer());
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 GetCircularReplacer()
{
return (key, value) => {
if (key === 'parent') {
return;
}
if (key === 'containers') {
return Array.from(value.entries());
}
if (key === 'symbols') {
return Array.from(value.entries());
}
if (key === 'linkedContainers') {
return Array.from(value);
}
return value;
};
}