Currently, I am developing a chat application within a Vue project and experimenting with different features.
A fully-featured chat app must have the ability to live-update "seen" states. This means that when one user views the last sent message, the other user should see a previously grey dot next to their sent message change to the other person's profile picture (this is not crucial to the main issue).
I am utilizing socket.io to trigger various events back and forth as these updates occur.
The overall flow is working smoothly so far, except for one specific aspect:
messagesArray.value.findLast(function (message: any) {
if (message.showUnRead === true && message.senderID === data.senderID) {
delete message.showUnRead;
message.read = true;
}
if (message.showRead === true && message.senderID === data.senderID) {
delete message.showRead;
message.read = true;
}
Initially, this code functioned well. However, when I introduced changes to object values, it resulted in duplicating itself within the array.
Instead of just updating the
messagesArray.value
's last object, it updated that object and then added another identical one after it.
This issue only arises when I include the object value manipulation functions ('delete message.showRead' or 'message.read').
My assumption is that the findLast()
function returns the object itself and subsequently appends this returned object back into the array, causing duplication.
I would prefer not to use the array.pop()
method as this interferes with animations that are set up using transitiongroup
. Additionally, it seems redundant to modify an object's values, retrieve the object again, make changes, and then manually delete the duplicate. It may be more efficient to find a different approach.
Ultimately, my goal is to solely update specific key-value pairs of the located (findLast()
) object.
Therefore, my question is:
Is there a built-in function that accomplishes this task?
If such a function does not exist, how should I tackle this problem without experiencing disruptive effects from using pop()?
Various methods like pop(), slice(), and splice() have been attempted, all resulting in unnecessary complications.