feat: cleanup appstate, move sort to tab

This commit is contained in:
2025-05-26 17:22:23 +02:00
parent c4c901f1d4
commit 095b8659d6
9 changed files with 270 additions and 159 deletions

View File

@@ -1,6 +1,5 @@
running: bool = true,
usage_number: locked(u16) = .{ .data = 0 },
current_items: locked(?[]Arc(*models.Item).Weak) = .{ .data = null },
allocator: std.mem.Allocator,
appState: AppState,
@@ -12,66 +11,8 @@ pub fn init(model: *Self, allocator: std.mem.Allocator, appState: AppState) !voi
}
pub fn updateCurrentItems(self: *Self, tab_current_items: *std.ArrayList(Arc(*models.Item))) !void {
self.resetCurrentItems();
var items = try self.allocator.alloc(Arc(*models.Item).Weak, tab_current_items.items.len);
errdefer {
self.allocator.free(items);
self.current_items.data = null;
}
for (tab_current_items.items, 0..) |*item, i| {
items[i] = item.downgrade();
}
std.mem.sort(Arc(*models.Item).Weak, items, {}, struct {
fn sort(_: void, lhs_weak: Arc(*models.Item).Weak, rhs_weak: Arc(*models.Item).Weak) bool {
const lhs_arc = @constCast(&lhs_weak).upgrade();
defer if (lhs_arc) |l1| if (l1.releaseUnwrap()) |l2| l2.deinit();
const rhs_arc = @constCast(&rhs_weak).upgrade();
defer if (rhs_arc) |r1| if (r1.releaseUnwrap()) |r2| r2.deinit();
const lhs = if (lhs_arc) |l|
l.value.*
else
return false;
const rhs = if (rhs_arc) |r|
r.value.*
else
return false;
if (lhs.item == .container and rhs.item == .element) return true;
if (lhs.item == .element and rhs.item == .container) return false;
return std.mem.order(u8, lhs.displayName, rhs.displayName) == .lt;
}
}.sort);
self.current_items.mutex.lock();
defer self.current_items.mutex.unlock();
self.current_items.data = items;
}
fn resetCurrentItems(self: *@This()) void {
self.current_items.mutex.lock();
defer self.current_items.mutex.unlock();
const data = self.current_items.data;
// self.current_items.data = null;
if (data) |currentItems| {
for (currentItems) |item| {
item.release();
}
self.allocator.free(currentItems);
}
}
pub fn deinit(self: *@This()) void {
self.appState.deinit();
self.resetCurrentItems();
}
const std = @import("std");

View File

@@ -1,26 +0,0 @@
const std = @import("std");
const models = @import("../core/models.zig");
const Model = @import("Model.zig");
pub fn data_loop(vm: *Model) void {
vm.usage_number.data += 1;
while (vm.running) {
inner_loop(vm) catch {};
std.Thread.sleep(100 * std.time.ns_per_ms);
}
vm.usage_number.data -= 1;
}
fn inner_loop(appCommonModel: *Model) !void {
const tab = appCommonModel.appState.currentTab;
tab.currentItems.mutex.lock();
defer tab.currentItems.mutex.unlock();
if (tab.currentItemsChanged) {
std.Thread.sleep(10 * std.time.ns_per_ms);
if (tab.currentItems.data) |*tab_current_items| {
try appCommonModel.updateCurrentItems(tab_current_items);
tab.currentItemsChanged = false;
}
}
}