feat(core): incrementally load items

This commit is contained in:
2025-05-27 17:48:47 +02:00
parent ae8ead1e5d
commit 1c0004a5d5
3 changed files with 49 additions and 30 deletions

View File

@@ -2,6 +2,7 @@ pub const Tab = struct {
allocator: std.mem.Allocator,
currentLocation: ?*Container,
currentItems: locked(?std.ArrayList(Arc(*Item))),
currentParentItems: locked(?std.ArrayList(Arc(*Item))),
currentLocationChanged: Observable(?*Container),
currentItemFullName: ?models.FullName,
currentItem: ?Arc(*models.Item),
@@ -19,6 +20,7 @@ pub const Tab = struct {
self.* = Tab{
.allocator = allocator,
.currentItems = .{ .data = null },
.currentParentItems = .{ .data = null },
.currentLocationChanged = Observable(?*Container).init(allocator),
.currentLocation = null,
.currentRequested = null,
@@ -198,32 +200,43 @@ pub const Tab = struct {
}
//TODO: add async
while (location.childrenLoading) {
std.Thread.sleep(1 * std.time.ns_per_ms);
}
//NOTE: also we should
var processed_number: usize = 0;
while (true) {
std.Thread.sleep(100 * std.time.ns_per_ms);
for (location.children.items) |item_fullname| {
const resolvedItem = location.item.provider.getItemByFullName(item_fullname, &.{ .skipChildInit = true }, self.allocator) catch |e| {
//TODO: save error to container errors
std.debug.print("error {} {s}\r\n", .{ e, item_fullname.path });
continue;
};
location.children.mutex.lock();
defer location.children.mutex.unlock();
self.currentItems.mutex.lock();
defer self.currentItems.mutex.unlock();
std.debug.assert(processed_number <= location.children.data.items.len);
const insertIndex = for (self.currentItems.data.?.items, 0..) |*item, i| {
if (order.orderByDisplayName(resolvedItem, item.value.*) == .lt) break i;
} else null;
if (!location.childrenLoading and processed_number == location.children.data.items.len) break;
if (processed_number == location.children.data.items.len) continue;
const arc = try Arc(*Item).init(self.allocator, resolvedItem);
if (insertIndex) |i| {
try self.currentItems.data.?.insert(i, arc);
} else {
try self.currentItems.data.?.append(arc);
defer processed_number = location.children.data.items.len;
for (location.children.data.items[processed_number..]) |item_fullname| {
const resolvedItem = location.item.provider.getItemByFullName(item_fullname, &.{ .skipChildInit = true }, self.allocator) catch |e| {
//TODO: save error to container errors
std.debug.print("error {} {s}\r\n", .{ e, item_fullname.path });
continue;
};
self.currentItems.mutex.lock();
defer self.currentItems.mutex.unlock();
const insertIndex = for (self.currentItems.data.?.items, 0..) |*item, i| {
if (order.orderByDisplayName(resolvedItem, item.value.*) == .lt) break i;
} else null;
const arc = try Arc(*Item).init(self.allocator, resolvedItem);
if (insertIndex) |i| {
try self.currentItems.data.?.insert(i, arc);
} else {
try self.currentItems.data.?.append(arc);
}
self.updateCurrentItem() catch {};
}
self.updateCurrentItem() catch {};
}
}