@@ -12,29 +12,31 @@ import React from "react";
1212
1313import PlayArrowRoundedIcon from "@mui/icons-material/PlayArrowRounded" ;
1414import PauseRoundedIcon from "@mui/icons-material/PauseRounded" ;
15+ import SyncRoundedIcon from "@mui/icons-material/SyncRounded" ;
1516
1617const PlayPauseButton = ( {
1718 project,
1819 language,
19- appRunning,
20- setAppRunning,
2120 dlModel,
2221 hasDLModel,
2322 connectManager,
2423} : {
2524 project : string ;
2625 language ?: string ;
27- appRunning : boolean ;
28- setAppRunning : ( running : boolean ) => void ;
2926 dlModel : ArrayBuffer | undefined ;
3027 hasDLModel : boolean ;
31- connectManager : ( desiredState ?: string , callback ?: ( ) => void ) => Promise < void > ;
28+ connectManager : (
29+ desiredState ?: string ,
30+ callback ?: ( ) => void
31+ ) => Promise < void > ;
3232} ) => {
3333 const theme = useAcademyTheme ( ) ;
3434 const exerciseContext = useExercise ( ) ;
3535 const { warning, error, info, close } = useError ( ) ;
3636 const codeRef = useRef ( "" ) ;
3737 const runningCodeRef = useRef ( "" ) ;
38+ const [ state , setState ] = useState < string > ( CommsManager . getInstance ( ) . getState ( ) ) ;
39+ const [ loading , setLoading ] = useState < boolean > ( false ) ;
3840 const isCodeUpdatedRef = useRef < boolean | undefined > ( undefined ) ;
3941 const [ isCodeUpdated , _updateCode ] = useState < boolean | undefined > ( false ) ;
4042
@@ -43,28 +45,45 @@ const PlayPauseButton = ({
4345 _updateCode ( data ) ;
4446 } ;
4547
48+ const updateState = ( e : any ) => {
49+ setState ( e . detail . state ) ;
50+ } ;
51+
4652 useEffect ( ( ) => {
4753 subscribe ( "autoSaveCompleted" , ( ) => {
4854 updateCode ( true ) ;
4955 } ) ;
56+ subscribe ( "CommsManagerStateChange" , updateState ) ;
5057
5158 return ( ) => {
5259 unsubscribe ( "autoSaveCompleted" , ( ) => { } ) ;
60+ unsubscribe ( "CommsManagerStateChange" , ( ) => { } ) ;
5361 } ;
5462 } , [ ] ) ;
5563
5664 useEffect ( ( ) => {
5765 codeRef . current = exerciseContext . code ;
5866 } , [ exerciseContext ] ) ;
5967
68+ useEffect ( ( ) => {
69+ if ( state === states . RUNNING || state === states . PAUSED ) {
70+ setLoading ( false ) ;
71+ }
72+ } , [ state ] ) ;
73+
6074 // App handling
6175
6276 const onAppStateChange = async ( save ?: boolean ) => {
77+
6378 const manager = CommsManager . getInstance ( ) ;
79+ setLoading ( true ) ;
6480
6581 if ( manager . getState ( ) === states . IDLE ) {
66- info ( "Connecting with the Robotics Backend ..." )
67- connectManager ( states . TOOLS_READY , ( ) => { onAppStateChange ( ) ; close ( ) } ) ;
82+ info ( "Connecting with the Robotics Backend ..." ) ;
83+ connectManager ( states . TOOLS_READY , ( ) => {
84+ onAppStateChange ( ) ;
85+ close ( ) ;
86+ } ) ;
6887 return ;
6988 }
7089
@@ -77,13 +96,13 @@ const PlayPauseButton = ({
7796 warning (
7897 "Failed to found a running simulation. Please make sure an universe is selected."
7998 ) ;
99+ setLoading ( false ) ;
80100 return ;
81101 }
82102
83- if ( appRunning ) {
103+ if ( state === states . RUNNING ) {
84104 try {
85105 await manager . pause ( ) ;
86- setAppRunning ( false ) ;
87106 console . log ( "App paused correctly!" ) ;
88107 return ;
89108 } catch ( e : unknown ) {
@@ -108,7 +127,6 @@ const PlayPauseButton = ({
108127 runningCodeRef . current === codeRef . current
109128 ) {
110129 await manager . resume ( ) ;
111- setAppRunning ( true ) ;
112130 console . log ( "App resumed correctly!" ) ;
113131 return ;
114132 }
@@ -125,7 +143,6 @@ const PlayPauseButton = ({
125143 } else {
126144 commonsZip = zip ;
127145 }
128-
129146
130147 const extraFiles : { name : string ; content : string } [ ] =
131148 await getProjectExtraFiles ( project , language ? language : "python" ) ;
@@ -166,9 +183,9 @@ const PlayPauseButton = ({
166183 reader . readAsDataURL ( content ) ;
167184 } ) ;
168185
169- setAppRunning ( true ) ;
170186 console . log ( "App started successfully" ) ;
171187 } catch ( e : unknown ) {
188+ setLoading ( false ) ;
172189 if ( e instanceof Error ) {
173190 console . error ( "Error running app: " + e . message ) ;
174191 error ( "Error running app: " + e . message ) ;
@@ -177,20 +194,29 @@ const PlayPauseButton = ({
177194 } ;
178195
179196 return (
180- < StyledHeaderButton
181- bgColor = { theme . palette . primary }
182- hoverColor = { theme . palette . secondary }
183- roundness = { theme . roundness }
184- id = "run-app"
185- onClick = { ( ) => onAppStateChange ( undefined ) }
186- title = "Run app"
187- >
188- { appRunning ? (
189- < PauseRoundedIcon htmlColor = { theme . palette . text } />
190- ) : (
191- < PlayArrowRoundedIcon htmlColor = { theme . palette . text } />
192- ) }
193- </ StyledHeaderButton >
197+ < >
198+ < StyledHeaderButton
199+ bgColor = { theme . palette . primary }
200+ hoverColor = { theme . palette . secondary }
201+ roundness = { theme . roundness }
202+ id = "run-app"
203+ onClick = { ( ) => onAppStateChange ( undefined ) }
204+ title = "Run app"
205+ disabled = { loading }
206+ >
207+ { loading ? (
208+ < SyncRoundedIcon htmlColor = { theme . palette . text } id = "loading-spin" />
209+ ) : (
210+ < >
211+ { state === states . RUNNING ? (
212+ < PauseRoundedIcon htmlColor = { theme . palette . text } />
213+ ) : (
214+ < PlayArrowRoundedIcon htmlColor = { theme . palette . text } />
215+ ) }
216+ </ >
217+ ) }
218+ </ StyledHeaderButton >
219+ </ >
194220 ) ;
195221} ;
196222
0 commit comments