Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/example_dusty.zig
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,10 @@ const snippets = [_][]const u8{
fn executeScript(req: *dusty.Request, res: *dusty.Response) !void {
const sample = paramInt(u8, req, "sample") orelse 0;

var attribs = datastar.ScriptAttributes.init(req.arena);
try attribs.put("type", "text/javascript");
try attribs.put("trace", "true");
try attribs.put("aardvark", "should appear last, not first");
var attribs: datastar.ScriptAttributes = .empty;
try attribs.put(req.arena, "type", "text/javascript");
try attribs.put(req.arena, "trace", "true");
try attribs.put(req.arena, "aardvark", "should appear last, not first");

try beginSseBatch(res);
res.body = switch (sample) {
Expand Down
8 changes: 4 additions & 4 deletions examples/example_httpz.zig
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,10 @@ const snippets = [_][]const u8{
fn executeScript(req: *httpz.Request, res: *httpz.Response) !void {
const sample = paramInt(u8, req, "sample") orelse 0;

var attribs = datastar.ScriptAttributes.init(res.arena);
try attribs.put("type", "text/javascript");
try attribs.put("trace", "true");
try attribs.put("aardvark", "should appear last, not first");
var attribs: datastar.ScriptAttributes = .empty;
try attribs.put(res.arena, "type", "text/javascript");
try attribs.put(res.arena, "trace", "true");
try attribs.put(res.arena, "aardvark", "should appear last, not first");

beginSse(res);
res.body = switch (sample) {
Expand Down
52 changes: 24 additions & 28 deletions examples/example_stdlib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,14 @@ const PORT = 8081;

pub const std_options = std.Options{ .log_level = .debug };

var update_count: usize = 1;
var update_mutex: Io.Mutex = .init;
var update_count: std.atomic.Value(usize) = .init(1);

var prng: std.Random.DefaultPrng = .init(0);

var shared_io: Io = undefined;

fn getCountAndIncrement(io: Io) !usize {
try update_mutex.lock(io);
defer {
update_count += 1;
update_mutex.unlock(io);
}
return update_count;
fn getCountAndIncrement() usize {
return update_count.fetchAdd(1, .acq_rel);
}

var hotreload_id: u64 = 0;
Expand Down Expand Up @@ -183,17 +177,17 @@ fn respondJson(arena: std.mem.Allocator, request: *std.http.Server.Request, valu
},
},
});
defer body.end() catch {};
const jf = std.json.fmt(value, .{});
try jf.format(&body.writer);
try body.end();
}

// ----- Handlers -----

fn textHtml(arena: std.mem.Allocator, request: *std.http.Server.Request) !void {
const body = try std.fmt.allocPrint(arena,
\\<p id="text-html">This is update number {d}</p>
, .{try getCountAndIncrement(shared_io)});
, .{getCountAndIncrement()});
try request.respond(body, .{
.extra_headers = &.{.{ .name = "content-type", .value = "text/html; charset=UTF-8" }},
});
Expand All @@ -202,7 +196,7 @@ fn textHtml(arena: std.mem.Allocator, request: *std.http.Server.Request) !void {
fn patchElements(arena: std.mem.Allocator, request: *std.http.Server.Request) !void {
const block = try datastar.patchElementsFmt(arena,
\\<p id="mf-patch">This is update number {d}</p>
, .{try getCountAndIncrement(shared_io)}, .{});
, .{getCountAndIncrement()}, .{});
try respondSse(arena, request, block);
}

Expand All @@ -226,7 +220,7 @@ fn patchElementsOpts(arena: std.mem.Allocator, request: *std.http.Server.Request
, opts),
else => try datastar.patchElementsFmt(arena,
\\<p>This is update number {d}</p>
, .{try getCountAndIncrement(shared_io)}, opts),
, .{getCountAndIncrement()}, opts),
};
try respondSse(arena, request, body);
}
Expand Down Expand Up @@ -282,10 +276,10 @@ fn patchSignalsRemove(arena: std.mem.Allocator, request: *std.http.Server.Reques
}

