19
19
const std = @import ("std" );
20
20
21
21
const parser = @import ("netsurf" );
22
+ const jsruntime = @import ("jsruntime" );
23
+ const Callback = jsruntime .Callback ;
24
+ const CallbackArg = jsruntime .CallbackArg ;
25
+ const Loop = jsruntime .Loop ;
22
26
23
27
const EventTarget = @import ("../dom/event_target.zig" ).EventTarget ;
24
28
@@ -39,6 +43,11 @@ pub const Window = struct {
39
43
40
44
storageShelf : ? * storage.Shelf = null ,
41
45
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
+
42
51
pub fn create (target : ? []const u8 ) Window {
43
52
return Window {
44
53
.target = target orelse "" ,
@@ -82,4 +91,26 @@ pub const Window = struct {
82
91
if (self .storageShelf == null ) return parser .DOMError .NotSupported ;
83
92
return & self .storageShelf .? .bucket .session ;
84
93
}
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
+ }
85
116
};
0 commit comments