diff --git a/src/browser/browser.zig b/src/browser/browser.zig index 700554db..033ae8d5 100644 --- a/src/browser/browser.zig +++ b/src/browser/browser.zig @@ -100,6 +100,13 @@ pub const Browser = struct { self.session = null; } } + + pub fn runMicrotasks(self: *const Browser) void { + // if no session exists, there is nothing to do. + if (self.session == null) return; + + return self.session.?.env.runMicrotasks(); + } }; // Session is like a browser's tab. @@ -237,7 +244,7 @@ pub const Session = struct { std.debug.assert(self.page != null); // Reset all existing callbacks. - self.app.loop.reset(); + self.app.loop.resetJS(); self.env.stop(); // TODO unload document: https://html.spec.whatwg.org/#unloading-documents diff --git a/src/cdp/runtime.zig b/src/cdp/runtime.zig index 0b7c8750..f0692ea8 100644 --- a/src/cdp/runtime.zig +++ b/src/cdp/runtime.zig @@ -44,21 +44,11 @@ fn sendInspector(cmd: anytype, action: anytype) !void { const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded; - // remove awaitPromise true params - // TODO: delete when Promise are correctly handled by zig-js-runtime - if (action == .callFunctionOn or action == .evaluate) { - const json = cmd.input.json; - if (std.mem.indexOf(u8, json, "\"awaitPromise\":true")) |_| { - // +1 because we'll be turning a true -> false - const buf = try cmd.arena.alloc(u8, json.len + 1); - _ = std.mem.replace(u8, json, "\"awaitPromise\":true", "\"awaitPromise\":false", buf); - bc.session.callInspector(buf); - return; - } - } - // the result to return is handled directly by the inspector. bc.session.callInspector(cmd.input.json); + + // force running micro tasks after send input to the inspector. + cmd.cdp.browser.runMicrotasks(); } pub const ExecutionContextCreated = struct { diff --git a/src/cdp/testing.zig b/src/cdp/testing.zig index 891fc67f..99e98d6a 100644 --- a/src/cdp/testing.zig +++ b/src/cdp/testing.zig @@ -64,6 +64,8 @@ const Browser = struct { const session = self.session orelse return false; return std.mem.eql(u8, session.id, session_id); } + + pub fn runMicrotasks(_: *const Browser) void {} }; const Session = struct { diff --git a/vendor/zig-js-runtime b/vendor/zig-js-runtime index d959ef86..8fd91765 160000 --- a/vendor/zig-js-runtime +++ b/vendor/zig-js-runtime @@ -1 +1 @@ -Subproject commit d959ef86cc60e1033980926e625e49c85fcda898 +Subproject commit 8fd9176583b90f5a5dda0c8e3310525ccc14427c