@@ -2,37 +2,37 @@ import {TESTS_LOAD, TEST_RESULT} from '../../actions/_types';
22import { editorActions } from './actions' ;
33import store from '../../store' ;
44
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 ( ) ) ;
1112}
1213}
1314
15+ // trigger actions only once, moving fowards
1416let currentTaskPosition = 0 ;
15- /**
16- * Test is running, return true, else false
17- */
17+
1818export default function editorActionsReducer (
1919editorActions = [ ] , action :Action
2020) :string [ ] [ ] {
2121let actions :string [ ] [ ] = null ;
2222switch ( action . type ) {
2323case TESTS_LOAD :
2424actions = store . getState ( ) . tasks . map ( task => task . actions || [ ] ) ;
25- currentTaskPosition = 0 ;
26- handleEditorActions ( actions . shift ( ) ) ; // run first action
25+ handleEditorActions ( actions ) ; // run first action
2726return actions ;
2827
2928case TEST_RESULT :
3029actions = 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 ) {
3333// 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
3636}
3737currentTaskPosition = nextTaskPosition ;
3838}