@@ -22,38 +22,41 @@ const Allocator = std.mem.Allocator;
2222
2323const Scheduler = @This ();
2424
25- primary : Queue ,
25+ high_priority : Queue ,
2626
2727// For repeating tasks. We only want to run these if there are other things to
2828// do. We don't, for example, want a window.setInterval or the page.runMicrotasks
2929// to block the page.wait.
30- secondary : Queue ,
30+ low_priority : Queue ,
3131
32- // we expect allocator to be the page arena, hence we never call primary .deinit
32+ // we expect allocator to be the page arena, hence we never call high_priority .deinit
3333pub fn init (allocator : Allocator ) Scheduler {
3434 return .{
35- .primary = Queue .init (allocator , {}),
36- .secondary = Queue .init (allocator , {}),
35+ .high_priority = Queue .init (allocator , {}),
36+ .low_priority = Queue .init (allocator , {}),
3737 };
3838}
3939
4040pub fn reset (self : * Scheduler ) void {
41- self .primary .clearRetainingCapacity ();
42- self .secondary .clearRetainingCapacity ();
41+ self .high_priority .clearRetainingCapacity ();
42+ self .low_priority .clearRetainingCapacity ();
4343}
4444
4545const AddOpts = struct {
4646 name : []const u8 = "" ,
4747 low_priority : bool = false ,
4848};
4949pub fn add (self : * Scheduler , ctx : * anyopaque , func : Task.Func , ms : u32 , opts : AddOpts ) ! void {
50+ var low_priority = opts .low_priority ;
5051 if (ms > 5_000 ) {
51- log .warn (.user_script , "long timeout ignored" , .{ .delay = ms });
52- // ignore any task that we're almost certainly never going to run
53- return ;
52+ // we don't want tasks in the far future to block page.wait from
53+ // completing. However, if page.wait is called multiple times (maybe
54+ // a CDP driver is wait for something to happen), then we do want
55+ // to [eventually] run these when their time is up.
56+ low_priority = true ;
5457 }
5558
56- var q = if (opts . low_priority ) & self .secondary else & self .primary ;
59+ var q = if (low_priority ) & self .low_priority else & self .high_priority ;
5760 return q .add (.{
5861 .ms = std .time .milliTimestamp () + ms ,
5962 .ctx = ctx ,
@@ -63,11 +66,11 @@ pub fn add(self: *Scheduler, ctx: *anyopaque, func: Task.Func, ms: u32, opts: Ad
6366}
6467
6568pub fn runHighPriority (self : * Scheduler ) ! ? i32 {
66- return self .runQueue (& self .primary );
69+ return self .runQueue (& self .high_priority );
6770}
6871
6972pub fn runLowPriority (self : * Scheduler ) ! ? i32 {
70- return self .runQueue (& self .secondary );
73+ return self .runQueue (& self .low_priority );
7174}
7275
7376fn runQueue (self : * Scheduler , queue : * Queue ) ! ? i32 {
@@ -94,7 +97,7 @@ fn runQueue(self: *Scheduler, queue: *Queue) !?i32 {
9497
9598 var copy = task ;
9699 copy .ms = now + repeat_delay ;
97- try self .secondary .add (copy );
100+ try self .low_priority .add (copy );
98101 }
99102 _ = queue .remove ();
100103 next = queue .peek ();
@@ -144,11 +147,11 @@ test "Scheduler" {
144147 try testing .expectEqualSlices (u32 , &.{ 1 , 1 , 2 }, task .calls .items );
145148
146149 std .Thread .sleep (std .time .ns_per_ms * 5 );
147- // wont' run secondary
150+ // won't run low_priority
148151 try testing .expectEqual (null , try s .runHighPriority ());
149152 try testing .expectEqualSlices (u32 , &.{ 1 , 1 , 2 }, task .calls .items );
150153
151- //runs secondary
154+ //runs low_priority
152155 try testing .expectDelta (2 , try s .runLowPriority (), 1 );
153156 try testing .expectEqualSlices (u32 , &.{ 1 , 1 , 2 , 2 }, task .calls .items );
154157}
0 commit comments