1- import { InputMessageTypes , OutputMessageTypes } from './consts' ;
2- import {
3- Action ,
4- InputEstablishConnectionMaster ,
5- InputEstablishConnectionPlayer ,
6- SocketInfo ,
7- } from './model' ;
8- import {
9- isTrainerUser ,
10- saveRoomInfo ,
11- isExistingConnection ,
12- isRoomAvailable ,
13- addNewUser ,
14- getRoomFromConnectionId ,
15- } from '../storage' ;
16- import { processOuputMessage } from './output-processor' ;
1+ import { InputMessageTypes , OutputMessageTypes } from './messages.consts' ;
2+ import { Action , InputEstablishConnectionTrainer , SocketInfo } from './messages.model' ;
3+ import { sessionRepository } from 'dals' ;
4+ const { addNewUser, getRoomFromConnectionId, isExistingConnection, isRoomAvailable, isTrainerUser, saveRoomInfo} = sessionRepository ;
175
18- export const processInputMessage = ( socketInfo : SocketInfo , action : Action ) : Action [ ] => {
6+ export const processInputMessage = async ( socketInfo : SocketInfo , action : Action ) : Promise < Action [ ] > => {
197 let outputActionCollection : Action [ ] = [ ] ;
208 switch ( action . type ) {
219 case InputMessageTypes . ESTABLISH_CONNECTION_TRAINER :
22- const payloadECT : InputEstablishConnectionMaster = action . payload ;
23- outputActionCollection = handleEstablishConnectionTrainer (
10+ const payloadECT : InputEstablishConnectionTrainer = action . payload ;
11+ outputActionCollection = await handleEstablishConnectionTrainer (
2412 socketInfo ,
2513 payloadECT . room ,
2614 payloadECT . trainertoken
2715 ) ;
2816 break ;
2917 case InputMessageTypes . ESTABLISH_CONNECTION_STUDENT :
30- const payloadECS : InputEstablishConnectionMaster = action . payload ;
31- outputActionCollection = handleEstablishConnectionStudent (
18+ const payloadECS : InputEstablishConnectionTrainer = action . payload ;
19+ outputActionCollection = await handleEstablishConnectionStudent (
3220 socketInfo ,
3321 payloadECS . room
3422 ) ;
3523 break ;
3624 case InputMessageTypes . TRAINER_APPEND_TEXT :
37- outputActionCollection = handleTrainerSendText (
25+ outputActionCollection = await handleTrainerSendText (
3826 socketInfo ,
3927 action . payload ,
4028 InputMessageTypes . TRAINER_APPEND_TEXT
4129 ) ;
4230 break ;
4331 case InputMessageTypes . TRAINER_SET_FULL_TEXT :
44- outputActionCollection = handleTrainerSendText (
32+ outputActionCollection = await handleTrainerSendText (
4533 socketInfo ,
4634 action . payload ,
4735 InputMessageTypes . TRAINER_SET_FULL_TEXT
4836 ) ;
4937 break ;
5038 case InputMessageTypes . STUDENT_REQUEST_FULL_CONTENT :
51- outputActionCollection = handleRequestGetStudentContent ( socketInfo ) ;
39+ outputActionCollection = await handleRequestGetStudentContent ( socketInfo ) ;
5240 break ;
5341 case InputMessageTypes . TRAINER_REQUEST_FULL_CONTENT :
54- outputActionCollection = handleRequestGetTrainerContent ( socketInfo ) ;
42+ outputActionCollection = await handleRequestGetTrainerContent ( socketInfo ) ;
5543 break ;
5644 default :
5745 break ;
@@ -60,19 +48,19 @@ export const processInputMessage = (socketInfo: SocketInfo, action: Action): Act
6048 return outputActionCollection ;
6149} ;
6250
63- const handleRequestGetStudentContent = ( socketInfo : SocketInfo ) => {
51+ const handleRequestGetStudentContent = async ( socketInfo : SocketInfo ) : Promise < Action [ ] > => {
6452 return [ { type : OutputMessageTypes . STUDENT_SEND_FULL_CONTENT } ] ;
6553} ;
6654
67- const handleRequestGetTrainerContent = ( socketInfo : SocketInfo ) => {
55+ const handleRequestGetTrainerContent = async ( socketInfo : SocketInfo ) : Promise < Action [ ] > => {
6856 return [ { type : OutputMessageTypes . TRAINER_SEND_FULL_CONTENT } ] ;
6957} ;
7058
71- const handleTrainerSendText = ( socketInfo : SocketInfo , text : string , action : string ) => {
59+ const handleTrainerSendText = async ( socketInfo : SocketInfo , text : string , action : string ) : Promise < Action [ ] > => {
7260 if ( ! isTrainerUser ( socketInfo . connectionId ) ) {
7361 return [ ] ;
7462 }
75- const room = getRoomFromConnectionId ( socketInfo . connectionId ) ;
63+ const room = await getRoomFromConnectionId ( socketInfo . connectionId ) ;
7664 const roomInfo = { room, content : text } ;
7765 saveRoomInfo ( roomInfo , action ) ;
7866 switch ( action ) {
@@ -85,17 +73,14 @@ const handleTrainerSendText = (socketInfo: SocketInfo, text: string, action: str
8573 } ;
8674} ;
8775
88- const handleEstablishConnectionStudent = (
89- socketInfo : SocketInfo ,
90- room : string
91- ) => {
76+ const handleEstablishConnectionStudent = async ( socketInfo : SocketInfo , room : string ) : Promise < Action [ ] > => {
9277 if ( ! room ) {
9378 // Ignore
9479 return [ ] ;
9580 }
9681
97- if ( isRoomAvailable ( room ) || ! isExistingConnection ( socketInfo . connectionId ) ) {
98- addNewUser ( socketInfo . connectionId , {
82+ if ( await isRoomAvailable ( room ) || ! ( await isExistingConnection ( socketInfo . connectionId ) ) ) {
83+ await addNewUser ( socketInfo . connectionId , {
9984 room,
10085 trainerToken : '' ,
10186 isTrainer : false ,
@@ -105,18 +90,14 @@ const handleEstablishConnectionStudent = (
10590 return [ { type : OutputMessageTypes . CONNECTION_ESTABLISHED_STUDENT } ] ;
10691} ;
10792
108- const handleEstablishConnectionTrainer = (
109- socketInfo : SocketInfo ,
110- room : string ,
111- trainerToken : string
112- ) : Action [ ] => {
93+ const handleEstablishConnectionTrainer = async ( socketInfo : SocketInfo , room : string , trainerToken : string ) : Promise < Action [ ] > => {
11394 if ( ! trainerToken || ! room ) {
11495 // Ignore
11596 return [ ] ;
11697 }
11798
118- if ( isRoomAvailable ( room ) || ! isExistingConnection ( socketInfo . connectionId ) ) {
119- addNewUser ( socketInfo . connectionId , {
99+ if ( await isRoomAvailable ( room ) || ! ( await isExistingConnection ( socketInfo . connectionId ) ) ) {
100+ await addNewUser ( socketInfo . connectionId , {
120101 room,
121102 trainerToken,
122103 isTrainer : ! ! trainerToken ,
0 commit comments