mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-19 04:57:52 +00:00
fix duplicate message
This commit is contained in:
@@ -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)
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user