feat: refactor3
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
running: bool = true,
|
||||
usage_number: locked(u16) = .{ .data = 0 },
|
||||
current_items: locked(?[]*models.Item) = .{ .data = null },
|
||||
current_items_allocator: std.heap.ArenaAllocator,
|
||||
allocator: std.mem.Allocator,
|
||||
appState: AppState,
|
||||
_private: Private,
|
||||
|
||||
@@ -9,9 +9,9 @@ const Private = struct {
|
||||
preCurrentItemsUnload: Observer(*Tab),
|
||||
};
|
||||
|
||||
pub fn init(model: *Self, currentItemsAllocator: std.heap.ArenaAllocator, appState: AppState) !void {
|
||||
pub fn init(model: *Self, allocator: std.mem.Allocator, appState: AppState) !void {
|
||||
model.* = Self{
|
||||
.current_items_allocator = currentItemsAllocator,
|
||||
.allocator = allocator,
|
||||
.appState = appState,
|
||||
._private = .{
|
||||
.preCurrentItemsUnload = Observer(*Tab){
|
||||
@@ -25,15 +25,13 @@ pub fn init(model: *Self, currentItemsAllocator: std.heap.ArenaAllocator, appSta
|
||||
}
|
||||
|
||||
pub fn updateCurrentItems(self: *Self, tab_current_items: *std.ArrayList(*models.Item)) !void {
|
||||
{
|
||||
self.current_items.mutex.lock();
|
||||
defer self.current_items.mutex.unlock();
|
||||
self.resetCurrentItems();
|
||||
|
||||
const items = try self.allocator.alloc(*models.Item, tab_current_items.items.len);
|
||||
errdefer {
|
||||
self.allocator.free(items);
|
||||
self.current_items.data = null;
|
||||
}
|
||||
_ = self.current_items_allocator.reset(.retain_capacity);
|
||||
const allocator = self.current_items_allocator.allocator();
|
||||
|
||||
const items = try allocator.alloc(*models.Item, tab_current_items.items.len);
|
||||
for (tab_current_items.items, 0..) |item, i| {
|
||||
items[i] = item;
|
||||
}
|
||||
@@ -55,17 +53,27 @@ pub fn preCurrentItemsUnload(ctx: *anyopaque, tab: *Tab) void {
|
||||
const self: *Self = @ptrCast(@alignCast(ctx));
|
||||
if (tab == self.appState.currentTab) {
|
||||
// @panic("asdasdasd");
|
||||
self.resetCurrentItems();
|
||||
}
|
||||
}
|
||||
|
||||
fn resetCurrentItems(self: *@This()) void {
|
||||
self.current_items.mutex.lock();
|
||||
defer self.current_items.mutex.unlock();
|
||||
|
||||
self.current_items.data = null;
|
||||
const data = self.current_items.data;
|
||||
// self.current_items.data = null;
|
||||
|
||||
if (data) |currentItems| {
|
||||
self.allocator.free(currentItems);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn deinit(self: *@This()) void {
|
||||
self.appState.tabPreCurrentItemsUnload.detach(&self._private.preCurrentItemsUnload);
|
||||
self.appState.deinit();
|
||||
self.current_items_allocator.deinit();
|
||||
|
||||
self.resetCurrentItems();
|
||||
}
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
@@ -3,7 +3,7 @@ const Model = struct {
|
||||
crash: bool = false,
|
||||
allocator: std.mem.Allocator,
|
||||
current_items_view: *vxfw.ListView,
|
||||
app_common_model: AppCommonModel,
|
||||
app_common_model: *AppCommonModel,
|
||||
root_provider: *RootProvider,
|
||||
|
||||
/// Helper function to return a vxfw.Widget struct
|
||||
@@ -119,9 +119,11 @@ const Model = struct {
|
||||
fn createCurrentItems(ctx: vxfw.DrawContext, vm: *Model) !void {
|
||||
if (vm.crash) @panic("asd123");
|
||||
|
||||
const text_items = blk2: {
|
||||
vm.app_common_model.current_items.mutex.lock();
|
||||
defer vm.app_common_model.current_items.mutex.unlock();
|
||||
const text_items = if (vm.app_common_model.current_items.data) |items| blk: {
|
||||
|
||||
break :blk2 if (vm.app_common_model.current_items.data) |items| blk: {
|
||||
const children = try ctx.arena.alloc(*vxfw.Text, items.len);
|
||||
for (0.., items[0..children.len]) |i, child| {
|
||||
const is_active = i == vm.current_items_view.cursor;
|
||||
@@ -164,6 +166,7 @@ const Model = struct {
|
||||
}
|
||||
break :blk children;
|
||||
} else &.{};
|
||||
};
|
||||
|
||||
const widgets = try ctx.arena.alloc(vxfw.Widget, text_items.len);
|
||||
for (text_items, 0..) |t, i| {
|
||||
@@ -266,19 +269,19 @@ pub fn main() !void {
|
||||
defer allocator.destroy(model);
|
||||
|
||||
var app_common_model: AppCommonModel = undefined;
|
||||
try AppCommonModel.init(&app_common_model, std.heap.ArenaAllocator.init(allocator), appState);
|
||||
try AppCommonModel.init(&app_common_model, allocator, appState);
|
||||
defer app_common_model.deinit();
|
||||
|
||||
model.* = .{
|
||||
.allocator = allocator,
|
||||
.app_common_model = app_common_model,
|
||||
.app_common_model = &app_common_model,
|
||||
.current_items_view = &list,
|
||||
.root_provider = &rootProvider,
|
||||
};
|
||||
|
||||
model.app_common_model.usage_number.data += 1;
|
||||
|
||||
try pool.spawn(core.data_loop, .{&model.app_common_model});
|
||||
try pool.spawn(core.data_loop, .{model.app_common_model});
|
||||
|
||||
var app = try vxfw.App.init(allocator);
|
||||
defer app.deinit();
|
||||
|
||||
@@ -55,7 +55,6 @@ pub const Tab = struct {
|
||||
|
||||
self.currentLocation = newLocationContainer;
|
||||
self.currentLocationChanged.notify(newLocationContainer);
|
||||
std.debug.print("\nASD1\n", .{});
|
||||
|
||||
//TODO: Proper error handling
|
||||
std.Thread.Pool.spawn(self.threadPool, loadItemsWrapper, .{ self, newLocationContainer }) catch unreachable;
|
||||
@@ -119,7 +118,6 @@ pub const Tab = struct {
|
||||
defer self.currentItems.mutex.unlock();
|
||||
self.currentItemsChanged = true;
|
||||
}
|
||||
std.debug.print("\nASDX\n", .{});
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Tab) void {
|
||||
|
||||
@@ -53,21 +53,19 @@ pub fn main() !void {
|
||||
const homeFullName: models.FullName = .{ .path = start_path };
|
||||
|
||||
var tab1 = try allocator.create(Tab);
|
||||
defer allocator.destroy(tab1);
|
||||
|
||||
tab1.init(&pool, &rootProvider, allocator);
|
||||
defer tab1.deinit();
|
||||
|
||||
try tab1.setCurrentLocation(homeFullName);
|
||||
|
||||
var appState: AppState = AppState.init(allocator);
|
||||
appState.currentTab = tab1;
|
||||
try appState.addTab(tab1);
|
||||
try appState.setCurrentTab(tab1);
|
||||
|
||||
const model = try allocator.create(Model);
|
||||
defer allocator.destroy(model);
|
||||
|
||||
var core_model: CoreModel = undefined;
|
||||
try CoreModel.init(&core_model, std.heap.ArenaAllocator.init(allocator), appState);
|
||||
try CoreModel.init(&core_model, allocator, appState);
|
||||
|
||||
model.* = .{
|
||||
.allocator = allocator,
|
||||
|
||||
Reference in New Issue
Block a user