feat: base project
This commit is contained in:
65
src/core/tab/tab.zig
Normal file
65
src/core/tab/tab.zig
Normal file
@@ -0,0 +1,65 @@
|
||||
const std = @import("std");
|
||||
const assert = std.debug.assert;
|
||||
const Mutex = std.Thread.Mutex;
|
||||
|
||||
const models = @import("../models.zig");
|
||||
const Container = models.Container;
|
||||
const Item = models.Item;
|
||||
|
||||
pub const Tab = struct {
|
||||
allocator: std.mem.Allocator,
|
||||
currentLocation: ?*Container,
|
||||
currentItems: ?std.ArrayList(Item),
|
||||
threadPool: *std.Thread.Pool,
|
||||
_private: Private,
|
||||
|
||||
const Private = struct {
|
||||
currentItemsAllocator: ?std.heap.ArenaAllocator,
|
||||
};
|
||||
|
||||
pub fn create(
|
||||
threadPool: *std.Thread.Pool,
|
||||
allocator: std.mem.Allocator,
|
||||
) Tab {
|
||||
return Tab{
|
||||
.allocator = allocator,
|
||||
.currentItems = null,
|
||||
.currentLocation = null,
|
||||
.threadPool = threadPool,
|
||||
._private = .{
|
||||
.currentItemsAllocator = null,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
pub fn setCurrentLocation(self: *Tab, newLocation: *Container) void {
|
||||
if (self.currentLocation) |c| {
|
||||
c.item.deinit();
|
||||
}
|
||||
|
||||
self.currentLocation = newLocation;
|
||||
|
||||
//TODO: Proper error handling
|
||||
std.Thread.Pool.spawn(self.threadPool, loadItems, .{ self, newLocation }) catch unreachable;
|
||||
}
|
||||
|
||||
fn loadItems(self: *Tab, location: *Container) void {
|
||||
if (self._private.currentItemsAllocator) |arena| {
|
||||
arena.deinit();
|
||||
}
|
||||
|
||||
self._private.currentItemsAllocator = std.heap.ArenaAllocator.init(self.allocator);
|
||||
for (location.children.items) |item| {
|
||||
_ = item;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Tab) void {
|
||||
if (self.currentLocation) |c| {
|
||||
c.deinit();
|
||||
}
|
||||
if (self._private.currentItemsAllocator) |arena| {
|
||||
arena.deinit();
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user