OLD | NEW |
(Empty) | |
| 1 /** |
| 2 * This task generator just counts. |
| 3 * |
| 4 * @param count |
| 5 * The number of iterations. |
| 6 * @param count_notifier |
| 7 * The count notifier is called once at each iteration with this current lo
op counter. |
| 8 * @param completion_notifier |
| 9 * The completion notifier is called when the task is finished. Because a l
ong running task does not run |
| 10 * synchronously, we need some kind of notification system. |
| 11 */ |
| 12 exports.tg_count = function ( count, count_notifier, completion_notifier ) |
| 13 { |
| 14 tg_log( "begin" ); |
| 15 var j; |
| 16 for ( j = 0 ; j < count ; ++j ) |
| 17 { |
| 18 /* |
| 19 * The rvalue of a yield statement is the argument to 'send()' called on
the generator. The task runner |
| 20 * calls 'send( true )' to indicated cancellation. 'next()' is a synonym
for 'send( undefined )'. Thus, |
| 21 * the possible values for 'cancelled' are 'true' and 'undefined'. |
| 22 */ |
| 23 // Note: the extra parentheses are a workaround for an IDEA defect about
'yield' as an rvalue. |
| 24 var cancelled = yield( false ); |
| 25 if ( cancelled ) |
| 26 { |
| 27 tg_log( "cancelled" ); |
| 28 break; |
| 29 } |
| 30 if ( count_notifier ) |
| 31 count_notifier( j ); |
| 32 } |
| 33 if ( j == count ) |
| 34 { |
| 35 // Assert the loop terminated in the 'for' statement, not with a cancell
ation. |
| 36 tg_log( "finished" ) |
| 37 } |
| 38 if ( completion_notifier ) |
| 39 completion_notifier(); |
| 40 tg_log( "end" ); |
| 41 }; |
| 42 |
| 43 |
| 44 function tg_log( msg ) |
| 45 { |
| 46 Cu.reportError( "tg_count: " + msg ); |
| 47 } |
OLD | NEW |