From 2ea6557fb7e1e0e35dcd9231291b0dcfab4833cf Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Tue, 6 Jan 2026 15:08:44 +0100 Subject: [PATCH] add initEvent into Factory and remove default value for Event._time_stamp --- src/browser/Factory.zig | 22 ++++++++++++++-------- src/browser/webapi/Event.zig | 2 +- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/browser/Factory.zig b/src/browser/Factory.zig index 1a79931a..fc3f2abf 100644 --- a/src/browser/Factory.zig +++ b/src/browser/Factory.zig @@ -168,6 +168,18 @@ pub fn eventTarget(self: *Factory, child: anytype) !*@TypeOf(child) { return chain.get(1); } +fn eventInit(typ: []const u8, value: anytype, page: *Page) !Event { + // Round to 2ms for privacy (browsers do this) + const raw_timestamp = @import("../datetime.zig").milliTimestamp(.monotonic); + const time_stamp = (raw_timestamp / 2) * 2; + + return .{ + ._type = unionInit(Event.Type, value), + ._type_string = try String.init(page.arena, typ, .{}), + ._time_stamp = time_stamp, + }; +} + // this is a root object pub fn event(self: *Factory, typ: []const u8, child: anytype) !*@TypeOf(child) { const allocator = self._slab.allocator(); @@ -178,10 +190,7 @@ pub fn event(self: *Factory, typ: []const u8, child: anytype) !*@TypeOf(child) { // Special case: Event has a _type_string field, so we need manual setup const event_ptr = chain.get(0); - event_ptr.* = .{ - ._type = unionInit(Event.Type, chain.get(1)), - ._type_string = try String.init(self._page.arena, typ, .{}), - }; + event_ptr.* = try eventInit(typ, chain.get(1), self._page); chain.setLeaf(1, child); return chain.get(1); @@ -196,10 +205,7 @@ pub fn uiEvent(self: *Factory, typ: []const u8, child: anytype) !*@TypeOf(child) // Special case: Event has a _type_string field, so we need manual setup const event_ptr = chain.get(0); - event_ptr.* = .{ - ._type = unionInit(Event.Type, chain.get(1)), - ._type_string = try String.init(self._page.arena, typ, .{}), - }; + event_ptr.* = try eventInit(typ, chain.get(1), self._page); chain.setMiddle(1, UIEvent.Type); chain.setLeaf(2, child); diff --git a/src/browser/webapi/Event.zig b/src/browser/webapi/Event.zig index c5b0d724..c658cd2f 100644 --- a/src/browser/webapi/Event.zig +++ b/src/browser/webapi/Event.zig @@ -40,7 +40,7 @@ _prevent_default: bool = false, _stop_propagation: bool = false, _stop_immediate_propagation: bool = false, _event_phase: EventPhase = .none, -_time_stamp: u64 = 0, +_time_stamp: u64, _needs_retargeting: bool = false, _isTrusted: bool = false,