|
18 | 18 |
|
19 | 19 | const std = @import("std"); |
20 | 20 | const Page = @import("../../browser/page.zig").Page; |
| 21 | +const timestampF = @import("../../datetime.zig").timestamp; |
21 | 22 | const Notification = @import("../../notification.zig").Notification; |
22 | 23 |
|
23 | 24 | const Allocator = std.mem.Allocator; |
@@ -82,11 +83,33 @@ fn setLifecycleEventsEnabled(cmd: anytype) !void { |
82 | 83 | })) orelse return error.InvalidParams; |
83 | 84 |
|
84 | 85 | 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) { |
88 | 88 | bc.lifecycleEventsDisable(); |
| 89 | + return cmd.sendResult(null, .{}); |
89 | 90 | } |
| 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 | + |
90 | 113 | return cmd.sendResult(null, .{}); |
91 | 114 | } |
92 | 115 |
|
|
0 commit comments