@@ -43,6 +43,11 @@ pub const Window = struct {
4343
4444 storageShelf : ? * storage.Shelf = null ,
4545
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+
4651 pub fn create (target : ? []const u8 ) Window {
4752 return Window {
4853 .target = target orelse "" ,
@@ -88,14 +93,20 @@ pub const Window = struct {
8893 }
8994
9095 // TODO handle callback arguments.
91- pub fn _setTimeout (_ : * Window , loop : * Loop , cbk : Callback , delay : ? u32 ) ! u32 {
96+ pub fn _setTimeout (self : * Window , loop : * Loop , cbk : Callback , delay : ? u32 ) ! u32 {
97+ if (self .timeoutid >= self .timeoutids .len ) return error .TooMuchTimeout ;
98+
9299 const ddelay : u63 = delay orelse 0 ;
93- loop .timeout (ddelay * std .time .ns_per_ms , cbk );
94- // TODO handle timeout ID
95- return 1 ;
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 ;
96106 }
97107
98- pub fn _clearTimeout (_ : * Window , _ : * Loop , id : u32 ) ! void {
99- _ = id ;
108+ pub fn _clearTimeout (self : * Window , loop : * Loop , id : u32 ) ! void {
109+ if (id >= self .timeoutid ) return error .InvalidTimeoutId ;
110+ loop .cancel (self .timeoutids [id ], null );
100111 }
101112};
0 commit comments