1919const std = @import ("std" );
2020
2121const parser = @import ("netsurf" );
22+ const jsruntime = @import ("jsruntime" );
23+ const Callback = jsruntime .Callback ;
24+ const CallbackArg = jsruntime .CallbackArg ;
25+ const Loop = jsruntime .Loop ;
2226
2327const EventTarget = @import ("../dom/event_target.zig" ).EventTarget ;
2428
@@ -39,6 +43,11 @@ pub const Window = struct {
3943
4044 storageShelf : ? * storage.Shelf = null ,
4145
46+ // store a map between internal timeouts ids and pointers to uint.
47+ // the maximum number of possible timeouts is fixed.
48+ timeoutid : u32 = 0 ,
49+ timeoutids : [512 ]u64 = undefined ,
50+
4251 pub fn create (target : ? []const u8 ) Window {
4352 return Window {
4453 .target = target orelse "" ,
@@ -82,4 +91,26 @@ pub const Window = struct {
8291 if (self .storageShelf == null ) return parser .DOMError .NotSupported ;
8392 return & self .storageShelf .? .bucket .session ;
8493 }
94+
95+ // TODO handle callback arguments.
96+ pub fn _setTimeout (self : * Window , loop : * Loop , cbk : Callback , delay : ? u32 ) ! u32 {
97+ if (self .timeoutid >= self .timeoutids .len ) return error .TooMuchTimeout ;
98+
99+ const ddelay : u63 = delay orelse 0 ;
100+ const id = loop .timeout (ddelay * std .time .ns_per_ms , cbk );
101+
102+ self .timeoutids [self .timeoutid ] = id ;
103+ defer self .timeoutid += 1 ;
104+
105+ return self .timeoutid ;
106+ }
107+
108+ pub fn _clearTimeout (self : * Window , loop : * Loop , id : u32 ) void {
109+ // I do would prefer return an error in this case, but it seems some JS
110+ // uses invalid id, in particular id 0.
111+ // So we silently ignore invalid id for now.
112+ if (id >= self .timeoutid ) return ;
113+
114+ loop .cancel (self .timeoutids [id ], null );
115+ }
85116};
0 commit comments