Skip to content

Commit c3c6441

Browse files
committed
loop: add cancel method
1 parent 1313740 commit c3c6441

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

src/loop.zig

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ pub const SingleThreaded = struct {
125125
}
126126
}
127127

128-
pub fn timeout(self: *Self, nanoseconds: u63, js_cbk: ?JSCallback) void {
128+
pub fn timeout(self: *Self, nanoseconds: u63, js_cbk: ?JSCallback) usize {
129129
const completion = self.alloc.create(IO.Completion) catch unreachable;
130130
completion.* = undefined;
131131
const ctx = self.alloc.create(ContextTimeout) catch unreachable;
@@ -138,6 +138,51 @@ pub const SingleThreaded = struct {
138138
if (builtin.is_test) {
139139
report("start timeout {d} for {d} nanoseconds", .{ old_events_nb + 1, nanoseconds });
140140
}
141+
142+
return @intFromPtr(completion);
143+
}
144+
145+
const ContextCancel = struct {
146+
loop: *Self,
147+
js_cbk: ?JSCallback,
148+
};
149+
150+
fn cancelCallback(
151+
ctx: *ContextCancel,
152+
completion: *IO.Completion,
153+
result: IO.CancelError!void,
154+
) void {
155+
defer ctx.loop.freeCbk(completion, ctx);
156+
157+
// TODO: return the error to the callback
158+
result catch |err| @panic(@errorName(err));
159+
160+
const old_events_nb = ctx.loop.removeEvent();
161+
if (builtin.is_test) {
162+
report("timeout done, remaining events: {d}", .{old_events_nb - 1});
163+
}
164+
165+
// js callback
166+
if (ctx.js_cbk) |js_cbk| {
167+
defer js_cbk.deinit(ctx.loop.alloc);
168+
js_cbk.call(null) catch {
169+
ctx.loop.cbk_error = true;
170+
};
171+
}
172+
}
173+
174+
pub fn cancel(self: *Self, id: usize, js_cbk: ?JSCallback) void {
175+
const comp_cancel: *IO.Completion = @ptrFromInt(id);
176+
177+
const completion = self.alloc.create(IO.Completion) catch unreachable;
178+
completion.* = undefined;
179+
const ctx = self.alloc.create(ContextTimeout) catch unreachable;
180+
ctx.* = ContextCancel{
181+
.loop = self,
182+
.js_cbk = js_cbk,
183+
};
184+
185+
self.io.cancel(*ContextCancel, ctx, cancelCallback, completion, comp_cancel);
141186
}
142187

143188
// Yield

src/tests/cbk_test.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub const Window = struct {
5252
) void {
5353
const n: u63 = @intCast(milliseconds);
5454
// TODO: check this value can be holded in u63
55-
loop.timeout(n * std.time.ns_per_ms, callback);
55+
_ = loop.timeout(n * std.time.ns_per_ms, callback);
5656
}
5757

5858
pub fn _cbkAsyncWithJSArg(
@@ -64,7 +64,7 @@ pub const Window = struct {
6464
) void {
6565
const n: u63 = @intCast(milliseconds);
6666
// TODO: check this value can be holded in u63
67-
loop.timeout(n * std.time.ns_per_ms, callback);
67+
_ = loop.timeout(n * std.time.ns_per_ms, callback);
6868
}
6969

7070
pub fn _cbkAsyncWithNatArg(_: Window, callback: Callback) !void {

0 commit comments

Comments
 (0)