fix: skip recent deleted message empty react changes
parent
0cc7994c12
commit
ad03fbd497
@ -0,0 +1,32 @@
|
||||
import { RingBuffer } from '../../../utils/RingBuffer';
|
||||
|
||||
const rollingDeletedMessageIds: Map<string, RingBuffer<number>> = new Map();
|
||||
|
||||
// keep 2000 deleted message ids in memory
|
||||
const perRoomRollingRemovedIds = 2000;
|
||||
|
||||
const addMessageDeletedId = (conversationId: string, messageDeletedId: number) => {
|
||||
if (!rollingDeletedMessageIds.has(conversationId)) {
|
||||
rollingDeletedMessageIds.set(conversationId, new RingBuffer<number>(perRoomRollingRemovedIds));
|
||||
}
|
||||
const ringBuffer = rollingDeletedMessageIds.get(conversationId);
|
||||
if (!ringBuffer) {
|
||||
return;
|
||||
}
|
||||
ringBuffer.add(messageDeletedId);
|
||||
};
|
||||
|
||||
|
||||
const hasMessageDeletedId = (conversationId: string, messageDeletedId: number) => {
|
||||
if (!rollingDeletedMessageIds.has(conversationId)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const messageIdWasDeletedRecently = rollingDeletedMessageIds
|
||||
?.get(conversationId)
|
||||
?.has(messageDeletedId);
|
||||
|
||||
return messageIdWasDeletedRecently;
|
||||
};
|
||||
|
||||
export const sogsRollingDeletions = { addMessageDeletedId, hasMessageDeletedId };
|
@ -0,0 +1,31 @@
|
||||
export class RingBuffer<T> {
|
||||
private buffer: Array<T> = [];
|
||||
private readonly capacity: number;
|
||||
|
||||
constructor(capacity: number) {
|
||||
this.capacity = capacity;
|
||||
}
|
||||
|
||||
public getCapacity(): number {
|
||||
return this.capacity;
|
||||
}
|
||||
|
||||
public add(item: T) {
|
||||
this.buffer.push(item);
|
||||
this.crop();
|
||||
}
|
||||
|
||||
public has(item: T) {
|
||||
return this.buffer.includes(item);
|
||||
}
|
||||
|
||||
public clear() {
|
||||
this.buffer = [];
|
||||
}
|
||||
|
||||
private crop() {
|
||||
while (this.buffer.length > this.capacity) {
|
||||
this.buffer.shift();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue