Files
doneit-web/src/app/module/chat/chat.module.ts
T

85 lines
2.7 KiB
TypeScript
Raw Normal View History

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';
2024-08-23 11:10:52 +01:00
import { UserTypingLocalRepository } from './data/repository/typing/user-typing-local-data-source.service';
import { UserTypingRemoteRepositoryService } from './data/repository/typing/user-typing-live-data-source.service';
2024-08-21 20:14:48 +01:00
import { RoomService } from 'src/app/module/chat/domain/service/room.service'
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-23 11:10:52 +01:00
private UserTypingRemoteRepositoryService: UserTypingRemoteRepositoryService,
2024-08-21 20:14:48 +01:00
private RoomService: RoomService
2024-08-07 15:23:23 +01:00
) {
2024-08-21 20:14:48 +01:00
this.RoomService.init()
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() {
2024-08-23 11:10:52 +01:00
this.UserTypingRemoteRepositoryService.listenToTyping().subscribe(async(e) => {
2024-08-18 15:40:43 +01:00
// 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-26 14:47:03 +01:00
2024-08-18 15:40:43 +01:00
}
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
2024-08-23 11:10:52 +01:00
).subscribe((value: boolean)=> {
2024-08-07 15:23:23 +01:00
if(value) {
2024-08-15 10:26:20 +01:00
// on reconnect
2024-08-21 10:40:54 +01:00
this.ChatServiceService.chatSync();
2024-08-15 10:26:20 +01:00
}
});
2024-08-23 11:10:52 +01:00
connection.subscribe((value: boolean) => {
2024-08-15 10:26:20 +01:00
if(value) {
// on connect
2024-08-26 14:47:03 +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
}
}