refactor: add support for delayed registering of command collections

This commit is contained in:
CJ van den Berg 2025-11-06 18:13:52 +01:00
parent 64deb9cee6
commit 70cc191c4a
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -288,15 +288,31 @@ pub fn Collection(comptime Namespace: type) type {
const Self = @This();
pub fn init(self: *Self, targetPtr: *Target) !void {
if (cmds.len == 0)
@compileError("no commands found in type " ++ @typeName(Target) ++ " (did you mark them public?)");
inline for (cmds) |cmd|
@field(self.fields, cmd.name) = Closure(*Target).init(cmd.f, targetPtr, cmd.name, cmd.meta);
try self.register();
}
pub fn init_unregistered(self: *Self, targetPtr: *Target) void {
if (cmds.len == 0)
@compileError("no commands found in type " ++ @typeName(Target) ++ " (did you mark them public?)");
inline for (cmds) |cmd| {
@field(self.fields, cmd.name) = Closure(*Target).init(cmd.f, targetPtr, cmd.name, cmd.meta);
try @field(self.fields, cmd.name).register();
}
}
pub fn deinit(self: *Self) void {
self.unregister();
}
pub fn register(self: *Self) !void {
inline for (cmds) |cmd|
try @field(self.fields, cmd.name).register();
}
pub fn unregister(self: *Self) void {
inline for (cmds) |cmd|
Closure(*Target).unregister(&@field(self.fields, cmd.name));
}