Skip to content

Commit c3fd0db

Browse files
Merge pull request #223 from lightpanda-io/settimout
dom: first draft for window setTimeout
2 parents 89f898c + aeaa745 commit c3fd0db

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/html/window.zig

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
const std = @import("std");
2020

2121
const parser = @import("netsurf");
22+
const jsruntime = @import("jsruntime");
23+
const Callback = jsruntime.Callback;
24+
const CallbackArg = jsruntime.CallbackArg;
25+
const Loop = jsruntime.Loop;
2226

2327
const 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
};

vendor/zig-js-runtime

0 commit comments

Comments
 (0)