fix duplicate message

This commit is contained in:
Peter Maquiran
2024-09-04 22:48:29 +01:00
parent 5315d185c2
commit 106267aee9
13 changed files with 160 additions and 79 deletions
@@ -8,6 +8,7 @@ import { chatDatabase } from 'src/app/infra/database/dexie/service';
import { IMessageLocalRepository } from 'src/app/core/chat/repository/message/message-local-repository';
import { BehaviorSubject, combineLatest, from, Observable } from 'rxjs';
import { filter, map } from 'rxjs/operators';
import { v4 as uuidv4 } from 'uuid'
@Injectable({
providedIn: 'root'
@@ -15,6 +16,7 @@ import { filter, map } from 'rxjs/operators';
export class MessageLocalDataSourceService extends DexieRepository<MessageTable, MessageEntity> implements IMessageLocalRepository {
private creatingSubject : BehaviorSubject<MessageTable> = new BehaviorSubject<MessageTable>(null);
private lastTimestamp = 0;
constructor() {
super(chatDatabase.message, MessageTableSchema)
@@ -26,6 +28,23 @@ export class MessageLocalDataSourceService extends DexieRepository<MessageTable,
private onCreatingHook() {
chatDatabase.message.hook('creating', (primaryKey, obj, transaction) => {
let now = Date.now();
// If the current time is the same as the last, increment
if (now <= this.lastTimestamp) {
obj.$createAt = this.lastTimestamp + 1;
this.lastTimestamp = this.lastTimestamp + 1;
} else {
this.lastTimestamp = now;
obj.$createAt = now;
}
if(obj.id) {
obj.$id = obj.id
} else {
obj.$id = 'Local-'+uuidv4()
}
this.creatingSubject.next(obj)
});
}