stop using destructor callback for fetch

This commit is contained in:
Muki Kiboigo
2025-09-16 12:38:50 -07:00
parent fcd82b2c14
commit 3badcdbdbd
2 changed files with 12 additions and 20 deletions

View File

@@ -78,14 +78,6 @@ pub const FetchContext = struct {
.url = self.url,
};
}
pub fn destructor(self: *FetchContext) void {
if (self.transfer) |_| {
self.promise_resolver.reject("TypeError") catch unreachable;
self.promise_resolver.deinit();
self.transfer = null;
}
}
};
// https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch
@@ -122,9 +114,6 @@ pub fn fetch(input: RequestInput, options: ?RequestInit, page: *Page) !Env.Promi
.url = req.url,
};
// Add destructor callback for FetchContext.
try page.main_context.destructor_callbacks.append(arena, Env.DestructorCallback.init(fetch_ctx));
try page.http_client.request(.{
.ctx = @ptrCast(fetch_ctx),
.url = req.url,
@@ -200,15 +189,18 @@ pub fn fetch(input: RequestInput, options: ?RequestInit, page: *Page) !Env.Promi
.error_callback = struct {
fn errorCallback(ctx: *anyopaque, err: anyerror) void {
const self: *FetchContext = @ptrCast(@alignCast(ctx));
if (self.transfer != null) {
self.transfer = null;
defer self.promise_resolver.deinit();
self.transfer = null;
log.err(.http, "error", .{
.url = self.url,
.err = err,
.source = "fetch error",
});
log.err(.http, "error", .{
.url = self.url,
.err = err,
.source = "fetch error",
});
// We throw an Abort error when the page is getting closed so,
// in this case, we don't need to reject the promise.
if (err != error.Abort) {
self.promise_resolver.reject(@errorName(err)) catch unreachable;
}
}

View File

@@ -2945,11 +2945,11 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
// An interface for types that want to have their jsDeinit function to be
// called when the call context ends
pub const DestructorCallback = struct {
const DestructorCallback = struct {
ptr: *anyopaque,
destructorFn: *const fn (ptr: *anyopaque) void,
pub fn init(ptr: anytype) DestructorCallback {
fn init(ptr: anytype) DestructorCallback {
const T = @TypeOf(ptr);
const ptr_info = @typeInfo(T);