File tree Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -43,6 +43,13 @@ mixin class CoroutineExecutor {
4343    }
4444  }
4545
46+   /// Returns whether a coroutine is currently running 
47+   /// 
48+ bool  isCoroutineRunning (Coroutine  coroutine) {
49+     final  int  id =  coroutine.hashCode;
50+     return  _runningCoroutines.containsKey (id);
51+   }
52+ 
4653  @pragma ('vm:always-consider-inlining' )
4754  T ?  _stepCoroutine <T >(int  id, CoroutineInstance <T > instance) {
4855    final  bool  hasNext =  instance.moveNext ();
Original file line number Diff line number Diff line change @@ -135,4 +135,26 @@ void main() {
135135    executor.runCoroutine (myCoroutine); // value null, coroutine finished 
136136    expect (executor.countCoroutines, equals (0 ));
137137  });
138+ 
139+   test ('correctly reports whether a coroutine is running' , () {
140+     final  executor =  CoroutineExecutor ();
141+ 
142+     CoroutineValue <int > myCoroutine () sync *  {
143+       int  counter =  1 ;
144+       yield  counter;
145+       counter =  2 ;
146+       yield  counter;
147+     }
148+ 
149+     expect (executor.isCoroutineRunning (myCoroutine), isFalse);
150+ 
151+     executor.runCoroutine (myCoroutine); // value 1 
152+     expect (executor.isCoroutineRunning (myCoroutine), isTrue);
153+ 
154+     executor.runCoroutine (myCoroutine); // value 2 
155+     expect (executor.isCoroutineRunning (myCoroutine), isTrue);
156+ 
157+     executor.runCoroutine (myCoroutine); // value null, coroutine finished 
158+     expect (executor.isCoroutineRunning (myCoroutine), isFalse);
159+   });
138160}
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments