@@ -2,37 +2,37 @@ import {TESTS_LOAD, TEST_RESULT} from '../../actions/_types';
2
2
import { editorActions } from './actions' ;
3
3
import store from '../../store' ;
4
4
5
- // TODO: optimize editorActions to string[]
6
-
7
- function handleEditorActions ( actionArray : string [ ] ) : void {
8
- if ( actionArray && actionArray . length ) {
9
- // TODO: What is this???
10
- actionArray . map ( ( actionString ) => editorActions ( actionString ) ) ;
5
+ function handleEditorActions ( actions : string [ ] [ ] ) : void {
6
+ const next = actions . shift ( ) ;
7
+ if ( next && next . length ) {
8
+ // resolve promises in order
9
+ next . reduce ( ( total : Promise < any > , curr : string ) => {
10
+ return total . then ( ( ) => editorActions ( curr ) ) ;
11
+ } , Promise . resolve ( ) ) ;
11
12
}
12
13
}
13
14
15
+ // trigger actions only once, moving fowards
14
16
let currentTaskPosition = 0 ;
15
- /**
16
- * Test is running, return true, else false
17
- */
17
+
18
18
export default function editorActionsReducer (
19
19
editorActions = [ ] , action : Action
20
20
) : string [ ] [ ] {
21
21
let actions : string [ ] [ ] = null ;
22
22
switch ( action . type ) {
23
23
case TESTS_LOAD :
24
24
actions = store . getState ( ) . tasks . map ( task => task . actions || [ ] ) ;
25
- currentTaskPosition = 0 ;
26
- handleEditorActions ( actions . shift ( ) ) ; // run first action
25
+ handleEditorActions ( actions ) ; // run first action
27
26
return actions ;
28
27
29
28
case TEST_RESULT :
30
29
actions = action . payload . actions || [ ] ;
31
- const nextTaskPosition = action . payload . result . taskPosition ;
32
- if ( nextTaskPosition > currentTaskPosition ) {
30
+ const nextTaskPosition : number = action . payload . result . taskPosition ;
31
+ const times : number = nextTaskPosition - currentTaskPosition ;
32
+ if ( times > 0 ) {
33
33
// run actions for each task position passed
34
- for ( let i = 0 ; i < nextTaskPosition - currentTaskPosition ; i ++ ) {
35
- handleEditorActions ( actions . shift ( ) ) ; // run first action
34
+ for ( let i = 0 ; i < times ; i ++ ) {
35
+ handleEditorActions ( actions ) ; // run first action
36
36
}
37
37
currentTaskPosition = nextTaskPosition ;
38
38
}
0 commit comments