1- import { OutputMessageTypes , SocketOuputMessageLiteral } from '../messages.consts' ;
2- import { Action , SocketInfo } from '../messages.model' ;
31import { sessionRepository , roomRepository } from 'dals' ;
4- const { isTrainerUser, getRoomFromConnectionId } = sessionRepository ;
5- const { getRoomContent } = roomRepository ;
6-
2+ import {
3+ OutputMessageTypes ,
4+ SocketOuputMessageLiteral ,
5+ } from '../messages.consts' ;
6+ import { Action , SocketInfo } from '../messages.model' ;
77import { ResponseBase , responseType } from './response' ;
88
9- export const processOutputMessageCollection = async ( socketInfo : SocketInfo , actionCollection : Action [ ] ) : Promise < void > => {
9+ export const processOutputMessageCollection = async (
10+ socketInfo : SocketInfo ,
11+ actionCollection : Action [ ]
12+ ) : Promise < void > => {
1013 if ( actionCollection ) {
1114 // TODO: Error handling
12- await actionCollection . forEach ( ( action ) =>
13- processOuputMessage ( socketInfo , action )
14- ) ;
15+ for ( const action of actionCollection ) {
16+ await processOuputMessage ( socketInfo , action ) ;
17+ }
1518 }
1619} ;
1720
18- export const processOuputMessage = async ( socketInfo : SocketInfo , action : Action ) => {
19- const { connectionId, io, socket } = socketInfo ;
20- const isMaster = await isTrainerUser ( connectionId ) ;
21- const room = await getRoomFromConnectionId ( connectionId ) ;
22-
21+ export const processOuputMessage = async (
22+ socketInfo : SocketInfo ,
23+ action : Action
24+ ) => {
2325 switch ( action . type ) {
2426 case OutputMessageTypes . CONNECTION_ESTABLISHED_TRAINER :
25- await handleNotifyConnectionEstablishedTrainer ( socketInfo , connectionId ) ;
27+ await handleNotifyConnectionEstablishedTrainer ( socketInfo ) ;
2628 break ;
2729 case OutputMessageTypes . CONNECTION_ESTABLISHED_STUDENT :
28- await handleNotifyConnectionEstablishedStudent ( socketInfo , connectionId ) ;
30+ await handleNotifyConnectionEstablishedStudent ( socketInfo ) ;
2931 break ;
3032 case OutputMessageTypes . APPEND_TEXT :
3133 await handleAppendText ( socketInfo , action . payload ) ;
@@ -44,44 +46,73 @@ export const processOuputMessage = async (socketInfo: SocketInfo, action: Action
4446 }
4547} ;
4648
47- const handleSingleStudentSendFullContent = async ( socketInfo : SocketInfo ) => {
48- await handleSendFullContent ( socketInfo , responseType . STUDENT_GET_FULL_CONTENT , false ) ;
49- }
50-
51- const handleTrainerSendFullContent = async ( socketInfo : SocketInfo ) => {
52- await handleSendFullContent ( socketInfo , responseType . TRAINER_GET_FULL_CONTENT , false ) ;
53- }
54-
55- const handleUpdateFullContent = async ( socketInfo : SocketInfo ) => {
56- await handleSendFullContent ( socketInfo , responseType . UPDATE_FULL_CONTENT , true ) ;
57- }
49+ const handleNotifyConnectionEstablishedTrainer = async (
50+ socketInfo : SocketInfo
51+ ) => {
52+ const room = await sessionRepository . getRoomFromConnectionId (
53+ socketInfo . connectionId
54+ ) ;
55+ const response : ResponseBase = { type : responseType . CONNECTION_ACK } ;
56+ socketInfo . socket . in ( room ) . emit ( SocketOuputMessageLiteral . MESSAGE , response ) ;
57+ } ;
5858
59- const handleSendFullContent = async ( socketInfo : SocketInfo , responseType : string , sendToAll : boolean ) => {
60- const room = await getRoomFromConnectionId ( socketInfo . connectionId ) ;
61- const content = await getRoomContent ( room ) ;
62- const msg = { type : responseType , payload : content } ;
63- sendToAll ? socketInfo . io . in ( room ) . emit ( SocketOuputMessageLiteral . MESSAGE , msg ) :
64- socketInfo . socket . emit ( SocketOuputMessageLiteral . MESSAGE , msg ) ;
59+ const handleNotifyConnectionEstablishedStudent = async (
60+ socketInfo : SocketInfo
61+ ) => {
62+ const room = await sessionRepository . getRoomFromConnectionId (
63+ socketInfo . connectionId
64+ ) ;
65+ const response : ResponseBase = {
66+ type : responseType . CONNECTION_ACK ,
67+ } ;
68+ socketInfo . socket . in ( room ) . emit ( SocketOuputMessageLiteral . MESSAGE , response ) ;
6569} ;
6670
6771const handleAppendText = async ( socketInfo : SocketInfo , text : string ) => {
68- const room = await getRoomFromConnectionId ( socketInfo . connectionId ) ;
72+ const room = await sessionRepository . getRoomFromConnectionId (
73+ socketInfo . connectionId
74+ ) ;
6975 socketInfo . io . in ( room ) . emit ( SocketOuputMessageLiteral . MESSAGE , {
7076 type : responseType . APPEND_TEXT ,
7177 payload : text ,
7278 } ) ;
7379} ;
7480
75- const handleNotifyConnectionEstablishedTrainer = async ( socketInfo : SocketInfo , connectionId : string ) => {
76- const room = await getRoomFromConnectionId ( socketInfo . connectionId ) ;
77- const response : ResponseBase = { type : responseType . CONNECTION_ACK } ;
78- socketInfo . socket . in ( room ) . emit ( SocketOuputMessageLiteral . MESSAGE , response ) ;
81+ const handleUpdateFullContent = async ( socketInfo : SocketInfo ) => {
82+ await handleSendFullContent (
83+ socketInfo ,
84+ responseType . UPDATE_FULL_CONTENT ,
85+ true
86+ ) ;
7987} ;
8088
81- const handleNotifyConnectionEstablishedStudent = async ( socketInfo : SocketInfo , connectionId : string ) => {
82- const room = await getRoomFromConnectionId ( socketInfo . connectionId ) ;
83- const response : ResponseBase = {
84- type : responseType . CONNECTION_ACK ,
85- } ;
86- socketInfo . socket . in ( room ) . emit ( SocketOuputMessageLiteral . MESSAGE , response ) ;
89+ const handleSingleStudentSendFullContent = async ( socketInfo : SocketInfo ) => {
90+ await handleSendFullContent (
91+ socketInfo ,
92+ responseType . STUDENT_GET_FULL_CONTENT ,
93+ false
94+ ) ;
95+ } ;
96+
97+ const handleTrainerSendFullContent = async ( socketInfo : SocketInfo ) => {
98+ await handleSendFullContent (
99+ socketInfo ,
100+ responseType . TRAINER_GET_FULL_CONTENT ,
101+ false
102+ ) ;
103+ } ;
104+
105+ const handleSendFullContent = async (
106+ socketInfo : SocketInfo ,
107+ responseType : string ,
108+ sendToAll : boolean
109+ ) => {
110+ const room = await sessionRepository . getRoomFromConnectionId (
111+ socketInfo . connectionId
112+ ) ;
113+ const content = await roomRepository . getRoomContent ( room ) ;
114+ const msg = { type : responseType , payload : content } ;
115+ sendToAll
116+ ? socketInfo . io . in ( room ) . emit ( SocketOuputMessageLiteral . MESSAGE , msg )
117+ : socketInfo . socket . emit ( SocketOuputMessageLiteral . MESSAGE , msg ) ;
87118} ;
0 commit comments