fn executeScript(arena: std.mem.Allocator, request: *std.http.Server.Request, sample: u8) !void {
var attribs = datastar.ScriptAttributes.init(arena);
try attribs.put("type", "text/javascript");
try attribs.put("trace", "true");
try attribs.put("aardvark", "should appear last, not first");
var attribs: datastar.ScriptAttributes = .empty;
try attribs.put(arena, "type", "text/javascript");
try attribs.put(arena, "trace", "true");
try attribs.put(arena, "aardvark", "should appear last, not first");

const body = switch (sample) {
1 => try datastar.executeScript(
Expand Down Expand Up @@ -316,9 +310,8 @@ fn executeScript(arena: std.mem.Allocator, request: *std.http.Server.Request, sa

// ----- Long-lived streaming SSE -----

fn beginStream(request: *std.http.Server.Request) !std.http.BodyWriter {
var buf: [4096]u8 = undefined;
var body = try request.respondStreaming(&buf, .{
fn beginStream(buffer: []u8, request: *std.http.Server.Request) !std.http.BodyWriter {
var body = try request.respondStreaming(buffer, .{
.respond_options = .{
.extra_headers = &.{
.{ .name = "content-type", .value = "text/event-stream; charset=UTF-8" },
Expand All @@ -333,8 +326,8 @@ fn beginStream(request: *std.http.Server.Request) !std.http.BodyWriter {
fn svgMorph(arena: std.mem.Allocator, request: *std.http.Server.Request) !void {
const opt = try datastar.readSignals(struct { svgMorph: usize = 1 }, arena, request);

var body = try beginStream(request);
defer body.end() catch {};
var body_buffer: [4096]u8 = undefined;
var body = try beginStream(&body_buffer, request);

var frame_buf: [4096]u8 = undefined;
var fba: std.heap.FixedBufferAllocator = .init(&frame_buf);
Expand All @@ -359,6 +352,8 @@ fn svgMorph(arena: std.mem.Allocator, request: *std.http.Server.Request) !void {
});
try shared_io.sleep(.fromMilliseconds(100), .real);
}

try body.end();
}

fn emitSvgFrame(body: *std.http.BodyWriter, fba: *std.heap.FixedBufferAllocator, comptime fmt: []const u8, args: anytype) !void {
Expand Down Expand Up @@ -391,8 +386,8 @@ fn mathMorph(arena: std.mem.Allocator, request: *std.http.Server.Request) !void
return;
}

var stream = try beginStream(request);
defer stream.end() catch {};
var stream_buffer: [4096]u8 = undefined;
var stream = try beginStream(&stream_buffer, request);

var frame_buf: [4096]u8 = undefined;
var fba: std.heap.FixedBufferAllocator = .init(&frame_buf);
Expand All @@ -417,8 +412,7 @@ fn mathMorph(arena: std.mem.Allocator, request: *std.http.Server.Request) !void
fba.reset();
const reset_block = try datastar.patchSignals(fba.allocator(), .{ .mathmlMorph = 1 }, .{});
try stream.writer.writeAll(reset_block);
try stream.writer.flush();
try stream.flush();
try stream.end();
}

const snippets = [_][]const u8{
Expand Down Expand Up @@ -469,8 +463,8 @@ fn mimeTest(arena: std.mem.Allocator, request: *std.http.Server.Request, filenam
}

fn hotreload(request: *std.http.Server.Request, id: u64) !void {
var stream = try beginStream(request);
defer stream.end() catch {};
var stream_buffer: [4096]u8 = undefined;
var stream = try beginStream(&stream_buffer, request);

var frame_buf: [1024]u8 = undefined;
var fba: std.heap.FixedBufferAllocator = .init(&frame_buf);
Expand Down Expand Up @@ -498,4 +492,6 @@ fn hotreload(request: *std.http.Server.Request, id: u64) !void {
try stream.writer.flush();
try stream.flush();
}

try stream.end();
}
2 changes: 1 addition & 1 deletion hello_world/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ fn streamHello(io: Io, arena: std.mem.Allocator, request: *std.http.Server.Reque
},
},
});
defer body.end() catch {};
try body.flush(); // push headers to the wire before first SSE event

for (0..MESSAGE.len) |i| {
Expand All @@ -118,4 +117,5 @@ fn streamHello(io: Io, arena: std.mem.Allocator, request: *std.http.Server.Reque
}
}
std.debug.print("\n", .{});
try body.end();
}
Loading