Skip to content

Commit

Permalink
fix: #13 replace and update message methods in MessageGroup
Browse files Browse the repository at this point in the history
  • Loading branch information
supersnager committed Sep 13, 2022
1 parent ef6b7bd commit ae0006d
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/MessageGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,37 +38,34 @@ export class MessageGroup {
}

/**
* Replace message in message collection
* Replace the given message in the message collection
* @param message
*/
updateMessage(message: ChatMessage<MessageContentType>): void {
const [foundMessage, idx] = this.getMessage(message.id);

if (foundMessage) {
this.messages = this.messages
.concat(this.messages.slice(0, idx as number))
.concat(message)
.concat(this.messages.slice((idx as number) + 1));
this.replaceMessage(foundMessage, idx);
}
}

/**
* Replace message at specified index
* Return true if message exists in specified position.
* Returns true if index is out of bound
* Replace the message at the specified index in the message collection.
* Returns true if the message exists at the specified position.
* Returns false if the given index is out of bound.
* @param message
* @param index
*/
replaceMessage(
message: ChatMessage<MessageContentType>,
index: number
): boolean {
if (this.messages.length > index) {
if (this.messages.length <= index) {
return false;
}

this.messages = this.messages
.concat(this.messages.slice(0, index))
.slice(0, index)
.concat(message)
.concat(this.messages.slice(index + 1));

Expand Down

0 comments on commit ae0006d

Please sign in to comment.