1- import produce from 'immer' ;
2- import { InputMessageTypes } from 'messages' ;
3- import { ConnectSessionInfo , RoomInfo , UserSession } from 'dals'
1+ import { UserSession } from 'dals'
42
53let userCollectionSession : UserSession [ ] = [ ] ;
6- let roomCollectionSession : RoomInfo [ ] = [ ] ;
7-
8- export const isRoomAvailable = async ( room : string ) : Promise < boolean > =>
9- ! userCollectionSession . find ( ( session ) => session . room === room ) ;
104
115export const addNewUser = async ( newUser : UserSession ) : Promise < void > => {
126 const { connectionId, room, trainerToken, isTrainer } = newUser ;
@@ -21,23 +15,6 @@ export const addNewUser = async (newUser: UserSession): Promise<void> => {
2115 ] ;
2216} ;
2317
24- export const saveRoomInfo = async ( newRoomInfo : RoomInfo , action : string ) : Promise < void > => {
25- const roomIndex : number = await getRoomIndexByName ( newRoomInfo ) ;
26- const content : string = newRoomInfo . content ;
27- roomCollectionSession =
28- roomIndex === - 1
29- ? await insertRoom ( newRoomInfo )
30- : await updateRoomContent ( roomIndex , content , action ) ;
31- } ;
32-
33- export const getRoomContent = async ( room : string ) : Promise < string > => {
34- const roomInfo = roomCollectionSession . find (
35- ( roomInfo : RoomInfo ) => roomInfo . room === room
36- ) ;
37-
38- return roomInfo ? roomInfo . content : '' ;
39- } ;
40-
4118export const isTrainerUser = async ( connectionId : string ) : Promise < boolean > => {
4219 const session = userCollectionSession . find (
4320 ( session ) => session . connectionId === connectionId && session . isTrainer
@@ -55,29 +32,4 @@ export const getRoomFromConnectionId = async (connectionId: string): Promise<str
5532 ( session ) => session . connectionId === connectionId
5633 ) ;
5734 return session ? session . room : '' ;
58- } ;
59-
60- const getRoomIndexByName = async ( newRoomInfo : RoomInfo ) : Promise < number > => {
61- return roomCollectionSession . findIndex (
62- ( roomInfo : RoomInfo ) => roomInfo . room === newRoomInfo . room
63- ) ;
64- } ;
65-
66- const updateRoomContent = async ( index : number , content : string , action :string ) : Promise < RoomInfo [ ] > => {
67- return produce ( roomCollectionSession , ( draftState : RoomInfo [ ] ) => {
68- switch ( action ) {
69- case InputMessageTypes . TRAINER_APPEND_TEXT :
70- draftState [ index ] . content += '\n' + content ;
71- break ;
72- case InputMessageTypes . TRAINER_SET_FULL_TEXT :
73- draftState [ index ] . content = content ;
74- break ;
75- } ;
76- } ) ;
77- } ;
78-
79- const insertRoom = async ( newRoomInfo : RoomInfo ) : Promise < RoomInfo [ ] > => {
80- return produce ( roomCollectionSession , ( draftState : RoomInfo [ ] ) => {
81- draftState . push ( newRoomInfo ) ;
82- } ) ;
83- } ;
35+ } ;
0 commit comments