2024-08-07 15:23:23 +01:00
|
|
|
import { NgModule } from '@angular/core';
|
|
|
|
|
import { SignalRService } from 'src/app/module/chat/infra/socket/signal-r.service'
|
2024-08-14 15:29:16 +01:00
|
|
|
import { ChatServiceService } from 'src/app/module/chat/domain/chat-service.service'
|
2024-08-18 15:40:43 +01:00
|
|
|
import { skip, switchMap } from 'rxjs/operators';
|
2024-08-15 10:26:20 +01:00
|
|
|
import { SessionStore } from 'src/app/store/session.service';
|
2024-08-18 15:40:43 +01:00
|
|
|
import { Subject, timer } from 'rxjs';
|
|
|
|
|
import { UserTypingLocalRepository } from './data/repository/user-typing-local-data-source.service';
|
2024-08-13 10:52:35 +01:00
|
|
|
|
2024-08-07 15:23:23 +01:00
|
|
|
@NgModule({
|
2024-08-18 15:40:43 +01:00
|
|
|
imports: [],
|
|
|
|
|
providers: [],
|
2024-08-07 15:23:23 +01:00
|
|
|
declarations: [],
|
|
|
|
|
schemas: [],
|
|
|
|
|
entryComponents: []
|
|
|
|
|
})
|
|
|
|
|
export class ChatModule {
|
|
|
|
|
|
2024-08-18 15:40:43 +01:00
|
|
|
typingCallback: {[key: string]: Subject<any> } = {}
|
|
|
|
|
|
2024-08-07 15:23:23 +01:00
|
|
|
constructor(
|
2024-08-13 10:52:35 +01:00
|
|
|
private SignalRService: SignalRService,
|
2024-08-18 15:40:43 +01:00
|
|
|
private ChatServiceService: ChatServiceService,
|
|
|
|
|
private signalR: SignalRService,
|
|
|
|
|
private localDataSource: UserTypingLocalRepository,
|
2024-08-07 15:23:23 +01:00
|
|
|
) {
|
|
|
|
|
|
2024-08-15 10:26:20 +01:00
|
|
|
this.syncMessage()
|
2024-08-18 15:40:43 +01:00
|
|
|
this.listenToTyping()
|
2024-08-07 15:23:23 +01:00
|
|
|
}
|
|
|
|
|
|
2024-08-18 15:40:43 +01:00
|
|
|
async listenToTyping() {
|
|
|
|
|
this.signalR.getTyping().subscribe(async (e) => {
|
|
|
|
|
if(e?.roomId) {
|
|
|
|
|
|
|
|
|
|
// this.memoryDataSource.dispatch(removeUserTyping({data: {...e} as any}))
|
|
|
|
|
// this.memoryDataSource.dispatch(addUserTyping({data: {...e} as any}))
|
|
|
|
|
//
|
|
|
|
|
const value = await this.localDataSource.addUserTyping(e);
|
|
|
|
|
|
|
|
|
|
const id = e.roomId + '@' + e.userName
|
|
|
|
|
if(!this.typingCallback[id]) {
|
|
|
|
|
this.typingCallback[id] = new Subject()
|
|
|
|
|
this.typingCallback[id].pipe(
|
|
|
|
|
switchMap(() => timer(2000)),
|
|
|
|
|
).subscribe(() => {
|
2024-08-20 16:34:47 +01:00
|
|
|
// console.log('111111==============')
|
2024-08-18 15:40:43 +01:00
|
|
|
// this.memoryDataSource.dispatch(removeUserTyping({data: {...e} as any}))
|
|
|
|
|
this.localDataSource.removeUserTyping(e)
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
this.typingCallback[id].next()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2024-08-07 15:23:23 +01:00
|
|
|
|
2024-08-18 13:27:57 +01:00
|
|
|
async syncMessage() {
|
2024-08-15 10:26:20 +01:00
|
|
|
const connection = this.SignalRService.getConnectionState()
|
2024-08-07 15:23:23 +01:00
|
|
|
|
2024-08-15 10:26:20 +01:00
|
|
|
connection.pipe(
|
|
|
|
|
skip(1) // Skip the first value
|
|
|
|
|
).subscribe((value)=> {
|
2024-08-07 15:23:23 +01:00
|
|
|
if(value) {
|
2024-08-15 10:26:20 +01:00
|
|
|
// on reconnect
|
2024-08-18 13:27:57 +01:00
|
|
|
this.ChatServiceService.start();
|
2024-08-15 10:26:20 +01:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
connection.subscribe((value) => {
|
|
|
|
|
if(value) {
|
|
|
|
|
// on connect
|
2024-08-18 15:40:43 +01:00
|
|
|
this.ChatServiceService.sendLocalMessages()
|
2024-08-07 15:23:23 +01:00
|
|
|
}
|
|
|
|
|
})
|
2024-08-15 10:26:20 +01:00
|
|
|
|
|
|
|
|
// on page reload sync
|
|
|
|
|
if(!(!SessionStore.user.Inactivity || !SessionStore.exist)) {
|
2024-08-18 13:27:57 +01:00
|
|
|
this.ChatServiceService.start();
|
2024-08-15 10:26:20 +01:00
|
|
|
}
|
2024-08-07 15:23:23 +01:00
|
|
|
}
|
|
|
|
|
}
|