description import { ThunkAction } from 'redux-thunk' ; import { Action } from 'redux' ; import { v4 as uuidv4 } from 'uuid' ; import ServerManager from '../../ServerManager/ServerManager' ; import { RootState } from '../reducers' ; import * as types from './actionTypes' ; import { ServerConfig , ServerState } from '../../ServerManager/type' ; import { createStream , logMessage } from './messagesActions' ; import { setCurrentServerId } from './navigationActions' ; export const createServer = ( data ) => ( { type : types . CREATE_SERVER , payload : data , } ) ; export const updateServerState = ( serverState ) => ( { type : types . UPDATE_SERVER_STATE , payload : serverState , } ) ; export const removeServer = ( id ) => ( { type : types . REMOVE_SERVER , payload : id , } ) ; export const stopAndRemoveServer = ( id ) => ( { type : types . STOP_AND_REMOVE_SERVER , payload : id , } ) ; export const serverManagerStopServer = ( id ) : ThunkAction < void , RootState , unknown , Action < string > > => ( dispatch ) => { ServerManager . stopServer ( id ) ; } ; export const serverManagerStartServer = ( config , id ) : ThunkAction < void , RootState , unknown , Action < string > > => dispatch => { ServerManager . createServer ( { ... config , id : id , onMessage : ( message , id ) => { console . log ( `from client to ${ id } server: ${ message }` ) ; dispatch ( logMessage ( id , message ) ) ; } , onServerClose : ( serverState ) => { dispatch ( updateServerState ( serverState ) ) ; } , } ) . then ( ( data ) => { dispatch ( createServer ( data ) ) ; dispatch ( createStream ( data . id ) ) ; } ) . catch ( ( err ) => { console . log ( err ) ; } ) ; } ; export const serverManagerCreateServer = ( config ) : ThunkAction < void , RootState , unknown , Action < string > > => ( dispatch ) => { ServerManager . createServer ( { ... config , id : uuidv4 ( ) , onMessage : ( message , id ) => { console . log ( `from client to ${ id } server: ${ message }` ) ; dispatch ( logMessage ( id , message ) ) ; } , onServerClose : ( serverState ) => { dispatch ( updateServerState ( serverState ) ) ; } , } ) . then ( ( data ) => { dispatch ( createServer ( data ) ) ; dispatch ( setCurrentServerId ( data . id ) ) ; dispatch ( createStream ( data . id ) ) ; } ) . catch ( ( err ) => { console . log ( err ) ; } ) ; } ; export const serverManagerBroadcastAll = ( id , message ) : ThunkAction < void , RootState , unknown , Action < string > > => ( dispatch ) => { /**
       * The following method, broadcastToAll() is not "promisified" yet.
       * However, it is an asynchronous action and Redux will "complain" if we do not
       * write this in thunk format.
       */ ServerManager . broadcastToAll ( id , message ) ; /**
       * After calling the above broadcastToAll() method, dispatch some action
       * that adds to our store's state that keeps track of messages.
       */ dispatch ( logMessage ( id , message ) ) ; } ;