loop.run now takes a maximum wait time

This commit is contained in:
Karl Seguin
2025-06-23 16:43:28 +08:00
parent 11fe79312d
commit fe6ccad485
4 changed files with 10 additions and 12 deletions

View File

@@ -144,12 +144,12 @@ pub const Page = struct {
return self.fetchData("module", src); return self.fetchData("module", src);
} }
pub fn wait(self: *Page) !void { pub fn wait(self: *Page, wait_time: usize) !void {
var try_catch: Env.TryCatch = undefined; var try_catch: Env.TryCatch = undefined;
try_catch.init(self.main_context); try_catch.init(self.main_context);
defer try_catch.deinit(); defer try_catch.deinit();
try self.session.browser.app.loop.run(); try self.session.browser.app.loop.run(wait_time);
if (try_catch.hasCaught() == false) { if (try_catch.hasCaught() == false) {
log.debug(.browser, "page wait complete", .{}); log.debug(.browser, "page wait complete", .{});

View File

@@ -126,7 +126,7 @@ fn run(alloc: Allocator) !void {
}, },
}; };
try page.wait(); try page.wait(std.time.ns_per_s * 3);
// dump // dump
if (opts.dump) { if (opts.dump) {

View File

@@ -102,12 +102,16 @@ pub const Loop = struct {
// Stops when there is no more I/O events registered on the loop. // Stops when there is no more I/O events registered on the loop.
// Note that I/O events callbacks might register more I/O events // Note that I/O events callbacks might register more I/O events
// on the go when they are executed (ie. nested I/O events). // on the go when they are executed (ie. nested I/O events).
pub fn run(self: *Self) !void { pub fn run(self: *Self, wait_time: usize) !void {
// stop repeating / interval timeouts from re-registering // stop repeating / interval timeouts from re-registering
self.stopping = true; self.stopping = true;
defer self.stopping = false; defer self.stopping = false;
while (self.pending_network_count != 0 or self.pending_timeout_count != 0) { const max_iterations = wait_time / (std.time.ns_per_ms * 10);
for (0..max_iterations) |_| {
if (self.pending_network_count == 0 and self.pending_timeout_count == 0) {
break;
}
self.io.run_for_ns(std.time.ns_per_ms * 10) catch |err| { self.io.run_for_ns(std.time.ns_per_ms * 10) catch |err| {
log.err(.loop, "deinit", .{ .err = err }); log.err(.loop, "deinit", .{ .err = err });
break; break;
@@ -187,12 +191,6 @@ pub const Loop = struct {
} }
pub fn timeout(self: *Self, nanoseconds: u63, callback_node: ?*CallbackNode) !usize { pub fn timeout(self: *Self, nanoseconds: u63, callback_node: ?*CallbackNode) !usize {
if (self.stopping) {
// Prevents a timeout callback from creating a new timeout, which
// would make `loop.run` run forever.
return 0;
}
const completion = try self.alloc.create(Completion); const completion = try self.alloc.create(Completion);
errdefer self.alloc.destroy(completion); errdefer self.alloc.destroy(completion);
completion.* = undefined; completion.* = undefined;

View File

@@ -435,7 +435,7 @@ pub const JsRunner = struct {
} }
return err; return err;
}; };
try self.page.loop.run(); try self.page.loop.run(std.time.ns_per_ms * 200);
@import("root").js_runner_duration += std.time.Instant.since(try std.time.Instant.now(), start); @import("root").js_runner_duration += std.time.Instant.since(try std.time.Instant.now(), start);
if (case.@"1") |expected| { if (case.@"1") |expected| {