Skip to content

Commit 1d82e62

Browse files
committed
use destructor callback for FetchContext
1 parent 4413f68 commit 1d82e62

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/browser/fetch/fetch.zig

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub const Interfaces = .{
4343
@import("Response.zig"),
4444
};
4545

46-
const FetchContext = struct {
46+
pub const FetchContext = struct {
4747
arena: std.mem.Allocator,
4848
js_ctx: *Env.JsContext,
4949
promise_resolver: v8.Persistent(v8.PromiseResolver),
@@ -79,6 +79,17 @@ const FetchContext = struct {
7979
.url = self.url,
8080
};
8181
}
82+
83+
pub fn destructor(self: *FetchContext) void {
84+
if (self.transfer) |_| {
85+
const resolver = Env.PromiseResolver{
86+
.js_context = self.js_ctx,
87+
.resolver = self.promise_resolver.castToPromiseResolver(),
88+
};
89+
90+
resolver.reject("TypeError") catch unreachable;
91+
}
92+
}
8293
};
8394

8495
// https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch
@@ -107,6 +118,9 @@ pub fn fetch(input: RequestInput, options: ?RequestInit, page: *Page) !Env.Promi
107118
.url = req.url,
108119
};
109120

121+
// Add destructor callback for FetchContext.
122+
try page.main_context.destructor_callbacks.append(arena, Env.DestructorCallback.init(fetch_ctx));
123+
110124
try page.http_client.request(.{
111125
.ctx = @ptrCast(fetch_ctx),
112126
.url = req.url,
@@ -189,13 +203,6 @@ pub fn fetch(input: RequestInput, options: ?RequestInit, page: *Page) !Env.Promi
189203
.err = err,
190204
.source = "fetch error",
191205
});
192-
193-
const promise_resolver: Env.PromiseResolver = .{
194-
.js_context = self.js_ctx,
195-
.resolver = self.promise_resolver.castToPromiseResolver(),
196-
};
197-
198-
promise_resolver.reject(@errorName(err)) catch unreachable;
199206
}
200207
}.errorCallback,
201208
});

src/runtime/js.zig

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

28722872
// An interface for types that want to have their jsDeinit function to be
28732873
// called when the call context ends
2874-
const DestructorCallback = struct {
2874+
pub const DestructorCallback = struct {
28752875
ptr: *anyopaque,
28762876
destructorFn: *const fn (ptr: *anyopaque) void,
28772877

2878-
fn init(ptr: anytype) DestructorCallback {
2878+
pub fn init(ptr: anytype) DestructorCallback {
28792879
const T = @TypeOf(ptr);
28802880
const ptr_info = @typeInfo(T);
28812881

0 commit comments

Comments
 (0)