Added events examples | Default AppExit event

This commit is contained in:
abux 2026-07-10 22:41:36 +01:00
parent 7d4957d819
commit 81335aeddf
3 changed files with 62 additions and 0 deletions

View file

@ -30,6 +30,48 @@ pub fn render(ctx: App.scheduler.Context) !void {
}
```
**Simple Events**
``` zig
// --- SEND MANY EVENTS ---
try app.events.sendMany(&.{
event.AppExit{
.reason = "Yes",
},
});
// --- MAIN GAME LOOP ---
while (true) {
// --- USE AND EAT EVENT ---
if (app.events.consume(event.AppExit)) |app_exit| {
std.debug.print("Exit Reason: {s}\n", .{
app_exit.reason,
});
break;
}
}
```
**Event Queue**
``` zig
const Register = struct {
username: []const u8,
};
// --- QUEUE ---
if (app.events.get(event.Register)) |queue| {
// --- GET EVERY EVENT ---
for (queue.items()) |register| {
// --- DEBUG ---
std.debug.print("New Register: Username(s)\n", .{
register.username
});
// ...
}
// --- CLEAR EVENT QUEUE ---
queue.clear();
}
```
**Simple**
``` zig
test "Main" {

View file

@ -101,6 +101,24 @@ test "Main" {
var app: Self = .init(std.testing.allocator);
defer app.deinit();
// --- SEND MANY EVENTS ---
try app.events.sendMany(&.{
event.AppExit{
.reason = "Yes",
},
});
// --- MAIN GAME LOOP ---
while (true) {
// --- USE AND EAT EVENT ---
if (app.events.consume(event.AppExit)) |app_exit| {
std.debug.print("| {s}\n", .{
app_exit.reason,
});
break;
}
}
{ // PLUGINS
// --- ADD PLUGINS ---
try app.plugins.addMany(&.{

View file

@ -1,3 +1,5 @@
pub const Event = @import("event.zig");
pub const Storage = @import("storage.zig");
pub const Queue = @import("queue.zig").Queue;
pub const AppExit = struct { reason: []const u8 = "" };