Skip to content

Commit 2a969f9

Browse files
committed
stop using destructor callback for fetch
1 parent 2a0964f commit 2a969f9

File tree

2 files changed

+12
-20
lines changed

2 files changed

+12
-20
lines changed

src/browser/fetch/fetch.zig

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,6 @@ pub const FetchContext = struct {
7878
.url = self.url,
7979
};
8080
}
81-
82-
pub fn destructor(self: *FetchContext) void {
83-
if (self.transfer) |_| {
84-
self.promise_resolver.reject("TypeError") catch unreachable;
85-
self.promise_resolver.deinit();
86-
self.transfer = null;
87-
}
88-
}
8981
};
9082

9183
// 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
122114
.url = req.url,
123115
};
124116

125-
// Add destructor callback for FetchContext.
126-
try page.main_context.destructor_callbacks.append(arena, Env.DestructorCallback.init(fetch_ctx));
127-
128117
try page.http_client.request(.{
129118
.ctx = @ptrCast(fetch_ctx),
130119
.url = req.url,
@@ -200,15 +189,18 @@ pub fn fetch(input: RequestInput, options: ?RequestInit, page: *Page) !Env.Promi
200189
.error_callback = struct {
201190
fn errorCallback(ctx: *anyopaque, err: anyerror) void {
202191
const self: *FetchContext = @ptrCast(@alignCast(ctx));
203-
if (self.transfer != null) {
204-
self.transfer = null;
192+
defer self.promise_resolver.deinit();
193+
self.transfer = null;
205194

206-
log.err(.http, "error", .{
207-
.url = self.url,
208-
.err = err,
209-
.source = "fetch error",
210-
});
195+
log.err(.http, "error", .{
196+
.url = self.url,
197+
.err = err,
198+
.source = "fetch error",
199+
});
211200

201+
// We throw an Abort error when the page is getting closed so,
202+
// in this case, we don't need to reject the promise.
203+
if (err != error.Abort) {
212204
self.promise_resolver.reject(@errorName(err)) catch unreachable;
213205
}
214206
}

src/runtime/js.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2945,11 +2945,11 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
29452945

29462946
// An interface for types that want to have their jsDeinit function to be
29472947
// called when the call context ends
2948-
pub const DestructorCallback = struct {
2948+
const DestructorCallback = struct {
29492949
ptr: *anyopaque,
29502950
destructorFn: *const fn (ptr: *anyopaque) void,
29512951

2952-
pub fn init(ptr: anytype) DestructorCallback {
2952+
fn init(ptr: anytype) DestructorCallback {
29532953
const T = @TypeOf(ptr);
29542954
const ptr_info = @typeInfo(T);
29552955

0 commit comments

Comments
 (0)