Skip to content

Commit da6b628

Browse files
committed
use destructor callback for FetchContext
1 parent 727e186 commit da6b628

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
@@ -2901,11 +2901,11 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
29012901

29022902
// An interface for types that want to have their jsDeinit function to be
29032903
// called when the call context ends
2904-
const DestructorCallback = struct {
2904+
pub const DestructorCallback = struct {
29052905
ptr: *anyopaque,
29062906
destructorFn: *const fn (ptr: *anyopaque) void,
29072907

2908-
fn init(ptr: anytype) DestructorCallback {
2908+
pub fn init(ptr: anytype) DestructorCallback {
29092909
const T = @TypeOf(ptr);
29102910
const ptr_info = @typeInfo(T);
29112911

0 commit comments

Comments
 (0)