feat(model): implement Elm architecture
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 1m2s
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 1m2s
Now the `App` contains a state which is a user-defined `struct` which is passed to the `handle` and `contents` callbacks for `Container`'s and `Element`'s. Built-in `Element`'s shall not access the `App.Model` and should therefore never cause any side-effects. User-defined events shall be used to act as *messages* to cause potential side-effects for the model. This is the reason why only the `handle` callback has a non-const pointer to the `App.Model`. The `contents` callback can only access the `App.Model` read-only to use for generating the *view* (in context of the elm architecture).
This commit is contained in:
@@ -34,10 +34,10 @@ pub const Renderer = struct {
|
||||
@memset(this.screen, .{});
|
||||
}
|
||||
|
||||
pub fn render(this: *@This(), comptime T: type, container: *const T) !void {
|
||||
pub fn render(this: *@This(), comptime T: type, container: *const T, comptime Model: type, model: *const Model) !void {
|
||||
const size: Point = container.size;
|
||||
const origin: Point = container.origin;
|
||||
const cells: []const Cell = try container.content();
|
||||
const cells: []const Cell = try container.content(model);
|
||||
|
||||
if (cells.len == 0) return;
|
||||
|
||||
@@ -58,7 +58,7 @@ pub const Renderer = struct {
|
||||
// free immediately
|
||||
container.allocator.free(cells);
|
||||
|
||||
for (container.elements.items) |*element| try this.render(T, element);
|
||||
for (container.elements.items) |*element| try this.render(T, element, Model, model);
|
||||
}
|
||||
|
||||
pub fn save(this: @This(), writer: anytype) !void {
|
||||
@@ -74,6 +74,8 @@ pub const Renderer = struct {
|
||||
/// Create a .zon file containing the expected `Cell` slice using the `zterm.testing.Renderer.save` method:
|
||||
///
|
||||
/// ```zig
|
||||
/// const Model = struct {};
|
||||
/// var model: Model = .{};
|
||||
/// const file = try std.fs.cwd().createFile("test/container/border/all.zon", .{ .truncate = true });
|
||||
/// defer file.close();
|
||||
///
|
||||
@@ -81,8 +83,8 @@ pub const Renderer = struct {
|
||||
/// var renderer: testing.Renderer = .init(allocator, size);
|
||||
/// defer renderer.deinit();
|
||||
///
|
||||
/// try container.handle(.{ .size = size });
|
||||
/// try renderer.render(Container(event.SystemEvent), &container);
|
||||
/// try container.handle(&model, .{ .size = size });
|
||||
/// try renderer.render(@TypeOf(container), &container, Model, &.{});
|
||||
/// try renderer.save(file.writer());
|
||||
/// ```
|
||||
///
|
||||
@@ -91,7 +93,8 @@ pub const Renderer = struct {
|
||||
/// Then later load that .zon file at compile time and run your test against this `Cell` slice.
|
||||
///
|
||||
/// ```zig
|
||||
/// var container: Container(event.SystemEvent) = try .init(std.testing.allocator, .{
|
||||
/// const Model = struct {};
|
||||
/// var container: Container(Model, event.SystemEvent) = try .init(std.testing.allocator, .{
|
||||
/// .border = .{
|
||||
/// .color = .green,
|
||||
/// .sides = .all,
|
||||
@@ -102,16 +105,16 @@ pub const Renderer = struct {
|
||||
/// try testing.expectContainerScreen(.{
|
||||
/// .rows = 20,
|
||||
/// .cols = 30,
|
||||
/// }, &container, @import("test/container/border.all.zon"));
|
||||
/// }, @TypeOf(container), &container, Model, @import("test/container/border.all.zon"));
|
||||
/// ```
|
||||
pub fn expectContainerScreen(size: Point, container: *Container(event.SystemEvent), expected: []const Cell) !void {
|
||||
pub fn expectContainerScreen(size: Point, comptime T: type, container: *T, comptime Model: type, expected: []const Cell) !void {
|
||||
const allocator = testing.allocator;
|
||||
var renderer: Renderer = .init(allocator, size);
|
||||
defer renderer.deinit();
|
||||
|
||||
container.resize(size);
|
||||
container.reposition(.{});
|
||||
try renderer.render(Container(event.SystemEvent), container);
|
||||
try renderer.render(T, container, Model, &.{});
|
||||
|
||||
try expectEqualCells(.{}, renderer.size, expected, renderer.screen);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user