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

@@ -1,7 +1,3 @@
const std = @import("std");
const Provider = @import("provider/provider.zig").Provider;
const consts = @import("consts.zig");
pub const Item = struct {
allocator: std.mem.Allocator,
provider: Provider,
@@ -52,13 +48,13 @@ pub const Element = struct {
pub const Container = struct {
item: Item,
children: std.ArrayList(FullName),
children: locked(std.ArrayList(FullName)),
childrenLoading: bool,
fn deinit(self: *Container) void {
for (self.children.items) |itemFullName| {
for (self.children.data.items) |itemFullName| {
self.item.allocator.free(itemFullName.path);
}
self.children.deinit();
self.children.data.deinit();
self.item.allocator.destroy(self);
}
};
@@ -103,3 +99,8 @@ pub const AbsolutePathType = union(enum) {
container,
item,
};
const std = @import("std");
const Provider = @import("provider/provider.zig").Provider;
const consts = @import("consts.zig");
const locked = @import("./sync.zig").locked;

View File

@@ -18,7 +18,9 @@ fn loadChildren(container: *Container) void {
while (it.next() catch return) |entry| {
const child = container.item.fullName.getChild(entry.name, container.item.allocator) catch return;
container.children.append(child) catch return;
container.children.mutex.lock();
defer container.children.mutex.unlock();
container.children.data.append(child) catch return;
}
}
@@ -108,7 +110,9 @@ pub const LocalContentProvider = struct {
.directory => blk: {
const container = try allocator.create(Container);
container.* = Container{
.children = std.ArrayList(FullName).init(allocator),
.children = locked(std.ArrayList(FullName)){
.data = std.ArrayList(FullName).init(allocator),
},
.childrenLoading = true,
.item = undefined,
};
@@ -214,3 +218,4 @@ const ItemEnum = models.ItemEnum;
const Element = models.Element;
const Container = models.Container;
const native_os = @import("builtin").os.tag;
const locked = @import("../sync.zig").locked;

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,11 +200,21 @@ 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| {
location.children.mutex.lock();
defer location.children.mutex.unlock();
std.debug.assert(processed_number <= location.children.data.items.len);
if (!location.childrenLoading and processed_number == location.children.data.items.len) break;
if (processed_number == location.children.data.items.len) continue;
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 });
@@ -226,6 +238,7 @@ pub const Tab = struct {
self.updateCurrentItem() catch {};
}
}
}
pub fn deinit(self: *Tab) void {
if (self.currentLocation) |c| {