Skip to content

Commit 2d8a959

Browse files
committed
cdp: dispatch lifecycle events when enable
1 parent a7c3bad commit 2d8a959

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/cdp/domains/page.zig

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
const std = @import("std");
2020
const Page = @import("../../browser/page.zig").Page;
21+
const timestampF = @import("../../datetime.zig").timestamp;
2122
const Notification = @import("../../notification.zig").Notification;
2223

2324
const Allocator = std.mem.Allocator;
@@ -82,11 +83,33 @@ fn setLifecycleEventsEnabled(cmd: anytype) !void {
8283
})) orelse return error.InvalidParams;
8384

8485
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
85-
if (params.enabled) {
86-
try bc.lifecycleEventsEnable();
87-
} else {
86+
87+
if (params.enabled == false) {
8888
bc.lifecycleEventsDisable();
89+
return cmd.sendResult(null, .{});
8990
}
91+
92+
// Enable lifecycle events.
93+
try bc.lifecycleEventsEnable();
94+
95+
// When we enable lifecycle events, we must dispatch events for all
96+
// attached targets.
97+
const page = bc.session.currentPage() orelse return error.PageNotLoaded;
98+
99+
if (page.load_state == .complete) {
100+
try sendPageLifecycle(bc, "DOMContentLoaded", timestampF());
101+
try sendPageLifecycle(bc, "load", timestampF());
102+
103+
const http_active = page.http_client.active;
104+
const total_network_activity = http_active + page.http_client.intercepted;
105+
if (page.notified_network_almost_idle.check(total_network_activity <= 2)) {
106+
try sendPageLifecycle(bc, "networkAlmostIdle", timestampF());
107+
}
108+
if (page.notified_network_idle.check(total_network_activity == 0)) {
109+
try sendPageLifecycle(bc, "networkIdle", timestampF());
110+
}
111+
}
112+
90113
return cmd.sendResult(null, .{});
91114
}
92115

0 commit comments

Comments
 (0)