@@ -58,6 +58,7 @@ pub const Browser = struct {
5858 allocator : Allocator ,
5959 http_client : * http.Client ,
6060 page_arena : ArenaAllocator ,
61+ notification : * Notification ,
6162
6263 pub fn init (app : * App ) ! Browser {
6364 const allocator = app .allocator ;
@@ -67,11 +68,15 @@ pub const Browser = struct {
6768 });
6869 errdefer env .deinit ();
6970
71+ const notification = try Notification .init (allocator , app .notification );
72+ errdefer notification .deinit ();
73+
7074 return .{
7175 .app = app ,
7276 .env = env ,
7377 .session = null ,
7478 .allocator = allocator ,
79+ .notification = notification ,
7580 .http_client = & app .http_client ,
7681 .page_arena = ArenaAllocator .init (allocator ),
7782 };
@@ -81,13 +86,14 @@ pub const Browser = struct {
8186 self .closeSession ();
8287 self .env .deinit ();
8388 self .page_arena .deinit ();
89+ self .notification .deinit ();
8490 }
8591
86- pub fn newSession (self : * Browser , ctx : anytype ) ! * Session {
92+ pub fn newSession (self : * Browser ) ! * Session {
8793 self .closeSession ();
8894 self .session = @as (Session , undefined );
8995 const session = & self .session .? ;
90- try Session .init (session , self , ctx );
96+ try Session .init (session , self );
9197 return session ;
9298 }
9399
@@ -119,32 +125,14 @@ pub const Session = struct {
119125
120126 page : ? Page = null ,
121127
122- // recipient of notification, passed as the first parameter to notify
123- notify_ctx : * anyopaque ,
124- notify_func : * const fn (ctx : * anyopaque , notification : * const Notification ) anyerror ! void ,
125-
126- fn init (self : * Session , browser : * Browser , ctx : anytype ) ! void {
127- const ContextT = @TypeOf (ctx );
128- const ContextStruct = switch (@typeInfo (ContextT )) {
129- .@"struct" = > ContextT ,
130- .pointer = > | ptr | ptr .child ,
131- .void = > NoopContext ,
132- else = > @compileError ("invalid context type" ),
133- };
134-
135- // ctx can be void, to be able to store it in our *anyopaque field, we
136- // need to play a little game.
137- const any_ctx : * anyopaque = if (@TypeOf (ctx ) == void ) @constCast (@ptrCast (&{})) else ctx ;
138-
128+ fn init (self : * Session , browser : * Browser ) ! void {
139129 var executor = try browser .env .newExecutor ();
140130 errdefer executor .deinit ();
141131
142132 const allocator = browser .app .allocator ;
143133 self .* = .{
144134 .browser = browser ,
145135 .executor = executor ,
146- .notify_ctx = any_ctx ,
147- .notify_func = ContextStruct .notify ,
148136 .arena = ArenaAllocator .init (allocator ),
149137 .storage_shed = storage .Shed .init (allocator ),
150138 .cookie_jar = storage .CookieJar .init (allocator ),
@@ -213,12 +201,6 @@ pub const Session = struct {
213201 .reason = .anchor ,
214202 });
215203 }
216-
217- fn notify (self : * const Session , notification : * const Notification ) void {
218- self .notify_func (self .notify_ctx , notification ) catch | err | {
219- log .err ("notify {}: {}" , .{ std .meta .activeTag (notification .* ), err });
220- };
221- }
222204};
223205
224206// Page navigates to an url.
@@ -350,20 +332,15 @@ pub const Page = struct {
350332 // redirect)
351333 self .url = request_url ;
352334
353- session .browser .app .telemetry .record (.{ .navigate = .{
354- .proxy = false ,
355- .tls = std .ascii .eqlIgnoreCase (request_url .scheme (), "https" ),
356- } });
357-
358335 // load the data
359336 var request = try self .newHTTPRequest (.GET , & self .url , .{ .navigation = true });
360337 defer request .deinit ();
361338
362- session .notify (&.{ . page_navigate = .{
339+ session .browser . notification . dispatch ( . page_navigate, & .{
363340 .url = & self .url ,
364341 .reason = opts .reason ,
365342 .timestamp = timestamp (),
366- } } );
343+ });
367344
368345 var response = try request .sendSync (.{});
369346
@@ -399,10 +376,10 @@ pub const Page = struct {
399376 self .raw_data = arr .items ;
400377 }
401378
402- session .notify (&.{ . page_navigated = .{
379+ session .browser . notification . dispatch ( . page_navigated, & .{
403380 .url = & self .url ,
404381 .timestamp = timestamp (),
405- } } );
382+ });
406383 }
407384
408385 // https://html.spec.whatwg.org/#read-html
0 commit comments