@@ -4,12 +4,14 @@ import * as actions from '../../redux/editor/editor.actions'
44import { Controlled as CodeMirror } from 'react-codemirror2'
55import Classes from './editor.module.css'
66import { ConsoleContainer , ConsoleData , ConsoleHeader , ConsoleTitle } from '../../styles/console'
7+
78import { Box } from '../../styles/flex'
89import Controls from '../Controls/Controls'
910
1011import {
1112 addFunctionToCallstack ,
1213 changeCallstackState ,
14+ removeFunctionFromCallstack ,
1315} from './../../redux/callstack/callstack.actions'
1416import {
1517 playAnimation ,
@@ -402,19 +404,60 @@ class Editor extends Component {
402404 } ,
403405 } ) => ( { start : firstOpenIndex . lineNo , end : lastCloseIndex . lineNo } )
404406
405- iterateThroughLines = function * it ( editor ) {
406- let currentLine = 0
407- let preservedLine = null
407+ //!-------------------------------------------------------------------------------------------
408+
409+ highLightAndUnhighLightLine = ( editor , start , end , scrollTo ) => {
410+ this . higlightCharacters ( editor , start , end )
411+ editor . scrollIntoView ( { line : scrollTo , ch : 0 } )
412+ //this.props.editor.scrollIntoView({ left: 0, top: scrollTo, right: 0, bottom: 0 }, 10)
413+ setTimeout ( ( ) => {
414+ this . removeHiglightFromCharacters ( editor , start , end )
415+ } , 500 )
416+ }
417+
418+ handleThreats = ( start , end ) => {
419+ const functionBodyGenerator = this . iterateThroughLines ( end , start + 1 )
408420
409- this . props . changeCallstackState ( true )
410- this . props . playAnimation ( )
421+ this . threadStates [ this . threadStates . length - 1 ] = false
422+ this . threadStates . push ( true )
423+ this . currentThreadStateId = this . threadStates . length - 1
424+ console . warn ( 'INIT NEW THREAD:' , this . threadStates , this . currentThreadStateId )
425+ const subLevel = setInterval ( ( ) => {
426+ const generatorValues = functionBodyGenerator . next ( )
427+
428+ console . error ( 'Generatorvalues: ' , generatorValues )
429+ if ( ! generatorValues . done ) {
430+ this . threadStates [ this . currentThreadStateId ] = true
431+ } else {
432+ clearInterval ( subLevel )
433+ this . threadStates . pop ( )
434+ this . currentThreadStateId --
435+ this . threadStates [ this . currentThreadStateId ] = true
436+ this . props . removeFunctionFromCallstack ( )
437+ console . warn ( 'NEW THREAD DESTROYED:' , this . threadStates , this . currentThreadStateId )
438+ }
439+ } , this . props . animationDuration )
440+ }
441+
442+ threadStates = [ true ]
443+ currentThreadStateId = 0
444+
445+ iterateThroughLines = function * it ( editorLineCount , startLine = 0 ) {
446+ let currentLine = startLine
447+ const editor = this . props . editor
448+ console . error ( 'STARTED THE GENERATOR WITH, ' , editorLineCount , startLine )
449+
450+ if ( this . threadStates [ 0 ] ) {
451+ this . props . changeCallstackState ( true )
452+ this . props . playAnimation ( )
453+ }
411454
412- while ( currentLine < editor . lineCount ( ) ) {
455+ while ( currentLine < editorLineCount ) {
413456 let startHighlight = currentLine
414457 let endHighlight = currentLine
415- editor . scrollIntoView ( { line : currentLine , ch : 0 } )
416458 const isFunctionCall =
417459 editor . getLine ( currentLine ) !== undefined ? editor . getLine ( currentLine ) . split ( '(' ) : [ '' ]
460+ isFunctionCall [ 0 ] = isFunctionCall [ 0 ] . trim ( )
418461 console . warn ( isFunctionCall )
419462 const isWebApi =
420463 isFunctionCall [ 0 ] !== '' && ( window [ isFunctionCall [ 0 ] ] || isFunctionCall [ 0 ] . includes ( '.then' ) )
@@ -433,11 +476,10 @@ class Editor extends Component {
433476 if ( ! isFunctionCall [ 0 ] . includes ( 'function' ) ) {
434477 // if we found a function name within the named function hash table
435478 if ( checkIfNamedFunction . length > 0 ) {
436- const { start, end } = this . returnOpeningAndClosingLine ( checkIfNamedFunction [ 0 ] )
479+ const { start, end } = this . returnOpeningAndClosingLine ( checkIfNamedFunction [ 0 ] . trim ( ) )
437480
438481 startHighlight = start
439482 endHighlight = end
440- preservedLine = currentLine
441483 currentLine = end
442484 } else {
443485 // if we found a anonymouse function within the anonymouse function hash table
@@ -451,7 +493,7 @@ class Editor extends Component {
451493 currentLine = end
452494
453495 this . props . addFunctionToCallstack ( {
454- name : mainMethod [ 0 ] ,
496+ name : mainMethod [ 0 ] . trim ( ) ,
455497 delay : 1000 ,
456498 webApi : isWebApi ,
457499 message : undefined ,
@@ -477,30 +519,23 @@ class Editor extends Component {
477519 } else {
478520 if ( callFunction ) {
479521 const { start, end } = this . returnOpeningAndClosingLine ( callFunction )
480- startHighlight = start
481- endHighlight = end
482- this . higlightCharacters ( editor , currentLine )
522+
523+ this . highLightAndUnhighLightLine ( editor , currentLine , currentLine , currentLine )
483524
484525 this . props . addFunctionToCallstack ( {
485- name : isFunctionCall [ 0 ] ,
526+ name : isFunctionCall [ 0 ] . trim ( ) ,
486527 delay : 1000 ,
487528 webApi : isWebApi ,
488529 message : undefined ,
489- block : false ,
530+ block : true ,
490531 } )
491- setTimeout ( ( ) => {
492- this . removeHiglightFromCharacters ( editor , currentLine , currentLine )
493- } , 500 )
532+
533+ this . handleThreats ( start , end )
534+ this . highLightAndUnhighLightLine ( editor , start , end , start )
494535 }
495536 }
496537 }
497538 }
498- this . higlightCharacters ( editor , startHighlight , endHighlight )
499-
500- setTimeout ( ( ) => {
501- this . removeHiglightFromCharacters ( editor , startHighlight , endHighlight )
502- } , 500 )
503- yield console . error ( 'EDITOR LINE: ' , currentLine )
504539 } else {
505540 if ( checkIfNamedFunction . length > 0 ) {
506541 const { start, end } = this . returnOpeningAndClosingLine ( checkIfNamedFunction [ 0 ] )
@@ -516,24 +551,32 @@ class Editor extends Component {
516551 endHighlight = end
517552 currentLine = end
518553 }
519- this . higlightCharacters ( editor , startHighlight , endHighlight )
520-
521- setTimeout ( ( ) => {
522- this . removeHiglightFromCharacters ( editor , startHighlight , endHighlight )
523- } , 500 )
524554 }
555+ this . highLightAndUnhighLightLine ( editor , startHighlight , endHighlight , endHighlight )
525556 currentLine ++
557+ yield 'go on'
526558 }
559+ yield 'outside of the gernator loop'
560+
561+ if ( this . threadStates [ 0 ] ) {
562+ //!this.threadStates.length === 1 &&
563+ while ( this . props . callbackQueue . stack . length > 0 ) {
564+ console . error ( 'CALLBACKQUEUE LENGTH ' , this . props . callbackQueue . stack . length )
565+ if ( this . props . callbackQueue . stack . length > 0 ) {
566+ this . props . changeCallstackState ( false )
567+ }
568+ yield 'playing'
569+ }
527570
528- while ( this . props . callbackQueue . stack . length > 0 ) {
529- console . error ( 'CALLBACKQUEUE LENGTH ' , this . props . callbackQueue . stack . length )
530- if ( this . props . callbackQueue . stack . length > 0 ) {
531- this . props . changeCallstackState ( false )
571+ yield this . props . stopAnimation ( )
572+ } else {
573+ let yieldThroughThreads = 0
574+ while ( yieldThroughThreads <= this . threadStates . length ) {
575+ yield yieldThroughThreads
576+ yieldThroughThreads ++
532577 }
533- yield 'playing'
534578 }
535-
536- yield this . props . stopAnimation ( )
579+ return
537580 }
538581
539582 editorDidMount = ( ) => {
@@ -542,40 +585,48 @@ class Editor extends Component {
542585 this . unHiglightLine ( editor , highlightedLine )
543586 this . loadStateWithCollapsable ( editor , 0 )
544587 this . loadStateWithFunctions ( editor , 0 )
545- const generator = this . iterateThroughLines ( editor )
588+ const generator = this . iterateThroughLines ( editor . lineCount ( ) )
546589 //console.log(generator)
547- setInterval ( ( ) => generator . next ( ) , 1500 )
590+ const interv = setInterval ( ( ) => {
591+ if ( this . threadStates [ 0 ] ) {
592+ if ( generator . next ( ) . done ) {
593+ clearInterval ( interv )
594+ }
595+ }
596+ } , this . props . animationDuration )
548597 }
549598
550599 render ( ) {
551600 return (
552601 < >
553602 < ConsoleContainer style = { { padding : '5px' } } >
554603 < ConsoleHeader >
555- < Box display = "flex" justifyContent = "space-around" alignItems = "center" >
556- < ConsoleTitle p = "10px" > Code Editor</ ConsoleTitle >
557- </ Box >
558- < Box display = "flex" justifyContent = "flex-end" alignItems = "center" >
559- < Controls loadFunctions = { this . editorDidMount } />
560- </ Box >
604+ < div style = { { display : 'grid' , gridTemplateColumns : '1fr 1fr 1fr' } } >
605+ < Box display = "flex" justifyContent = "space-around" alignItems = "center" />
606+ < Box display = "flex" justifyContent = "space-around" alignItems = "center" >
607+ < ConsoleTitle p = "10px" > Code Editor</ ConsoleTitle >
608+ </ Box >
609+ < Box display = "flex" justifyContent = "flex-end" alignItems = "center" >
610+ < Controls loadFunctions = { this . editorDidMount } />
611+ </ Box >
612+ </ div >
561613 </ ConsoleHeader >
562- < ConsoleData >
614+ < ConsoleData style = { { height : '400px' } } >
563615 < Box >
564616 < div className = { Classes . container } >
565617 < CodeMirror
566618 className = { Classes . codeMirror }
567619 value = { this . props . data }
568620 editorDidMount = { ( editor ) => {
621+ editor . setSize ( '100%' , '350px' )
569622 this . props . setEditor ( editor )
570- //this.editorDidMount
571- //editor.setSize('100%', '100%')
572623 } }
573624 options = { {
574625 mode : 'javascript' ,
575626 theme : 'material' ,
576627 tabSize : 2 ,
577628 lineNumbers : true ,
578- scrollbarStyle : null ,
629+ // scrollbarStyle: null,
579630 lineWrapping : true ,
580631 } }
581632 onCursor = { this . handleCursor }
@@ -599,6 +650,7 @@ const mapStateToProps = (state) => {
599650 editor : { editor, highlightedLines, collapsableLines, data, functions } ,
600651 callstack : { isOccupied } ,
601652 callbackQueue,
653+ controls : { animationDuration } ,
602654 } = state
603655
604656 return {
@@ -609,6 +661,7 @@ const mapStateToProps = (state) => {
609661 functions,
610662 isOccupied,
611663 callbackQueue,
664+ animationDuration,
612665 }
613666}
614667
@@ -626,6 +679,7 @@ const mapDispatchToProps = (dispatch) => {
626679 playAnimation : ( ) => dispatch ( playAnimation ( ) ) ,
627680 pauseAnimation : ( ) => dispatch ( pauseAnimation ( ) ) ,
628681 stopAnimation : ( ) => dispatch ( stopAnimation ( ) ) ,
682+ removeFunctionFromCallstack : ( ) => dispatch ( removeFunctionFromCallstack ( ) ) ,
629683 }
630684 // editorMount
631685}
0 commit comments