fix ballon

This commit is contained in:
Peter Maquiran
2024-09-04 10:58:26 +01:00
parent 100d64f799
commit 0f3096b25e
11 changed files with 123 additions and 34 deletions
@@ -31,13 +31,26 @@ export type ISocketMessageCreateOutput = z.infer<typeof SocketMessageCreateOutpu
})
export class SocketMessageCreateUseCaseService {
private broadcastChannel: BroadcastChannel;
private processedMessages = new Set<string>();
constructor(
private messageLocalDataSourceService: MessageLocalDataSourceService,
) { }
) {
this.broadcastChannel = new BroadcastChannel('socket-message');
this.broadcastChannel.onmessage = (event) => {
console.log('hello', event.data)
const messageId = event.data;
this.processedMessages.add(messageId);
};
// this.broadcastChannel.postMessage('incomingMessage.id');
}
@XTracerAsync({name:'Socket-Message-Create-UseCase', module:'chat', bugPrint: true})
async execute(input: ISocketMessageCreateOutput, tracing?: TracingType) {
this.broadcastChannel.postMessage(input.id);
ParamsValidation(SocketMessageCreateOutputSchema, input, tracing)
const incomingMessage = {
@@ -45,19 +58,31 @@ export class SocketMessageCreateUseCaseService {
sending: false
}
console.log('create message', {incomingMessage});
tracing?.addEvent("Message Create start")
const result = await this.messageLocalDataSourceService.insert(incomingMessage)
tracing?.addEvent("Message Create end")
if(result.isOk()) {
// Check if the message ID already exists in the processedMessages set
if (this.processedMessages.has(incomingMessage.id)) {
console.warn(`Duplicate message detected: ${incomingMessage.id}`);
return; // Exit early to prevent duplicate handling
} else {
tracing?.addEvent("error while creating message")
console.log('no duplicate')
}
// Add the message ID to the processedMessages set and broadcast it
this.processedMessages.add(incomingMessage.id);
console.log('create message', { incomingMessage });
tracing?.addEvent("Message Create start");
const result = await this.messageLocalDataSourceService.insert(incomingMessage);
tracing?.addEvent("Message Create end");
if (result.isOk()) {
// Optionally, you can handle post-insertion logic here
} else {
tracing?.addEvent("error while creating message");
tracing.log("error while creating message", {
error: result.error
})
});
}
}
}