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

View File

@@ -1,14 +1,48 @@
const std = @import("std");
const models = @import("../core/models.zig");
const Tab = @import("../core/tab/tab.zig").Tab;
const locked = @import("../core/sync.zig").locked;
running: bool = true,
usage_number: locked(u16) = .{ .data = 0 },
current_items: locked(?[]*models.Item) = .{ .data = null },
current_items_allocator: std.heap.ArenaAllocator,
tab: *Tab,
appState: AppState,
_private: Private,
const Private = struct {
preCurrentItemsUnload: Observer(*Tab),
};
pub fn init(model: *Self, currentItemsAllocator: std.heap.ArenaAllocator, appState: AppState) !void {
model.* = Self{
.current_items_allocator = currentItemsAllocator,
.appState = appState,
._private = .{
.preCurrentItemsUnload = Observer(*Tab){
.ctx = @ptrCast(@alignCast(model)),
.update = preCurrentItemsUnload,
},
},
};
try model.appState.tabPreCurrentItemsUnload.attach(&model._private.preCurrentItemsUnload);
}
pub fn preCurrentItemsUnload(ctx: *anyopaque, tab: *Tab) void {
const self: *Self = @ptrCast(@alignCast(ctx));
if (tab == self.appState.currentTab) {
// @panic("asdasdasd");
self.current_items.mutex.lock();
defer self.current_items.mutex.unlock();
self.current_items.data = null;
}
}
pub fn deinit(self: *@This()) void {
self.current_items_allocator.deinit();
}
const std = @import("std");
const models = @import("../core/models.zig");
const Tab = @import("../core/tab/tab.zig").Tab;
const locked = @import("../core/sync.zig").locked;
const AppState = @import("../core/app_state.zig").AppState;
const Observer = @import("../core/observable.zig").Observer;
const Self = @This();

View File

@@ -11,18 +11,24 @@ pub fn data_loop(vm: *Model) void {
vm.usage_number.data -= 1;
}
fn inner_loop(vm: *Model) !void {
if (vm.tab.currentItemsChanged) {
const tab = vm.appState.currentTab;
if (tab.currentItemsChanged) {
std.Thread.sleep(10 * std.time.ns_per_ms);
vm.tab.currentItems.mutex.lock();
defer vm.tab.currentItems.mutex.unlock();
tab.currentItems.mutex.lock();
defer tab.currentItems.mutex.unlock();
if (vm.tab.currentItems.data) |current_items| {
if (tab.currentItems.data) |tab_current_items| {
{
vm.current_items.mutex.lock();
defer vm.current_items.mutex.unlock();
vm.current_items.data = null;
}
_ = vm.current_items_allocator.reset(.retain_capacity);
const allocator = vm.current_items_allocator.allocator();
const items = try allocator.alloc(*models.Item, current_items.items.len);
for (current_items.items, 0..) |item, i| {
const items = try allocator.alloc(*models.Item, tab_current_items.items.len);
for (tab_current_items.items, 0..) |item, i| {
items[i] = item;
}
@@ -37,8 +43,7 @@ fn inner_loop(vm: *Model) !void {
vm.current_items.mutex.lock();
defer vm.current_items.mutex.unlock();
vm.current_items.data = items;
vm.tab.currentItemsChanged = false;
tab.currentItemsChanged = false;
}
}
}