feat(console+core): tui, core
This commit is contained in:
@@ -9,7 +9,8 @@ const Item = models.Item;
|
||||
pub const Tab = struct {
|
||||
allocator: std.mem.Allocator,
|
||||
currentLocation: ?*Container,
|
||||
currentItems: ?std.ArrayList(Item),
|
||||
currentItems: ?std.ArrayList(*Item),
|
||||
currentItemsChanged: bool = false,
|
||||
threadPool: *std.Thread.Pool,
|
||||
_private: Private,
|
||||
|
||||
@@ -41,17 +42,46 @@ pub const Tab = struct {
|
||||
self.currentLocation = newLocation;
|
||||
|
||||
//TODO: Proper error handling
|
||||
std.Thread.Pool.spawn(self.threadPool, loadItems, .{ self, newLocation }) catch unreachable;
|
||||
std.Thread.Pool.spawn(self.threadPool, loadItemsWrapper, .{ self, newLocation }) catch unreachable;
|
||||
}
|
||||
|
||||
fn loadItems(self: *Tab, location: *Container) void {
|
||||
fn loadItemsWrapper(self: *Tab, location: *Container) void {
|
||||
loadItems(self, location) catch return;
|
||||
}
|
||||
fn loadItems(self: *Tab, location: *Container) !void {
|
||||
if (self._private.currentItemsAllocator) |arena| {
|
||||
arena.deinit();
|
||||
}
|
||||
if (self.currentItems) |items| {
|
||||
items.deinit();
|
||||
}
|
||||
|
||||
self._private.currentItemsAllocator = std.heap.ArenaAllocator.init(self.allocator);
|
||||
const arenaAllocator = &self._private.currentItemsAllocator.?;
|
||||
const arena = arenaAllocator.allocator();
|
||||
|
||||
var threadSafeAllocator = std.heap.ThreadSafeAllocator{.child_allocator = arena};
|
||||
const allocator = threadSafeAllocator.allocator();
|
||||
|
||||
errdefer {
|
||||
arenaAllocator.deinit();
|
||||
self._private.currentItemsAllocator = null;
|
||||
}
|
||||
|
||||
self.currentItems = std.ArrayList(*Item).init(allocator);
|
||||
errdefer {
|
||||
self.currentItems.?.deinit();
|
||||
self.currentItems = null;
|
||||
}
|
||||
|
||||
while (location.childrenLoading) {
|
||||
std.Thread.sleep(1 * std.time.ns_per_ms);
|
||||
}
|
||||
|
||||
for (location.children.items) |item| {
|
||||
_ = item;
|
||||
const resolvedItem = try location.item.provider.getItemByFullName(item, &.{ .skipChildInit = false }, allocator);
|
||||
try self.currentItems.?.append(resolvedItem);
|
||||
self.currentItemsChanged = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,6 +92,5 @@ pub const Tab = struct {
|
||||
if (self._private.currentItemsAllocator) |arena| {
|
||||
arena.deinit();
|
||||
}
|
||||
self.allocator.destroy(self);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user