feat: refactor2

This commit is contained in:
2025-05-23 15:35:55 +02:00
parent 0cb9387492
commit 882f38b90f
2 changed files with 28 additions and 24 deletions

View File

@@ -24,6 +24,33 @@ pub fn init(model: *Self, currentItemsAllocator: std.heap.ArenaAllocator, appSta
try model.appState.tabPreCurrentItemsUnload.attach(&model._private.preCurrentItemsUnload); try model.appState.tabPreCurrentItemsUnload.attach(&model._private.preCurrentItemsUnload);
} }
pub fn updateCurrentItems(self: *Self, tab_current_items: *std.ArrayList(*models.Item)) !void {
{
self.current_items.mutex.lock();
defer self.current_items.mutex.unlock();
self.current_items.data = null;
}
_ = self.current_items_allocator.reset(.retain_capacity);
const allocator = self.current_items_allocator.allocator();
const items = try allocator.alloc(*models.Item, tab_current_items.items.len);
for (tab_current_items.items, 0..) |item, i| {
items[i] = item;
}
std.mem.sort(*models.Item, items, {}, struct {
fn sort(_: void, lhs: *models.Item, rhs: *models.Item) bool {
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;
}
pub fn preCurrentItemsUnload(ctx: *anyopaque, tab: *Tab) void { pub fn preCurrentItemsUnload(ctx: *anyopaque, tab: *Tab) void {
const self: *Self = @ptrCast(@alignCast(ctx)); const self: *Self = @ptrCast(@alignCast(ctx));
if (tab == self.appState.currentTab) { if (tab == self.appState.currentTab) {

View File

@@ -19,30 +19,7 @@ fn inner_loop(appCommonModel: *Model) !void {
std.Thread.sleep(10 * std.time.ns_per_ms); std.Thread.sleep(10 * std.time.ns_per_ms);
if (tab.currentItems.data) |*tab_current_items| { if (tab.currentItems.data) |*tab_current_items| {
{ try appCommonModel.updateCurrentItems(tab_current_items);
appCommonModel.current_items.mutex.lock();
defer appCommonModel.current_items.mutex.unlock();
appCommonModel.current_items.data = null;
}
_ = appCommonModel.current_items_allocator.reset(.retain_capacity);
const allocator = appCommonModel.current_items_allocator.allocator();
const items = try allocator.alloc(*models.Item, tab_current_items.items.len);
for (tab_current_items.items, 0..) |item, i| {
items[i] = item;
}
std.mem.sort(*models.Item, items, {}, struct {
fn sort(_: void, lhs: *models.Item, rhs: *models.Item) bool {
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);
appCommonModel.current_items.mutex.lock();
defer appCommonModel.current_items.mutex.unlock();
appCommonModel.current_items.data = items;
tab.currentItemsChanged = false; tab.currentItemsChanged = false;
} }
} }