mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-30 07:31:47 +00:00
async: remove context from loop impl init
This commit is contained in:
@@ -42,23 +42,20 @@ pub const Conn = struct {
|
||||
loop: *Loop,
|
||||
|
||||
pub fn connect(self: *Conn, socket: std.os.socket_t, address: std.net.Address) !void {
|
||||
var cmd = Command{ .impl = undefined };
|
||||
cmd.impl = NetworkImpl.init(self.loop, &cmd);
|
||||
cmd.impl.connect(socket, address);
|
||||
var cmd = Command{ .impl = NetworkImpl.init(self.loop) };
|
||||
cmd.impl.connect(&cmd, socket, address);
|
||||
_ = try cmd.wait();
|
||||
}
|
||||
|
||||
pub fn send(self: *Conn, socket: std.os.socket_t, buffer: []const u8) !usize {
|
||||
var cmd = Command{ .impl = undefined };
|
||||
cmd.impl = NetworkImpl.init(self.loop, &cmd);
|
||||
cmd.impl.send(socket, buffer);
|
||||
var cmd = Command{ .impl = NetworkImpl.init(self.loop) };
|
||||
cmd.impl.send(&cmd, socket, buffer);
|
||||
return try cmd.wait();
|
||||
}
|
||||
|
||||
pub fn receive(self: *Conn, socket: std.os.socket_t, buffer: []u8) !usize {
|
||||
var cmd = Command{ .impl = undefined };
|
||||
cmd.impl = NetworkImpl.init(self.loop, &cmd);
|
||||
cmd.impl.receive(socket, buffer);
|
||||
var cmd = Command{ .impl = NetworkImpl.init(self.loop) };
|
||||
cmd.impl.receive(&cmd, socket, buffer);
|
||||
return try cmd.wait();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -77,11 +77,10 @@ const AsyncClient = struct {
|
||||
|
||||
pub fn deinit(self: *AsyncRequest) void {
|
||||
self.headers.deinit();
|
||||
self.cli.allocator.destroy(self);
|
||||
}
|
||||
|
||||
pub fn fetch(self: *AsyncRequest) void {
|
||||
return self.impl.yield();
|
||||
return self.impl.yield(self);
|
||||
}
|
||||
|
||||
fn onerr(self: *AsyncRequest, err: anyerror) void {
|
||||
@@ -118,16 +117,13 @@ const AsyncClient = struct {
|
||||
self.cli.deinit();
|
||||
}
|
||||
|
||||
pub fn create(self: *AsyncClient, uri: std.Uri) !*AsyncRequest {
|
||||
var req = try self.cli.allocator.create(AsyncRequest);
|
||||
req.* = AsyncRequest{
|
||||
.impl = undefined,
|
||||
pub fn create(self: *AsyncClient, uri: std.Uri) !AsyncRequest {
|
||||
return .{
|
||||
.impl = YieldImpl.init(self.cli.loop),
|
||||
.cli = &self.cli,
|
||||
.uri = uri,
|
||||
.headers = .{ .allocator = self.cli.allocator, .owned = false },
|
||||
};
|
||||
req.impl = YieldImpl.init(self.cli.loop, req);
|
||||
return req;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -140,7 +136,7 @@ test "non blocking client" {
|
||||
var client = AsyncClient.init(alloc, &loop);
|
||||
defer client.deinit();
|
||||
|
||||
var reqs: [10]*AsyncClient.AsyncRequest = undefined;
|
||||
var reqs: [10]AsyncClient.AsyncRequest = undefined;
|
||||
for (0..reqs.len) |i| {
|
||||
reqs[i] = try client.create(try std.Uri.parse(url));
|
||||
reqs[i].fetch();
|
||||
|
||||
@@ -98,23 +98,17 @@ pub const XMLHttpRequest = struct {
|
||||
asyn: bool = true,
|
||||
err: ?anyerror = null,
|
||||
|
||||
pub fn constructor(alloc: std.mem.Allocator, loop: *Loop) !*XMLHttpRequest {
|
||||
var req = try alloc.create(XMLHttpRequest);
|
||||
req.* = XMLHttpRequest{
|
||||
pub fn constructor(alloc: std.mem.Allocator, loop: *Loop) !XMLHttpRequest {
|
||||
return .{
|
||||
.proto = try XMLHttpRequestEventTarget.constructor(),
|
||||
.headers = .{ .allocator = alloc, .owned = false },
|
||||
.impl = undefined,
|
||||
.impl = YieldImpl.init(loop),
|
||||
.url = null,
|
||||
.uri = undefined,
|
||||
.readyState = UNSENT,
|
||||
// TODO retrieve the HTTP client globally to reuse existing connections.
|
||||
.cli = .{
|
||||
.allocator = alloc,
|
||||
.loop = loop,
|
||||
},
|
||||
.cli = .{ .allocator = alloc, .loop = loop },
|
||||
};
|
||||
req.impl = YieldImpl.init(loop, req);
|
||||
return req;
|
||||
}
|
||||
|
||||
pub fn deinit(self: *XMLHttpRequest, alloc: std.mem.Allocator) void {
|
||||
@@ -123,7 +117,6 @@ pub const XMLHttpRequest = struct {
|
||||
if (self.url) |url| alloc.free(url);
|
||||
// TODO the client must be shared between requests.
|
||||
self.cli.deinit();
|
||||
alloc.destroy(self);
|
||||
}
|
||||
|
||||
pub fn get_readyState(self: *XMLHttpRequest) u16 {
|
||||
@@ -176,7 +169,7 @@ pub const XMLHttpRequest = struct {
|
||||
}
|
||||
|
||||
pub fn _send(self: *XMLHttpRequest) void {
|
||||
self.impl.yield();
|
||||
self.impl.yield(self);
|
||||
}
|
||||
|
||||
fn onerr(self: *XMLHttpRequest, err: anyerror) void {
|
||||
|
||||
Reference in New Issue
Block a user