Message sidebar will now only show the last packet of message
This commit is contained in:
parent
4b874dfff4
commit
3d91b1fb27
3 changed files with 76 additions and 31 deletions
69
public/workers/message_packet_worker.js
Normal file
69
public/workers/message_packet_worker.js
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
/**
|
||||||
|
* Web worker handling message API.
|
||||||
|
* When sending quickly multiple request,
|
||||||
|
* a single packet of message merging all request' responses is returned
|
||||||
|
*/
|
||||||
|
|
||||||
|
const DELAY_BEFORE_SEND = 200;
|
||||||
|
const queue = [];
|
||||||
|
let messagePacket = [];
|
||||||
|
onmessage = async(e) => {
|
||||||
|
let packetLength
|
||||||
|
|
||||||
|
const url = e.data.url;
|
||||||
|
const state = e.data.state;
|
||||||
|
const request = {
|
||||||
|
ApplicationState: state
|
||||||
|
};
|
||||||
|
const dataParsed = JSON.stringify(request, GetCircularReplacerKeepDataStructure());
|
||||||
|
queue.push(request);
|
||||||
|
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) => {
|
||||||
|
messagePacket.push.apply(messagePacket, json.messages);
|
||||||
|
queue.pop();
|
||||||
|
|
||||||
|
// The sleep allow the message packet to be filled by
|
||||||
|
// others requests before being sent as a single batch
|
||||||
|
// Reducing the wait time will reduce latency but increase error rate
|
||||||
|
let doLoop = true;
|
||||||
|
do {
|
||||||
|
packetLength = messagePacket.length;
|
||||||
|
await sleep(DELAY_BEFORE_SEND);
|
||||||
|
const newPacketLength = messagePacket.length;
|
||||||
|
doLoop = newPacketLength !== packetLength;
|
||||||
|
packetLength = newPacketLength;
|
||||||
|
} while (doLoop);
|
||||||
|
|
||||||
|
if (queue.length <= 0 && messagePacket.length > 0) {
|
||||||
|
console.debug(`[GetFeedback] Packet size before sent: ${messagePacket.length}`)
|
||||||
|
postMessage(messagePacket)
|
||||||
|
messagePacket.splice(0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
function sleep(ms) {
|
||||||
|
return new Promise(resolve => setTimeout(resolve, ms));
|
||||||
|
}
|
||||||
|
|
||||||
|
function GetCircularReplacerKeepDataStructure()
|
||||||
|
{
|
||||||
|
return (key, value) => {
|
||||||
|
if (key === 'parent') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
|
@ -1,16 +1,15 @@
|
||||||
const DELAY_BEFORE_SEND = 200;
|
/**
|
||||||
const queue = [];
|
* Web worker handling message API.
|
||||||
let messagePacket = [];
|
* Return a message of the request.
|
||||||
onmessage = async(e) => {
|
*/
|
||||||
let packetLength
|
|
||||||
|
|
||||||
|
onmessage = async(e) => {
|
||||||
const url = e.data.url;
|
const url = e.data.url;
|
||||||
const state = e.data.state;
|
const state = e.data.state;
|
||||||
const request = {
|
const request = {
|
||||||
ApplicationState: state
|
ApplicationState: state
|
||||||
};
|
};
|
||||||
const dataParsed = JSON.stringify(request, GetCircularReplacerKeepDataStructure());
|
const dataParsed = JSON.stringify(request, GetCircularReplacerKeepDataStructure());
|
||||||
queue.push(request);
|
|
||||||
fetch(url, {
|
fetch(url, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: new Headers({
|
headers: new Headers({
|
||||||
|
@ -23,33 +22,10 @@ onmessage = async(e) => {
|
||||||
response.json()
|
response.json()
|
||||||
)
|
)
|
||||||
.then(async(json) => {
|
.then(async(json) => {
|
||||||
messagePacket.push.apply(messagePacket, json.messages);
|
postMessage(json.messages)
|
||||||
queue.pop();
|
|
||||||
|
|
||||||
// The sleep allow the message packet to be filled by
|
|
||||||
// others requests before being sent as a single batch
|
|
||||||
// Reducing the wait time will reduce latency but increase error rate
|
|
||||||
let doLoop = true;
|
|
||||||
do {
|
|
||||||
packetLength = messagePacket.length;
|
|
||||||
await sleep(DELAY_BEFORE_SEND);
|
|
||||||
const newPacketLength = messagePacket.length;
|
|
||||||
doLoop = newPacketLength !== packetLength;
|
|
||||||
packetLength = newPacketLength;
|
|
||||||
} while (doLoop);
|
|
||||||
|
|
||||||
if (queue.length <= 0 && messagePacket.length > 0) {
|
|
||||||
console.debug(`[GetFeedback] Packet size before sent: ${messagePacket.length}`)
|
|
||||||
postMessage(messagePacket)
|
|
||||||
messagePacket.splice(0);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
function sleep(ms) {
|
|
||||||
return new Promise(resolve => setTimeout(resolve, ms));
|
|
||||||
}
|
|
||||||
|
|
||||||
function GetCircularReplacerKeepDataStructure()
|
function GetCircularReplacerKeepDataStructure()
|
||||||
{
|
{
|
||||||
return (key, value) => {
|
return (key, value) => {
|
||||||
|
|
|
@ -30,7 +30,7 @@ function UseWorker(
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
myWorker.onmessage = (event) => {
|
myWorker.onmessage = (event) => {
|
||||||
setMessages(messages.concat(event.data as IMessage[]));
|
setMessages(event.data as IMessage[]);
|
||||||
};
|
};
|
||||||
}, [messages, setMessages]);
|
}, [messages, setMessages]);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue