Skip to content

Commit

Permalink
Use source to determine if a message has already been sent.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mitchdev committed Dec 11, 2024
1 parent f1f6b71 commit 9016c43
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions assets/chat/js/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class ChatSource extends EventEmitter {
this.retryOnDisconnect = true;
this.retryAttempts = 0;
this.retryTimer = null;
this.lastMessageTimestamp = 0;
}

isConnected() {
Expand Down Expand Up @@ -99,8 +100,20 @@ class ChatSource extends EventEmitter {

parseAndDispatch(event) {
const { eventname, data } = this.parse(event);

// If message has a timestamp and it is older than the lastMessageTimestamp,
// then it has already been processed and doesn't not need to be sent again.
if (data.timestamp && data.timestamp <= this.lastMessageTimestamp) {
return;
}

this.emit('DISPATCH', { data, event: eventname }); // Event is used to hook into all dispatched events
this.emit(eventname, data);

// If message has a timestamp then update lastMessageTimestamp.
if (data.timestamp) {
this.lastMessageTimestamp = data.timestamp;
}
}

parse(event) {
Expand Down

0 comments on commit 9016c43

Please sign in to comment.