Skip to content

Commit 39b3786

Browse files
committed
cdp: ctx state has init and deinit now
1 parent 8b22313 commit 39b3786

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

src/cdp/cdp.zig

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const IncomingMessage = @import("msg.zig").IncomingMessage;
3434
const Input = @import("msg.zig").Input;
3535
const inspector = @import("inspector.zig").inspector;
3636
const dom = @import("dom.zig").dom;
37+
const cdpdom = @import("dom.zig");
3738
const css = @import("css.zig").css;
3839
const security = @import("security.zig").security;
3940

@@ -129,6 +130,33 @@ pub const State = struct {
129130
loaderID: []const u8 = LoaderID,
130131

131132
page_life_cycle_events: bool = false, // TODO; Target based value
133+
134+
// DOM
135+
nodelist: cdpdom.NodeList,
136+
nodesearchlist: cdpdom.NodeSearchList,
137+
138+
pub fn init(alloc: std.mem.Allocator) State {
139+
return .{
140+
.nodelist = cdpdom.NodeList.init(alloc),
141+
.nodesearchlist = cdpdom.NodeSearchList.init(alloc),
142+
};
143+
}
144+
145+
pub fn deinit(self: *State) void {
146+
self.nodelist.deinit();
147+
148+
// deinit all node searches.
149+
for (self.nodesearchlist.items) |*s| s.deinit();
150+
self.nodesearchlist.deinit();
151+
}
152+
153+
pub fn reset(self: *State) void {
154+
self.nodelist.reset();
155+
156+
// deinit all node searches.
157+
for (self.nodesearchlist.items) |*s| s.deinit();
158+
self.nodesearchlist.clearAndFree();
159+
}
132160
};
133161

134162
// Utils

src/cdp/page.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ fn navigate(
259259
log.debug("Req > id {d}, method {s}", .{ input.id, "page.navigate" });
260260

261261
// change state
262+
ctx.state.reset();
262263
ctx.state.url = input.params.url;
263264
// TODO: hard coded ID
264265
ctx.state.loaderID = "AF8667A203C5392DBE9AC290044AA4C2";

src/server.zig

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,17 @@ pub const Ctx = struct {
6969
last_active: ?std.time.Instant = null,
7070

7171
// CDP
72-
state: cdp.State = .{},
72+
state: cdp.State = undefined,
7373

7474
// JS fields
7575
browser: *Browser, // TODO: is pointer mandatory here?
7676
sessionNew: bool,
7777
// try_catch: jsruntime.TryCatch, // TODO
7878

79+
pub fn deinit(self: *Ctx) void {
80+
self.state.deinit();
81+
}
82+
7983
// callbacks
8084
// ---------
8185

@@ -458,7 +462,10 @@ pub fn handle(
458462
.accept_completion = &accept_completion,
459463
.conn_completion = &conn_completion,
460464
.timeout_completion = &timeout_completion,
465+
.state = cdp.State.init(browser.session.alloc),
461466
};
467+
defer ctx.deinit();
468+
462469
try browser.session.initInspector(
463470
&ctx,
464471
Ctx.onInspectorResp,

0 commit comments

Comments
 (0)