feat(console): lazy load items
This commit is contained in:
7
src/core/sync.zig
Normal file
7
src/core/sync.zig
Normal file
@@ -0,0 +1,7 @@
|
||||
const std = @import("std");
|
||||
pub fn locked(comptime T: type) type {
|
||||
return struct {
|
||||
data: T,
|
||||
mutex: std.Thread.Mutex = .{},
|
||||
};
|
||||
}
|
||||
@@ -6,10 +6,12 @@ const models = @import("../models.zig");
|
||||
const Container = models.Container;
|
||||
const Item = models.Item;
|
||||
|
||||
const locked = @import("../sync.zig").locked;
|
||||
|
||||
pub const Tab = struct {
|
||||
allocator: std.mem.Allocator,
|
||||
currentLocation: ?*Container,
|
||||
currentItems: ?std.ArrayList(*Item),
|
||||
currentItems: locked(?std.ArrayList(*Item)),
|
||||
currentItemsChanged: bool = false,
|
||||
threadPool: *std.Thread.Pool,
|
||||
_private: Private,
|
||||
@@ -25,7 +27,7 @@ pub const Tab = struct {
|
||||
) void {
|
||||
self.* = Tab{
|
||||
.allocator = allocator,
|
||||
.currentItems = null,
|
||||
.currentItems = .{ .data = null },
|
||||
.currentLocation = null,
|
||||
.threadPool = threadPool,
|
||||
._private = .{
|
||||
@@ -52,15 +54,20 @@ pub const Tab = struct {
|
||||
if (self._private.currentItemsAllocator) |arena| {
|
||||
arena.deinit();
|
||||
}
|
||||
if (self.currentItems) |items| {
|
||||
items.deinit();
|
||||
|
||||
{
|
||||
self.currentItems.mutex.lock();
|
||||
defer self.currentItems.mutex.unlock();
|
||||
if (self.currentItems.data) |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};
|
||||
var threadSafeAllocator = std.heap.ThreadSafeAllocator{ .child_allocator = arena };
|
||||
const allocator = threadSafeAllocator.allocator();
|
||||
|
||||
errdefer {
|
||||
@@ -68,10 +75,15 @@ pub const Tab = struct {
|
||||
self._private.currentItemsAllocator = null;
|
||||
}
|
||||
|
||||
self.currentItems = std.ArrayList(*Item).init(allocator);
|
||||
errdefer {
|
||||
self.currentItems.?.deinit();
|
||||
self.currentItems = null;
|
||||
{
|
||||
self.currentItems.mutex.lock();
|
||||
defer self.currentItems.mutex.unlock();
|
||||
|
||||
self.currentItems.data = std.ArrayList(*Item).init(allocator);
|
||||
errdefer {
|
||||
self.currentItems.data.?.deinit();
|
||||
self.currentItems.data = null;
|
||||
}
|
||||
}
|
||||
|
||||
while (location.childrenLoading) {
|
||||
@@ -80,7 +92,11 @@ pub const Tab = struct {
|
||||
|
||||
for (location.children.items) |item| {
|
||||
const resolvedItem = try location.item.provider.getItemByFullName(item, &.{ .skipChildInit = false }, allocator);
|
||||
try self.currentItems.?.append(resolvedItem);
|
||||
|
||||
self.currentItems.mutex.lock();
|
||||
defer self.currentItems.mutex.unlock();
|
||||
|
||||
try self.currentItems.data.?.append(resolvedItem);
|
||||
self.currentItemsChanged = true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user