feat(core): appstate

This commit is contained in:
2025-05-19 08:48:50 +02:00
parent 4eda4d335b
commit cbeed4003a
14 changed files with 454 additions and 103 deletions

41
src/core/observable.zig Normal file
View File

@@ -0,0 +1,41 @@
pub fn Observable(T: type) type {
return struct {
observers: std.ArrayList(*const Observer(T)),
const Self = @This();
pub fn init(allocator: std.mem.Allocator) Self {
return .{
.observers = std.ArrayList(*const Observer(T)).init(allocator),
};
}
pub fn deinit(self: *Self) void {
self.observers.deinit();
}
pub fn attach(self: *Self, obs: *const Observer(T)) !void {
try self.observers.append(obs);
}
pub fn detach(self: *Self, obs: *const Observer(T)) void {
if (std.mem.indexOfScalar(*const Observer, self.observers.items, obs)) |index| {
_ = self.observers.swapRemove(index);
}
}
pub fn notify(self: Self, args: T) void {
for (self.observers.items) |obs| {
obs.update(obs.ctx, args);
}
}
};
}
pub fn Observer(T: type) type {
return struct {
ctx: *anyopaque,
update: *const fn (ctx: *anyopaque, args: T) void,
};
}
const std = @import("std");