Skip to content

Commit c305858

Browse files
committed
checks the low priority queue for timings and enforces task flush on connection wait
1 parent f362601 commit c305858

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/browser/page.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,9 @@ pub const Page = struct {
394394
return err;
395395
},
396396
.raw_done => {
397+
// Run scheduler to clean up any pending tasks
398+
_ = try scheduler.run();
399+
397400
if (exit_when_done) {
398401
return .done;
399402
}

src/lib.zig

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export fn lightpanda_cdp_browser_context(cdp_ptr: *anyopaque) *anyopaque {
153153
return &cdp.browser_context.?;
154154
}
155155

156-
// returns -1 if no session/page, or if no events reamin, otherwise returns
156+
// returns -1 if no session/page, or if no events reamin, otherwise returns
157157
// milliseconds until next scheduled task
158158
export fn lightpanda_cdp_page_wait(cdp_ptr: *anyopaque, ms: i32) c_int {
159159
const cdp: *CDP = @ptrCast(@alignCast(cdp_ptr));
@@ -165,14 +165,20 @@ export fn lightpanda_cdp_page_wait(cdp_ptr: *anyopaque, ms: i32) c_int {
165165
}
166166

167167
fn cdp_peek_next_delay_ms(scheduler: *Scheduler) ?i32 {
168-
if (scheduler.high_priority.count() == 0) {
169-
return null;
170-
}
168+
var queue = queue: {
169+
if (scheduler.high_priority.count() == 0) {
170+
if (scheduler.low_priority.count() == 0) return null;
171+
break :queue scheduler.low_priority;
172+
} else {
173+
break :queue scheduler.high_priority;
174+
}
175+
};
171176

172177
const now = std.time.milliTimestamp();
173-
const next_task = scheduler.high_priority.peek().?;
174-
const time_to_next = next_task.ms - now;
178+
// we know this must exist because the count was not 0.
179+
const next_task = queue.peek().?;
175180

181+
const time_to_next = next_task.ms - now;
176182
return if (time_to_next > 0) @intCast(time_to_next) else 0;
177183
}
178184

0 commit comments

Comments
 (0)