cdp: run microtasks after send inspector

This commit is contained in:
Pierre Tachoire
2025-03-24 16:07:40 +01:00
parent c6cb6d5eeb
commit 3f1d0df7f9
3 changed files with 12 additions and 13 deletions

View File

@@ -100,6 +100,13 @@ pub const Browser = struct {
self.session = null; 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. // Session is like a browser's tab.

View File

@@ -44,21 +44,11 @@ fn sendInspector(cmd: anytype, action: anytype) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded; 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. // the result to return is handled directly by the inspector.
bc.session.callInspector(cmd.input.json); bc.session.callInspector(cmd.input.json);
// force running micro tasks after send input to the inspector.
cmd.cdp.browser.runMicrotasks();
} }
pub const ExecutionContextCreated = struct { pub const ExecutionContextCreated = struct {

View File

@@ -64,6 +64,8 @@ const Browser = struct {
const session = self.session orelse return false; const session = self.session orelse return false;
return std.mem.eql(u8, session.id, session_id); return std.mem.eql(u8, session.id, session_id);
} }
pub fn runMicrotasks(_: *const Browser) void {}
}; };
const Session = struct { const Session = struct {