Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/counter_task.js

Issue 9084006: task.js: cooperative multitasking for long-running tasks (Closed)
Patch Set: Created Dec. 26, 2012, 5:06 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/content/task_test_ui.js ('k') | lib/task.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 }
OLDNEW
« no previous file with comments | « chrome/content/task_test_ui.js ('k') | lib/task.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld