I have an async function here, since the messageQueue is changed outside, I want to know if it's safe to be written like this.
this.messageQueue.push(message1);
onChatMessage();
... // some other code
this.messageQueue.push(message2);
onChatMessage();
... // some other code
this.messageQueue.push(message3);
onChatMessage();
...
onChatMessage: async function () {
if (this.isProcessMsgQ) { return; }
this.isProcessMsgQ = true;
for (let i = 0; i < this.messageQueue.length; i++) {
const msg = this.messageQueue[i];
try {
await this.processMessage(msg);
} catch (err) {
}
}
this.messageQueue = [];
this.isProcessMsgQ = false;
},
my worry is that, for thread1, right after this.messageQueue = []; and before this.isProcessMsgQ = false;, could thread2 finish the process from adding message2 to the queue, going into onChatMessage, checking this.isProcessMsgQ, and return?
if yes, then there's the case that messsage2 will not be processed, any idea to deal with it?
Thanks!