Added events examples | Default AppExit event
This commit is contained in:
parent
7d4957d819
commit
81335aeddf
3 changed files with 62 additions and 0 deletions
42
README.md
42
README.md
|
|
@ -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**
|
**Simple**
|
||||||
``` zig
|
``` zig
|
||||||
test "Main" {
|
test "Main" {
|
||||||
|
|
|
||||||
18
src/app.zig
18
src/app.zig
|
|
@ -101,6 +101,24 @@ test "Main" {
|
||||||
var app: Self = .init(std.testing.allocator);
|
var app: Self = .init(std.testing.allocator);
|
||||||
defer app.deinit();
|
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
|
{ // PLUGINS
|
||||||
// --- ADD PLUGINS ---
|
// --- ADD PLUGINS ---
|
||||||
try app.plugins.addMany(&.{
|
try app.plugins.addMany(&.{
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
pub const Event = @import("event.zig");
|
pub const Event = @import("event.zig");
|
||||||
pub const Storage = @import("storage.zig");
|
pub const Storage = @import("storage.zig");
|
||||||
pub const Queue = @import("queue.zig").Queue;
|
pub const Queue = @import("queue.zig").Queue;
|
||||||
|
|
||||||
|
pub const AppExit = struct { reason: []const u8 = "" };
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue