@@ -74,12 +74,12 @@ const actions = {
7474 log : ( value ) => console . log ( value ) ,
7575
7676 system : null ,
77-
7877 preprogram : {
79- start : function start ( { blocks, motors } ) {
80- console . log ( motors ) ;
81-
82- const waitFor = ( ms ) => new Promise ( r => setTimeout ( r , ms ) ) ;
78+ start1 : ( { blocks, motors, preprogram } ) => ( state ) => {
79+ const waitFor = ( ms ) => new Promise ( r => {
80+ state . sid = setTimeout ( r , ms ) ;
81+ return state . sid ;
82+ } ) ;
8383 const asyncForEach = async ( array , callback ) => {
8484 for ( let index = 0 ; index < array . length ; index ++ ) {
8585 // eslint-disable-next-line no-await-in-loop
@@ -102,28 +102,30 @@ const actions = {
102102 } ;
103103 const run = async ( ) => {
104104 await asyncForEach ( blocks , async ( block ) => {
105- const iid = setInterval ( ( ) => {
105+ state . iid = setInterval ( ( ) => {
106106 console . log ( '[pre-program]:' , block . speed , motors ) ;
107107 motors . set ( block . speed , convertToArrOfDirections ( block . direction ) ) ;
108108 } , 100 ) ;
109109 await waitFor ( block . time * 1000 ) ;
110- clearInterval ( iid ) ;
110+ clearInterval ( state . iid ) ;
111111 } ) ;
112- motors . stop ( ) ;
112+ preprogram . stop ( { motors } ) ;
113113 console . log ( '[pre-program]: Done' ) ;
114114 } ;
115+ console . log ( run ( ) ) ;
116+ // return { running: !state.running };
117+ } ,
115118
116- run ( ) ;
119+ stop : ( { motors } ) => state => {
120+ motors . stop ( ) ;
121+ clearInterval ( state . iid ) ;
122+ clearTimeout ( state . sid ) ;
123+ return { running : ! state . running } ;
117124 } ,
118- add : ( ) => state => ( {
119- next : {
120- direction : 'fw' ,
121- speed : 0 ,
122- time : 0 ,
123- step : 1 ,
124- } ,
125- blocks : state . blocks . concat ( state . next ) ,
126- } ) ,
125+ setRunningFlag : ( ) => state => ( { running : ! state . running } ) ,
126+
127+ add : ( ) => state => ( { blocks : state . blocks . concat ( state . next ) } ) ,
128+
127129 remove : ( index ) => state => ( { blocks : state . blocks . filter ( block => state . blocks . indexOf ( block ) !== index ) } ) ,
128130 next : {
129131 setDirection : dir => state => ( { direction : dir } ) ,
0 commit comments