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.current_items.mutex.lock();
|
||||
defer self.current_items.mutex.unlock();
|
||||
self.resetCurrentItems();
|
||||
}
|
||||
}
|
||||
|
||||
self.current_items.data = null;
|
||||
fn resetCurrentItems(self: *@This()) void {
|
||||
self.current_items.mutex.lock();
|
||||
defer self.current_items.mutex.unlock();
|
||||
|
||||
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");
|
||||
|
||||
Reference in New Issue
Block a user