Skip to content

Commit fe6ccad

Browse files
committed
loop.run now takes a maximum wait time
1 parent 11fe793 commit fe6ccad

File tree

4 files changed

+10
-12
lines changed

4 files changed

+10
-12
lines changed

src/browser/page.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,12 @@ pub const Page = struct {
144144
return self.fetchData("module", src);
145145
}
146146

147-
pub fn wait(self: *Page) !void {
147+
pub fn wait(self: *Page, wait_time: usize) !void {
148148
var try_catch: Env.TryCatch = undefined;
149149
try_catch.init(self.main_context);
150150
defer try_catch.deinit();
151151

152-
try self.session.browser.app.loop.run();
152+
try self.session.browser.app.loop.run(wait_time);
153153

154154
if (try_catch.hasCaught() == false) {
155155
log.debug(.browser, "page wait complete", .{});

src/main.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ fn run(alloc: Allocator) !void {
126126
},
127127
};
128128

129-
try page.wait();
129+
try page.wait(std.time.ns_per_s * 3);
130130

131131
// dump
132132
if (opts.dump) {

src/runtime/loop.zig

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,16 @@ pub const Loop = struct {
102102
// Stops when there is no more I/O events registered on the loop.
103103
// Note that I/O events callbacks might register more I/O events
104104
// on the go when they are executed (ie. nested I/O events).
105-
pub fn run(self: *Self) !void {
105+
pub fn run(self: *Self, wait_time: usize) !void {
106106
// stop repeating / interval timeouts from re-registering
107107
self.stopping = true;
108108
defer self.stopping = false;
109109

110-
while (self.pending_network_count != 0 or self.pending_timeout_count != 0) {
110+
const max_iterations = wait_time / (std.time.ns_per_ms * 10);
111+
for (0..max_iterations) |_| {
112+
if (self.pending_network_count == 0 and self.pending_timeout_count == 0) {
113+
break;
114+
}
111115
self.io.run_for_ns(std.time.ns_per_ms * 10) catch |err| {
112116
log.err(.loop, "deinit", .{ .err = err });
113117
break;
@@ -187,12 +191,6 @@ pub const Loop = struct {
187191
}
188192

189193
pub fn timeout(self: *Self, nanoseconds: u63, callback_node: ?*CallbackNode) !usize {
190-
if (self.stopping) {
191-
// Prevents a timeout callback from creating a new timeout, which
192-
// would make `loop.run` run forever.
193-
return 0;
194-
}
195-
196194
const completion = try self.alloc.create(Completion);
197195
errdefer self.alloc.destroy(completion);
198196
completion.* = undefined;

src/testing.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ pub const JsRunner = struct {
435435
}
436436
return err;
437437
};
438-
try self.page.loop.run();
438+
try self.page.loop.run(std.time.ns_per_ms * 200);
439439
@import("root").js_runner_duration += std.time.Instant.since(try std.time.Instant.now(), start);
440440

441441
if (case.@"1") |expected| {

0 commit comments

Comments
 (0)