This repository was archived by the owner on Nov 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
feat: only mark child changed if child is modified in TreeView #79
Open
ensi321
wants to merge
1
commit into
main
Choose a base branch
from
nc/tree-view-parent-change
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+26
−10
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,15 +78,29 @@ pub fn TreeView(comptime ST: type) type { | |
| allocator: std.mem.Allocator, | ||
| pool: *Node.Pool, | ||
| data: Data, | ||
| parent_change: ?ParentChange = null, | ||
| pub const SszType: type = ST; | ||
|
|
||
| const Self = @This(); | ||
|
|
||
| const ParentChange = struct { | ||
| map: *std.AutoArrayHashMap(Gindex, void), // Parent's data.changed | ||
| gindex: Gindex, // gindex of current node . If current is changed, this will be inserted to parent's data.changed | ||
| }; | ||
|
|
||
| inline fn markChildChanged(self: *Self, gindex: Gindex) !void { | ||
| try self.data.changed.put(gindex, {}); | ||
| if (self.parent_change) |parent_change| { | ||
| try parent_change.map.put(parent_change.gindex, {}); | ||
| } | ||
| } | ||
|
|
||
| pub fn init(allocator: std.mem.Allocator, pool: *Node.Pool, root: Node.Id) !Self { | ||
| return Self{ | ||
| .allocator = allocator, | ||
| .pool = pool, | ||
| .data = try Data.init(allocator, pool, root), | ||
| .parent_change = null, | ||
| }; | ||
| } | ||
|
|
||
|
|
@@ -144,27 +158,28 @@ pub fn TreeView(comptime ST: type) type { | |
| } else { | ||
| const child_data = try self.getChildData(child_gindex); | ||
|
|
||
| // TODO only update changed if the subview is mutable | ||
| self.data.changed.put(child_gindex, void); | ||
|
|
||
| return TreeView(ST.Element){ | ||
| .allocator = self.allocator, | ||
| .pool = self.pool, | ||
| .data = child_data, | ||
| .parent_change = .{ | ||
| .map = &self.data.changed, | ||
| .gindex = child_gindex, | ||
| }, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| /// Set an element by index. If the element is a basic type, pass the value directly. | ||
| /// If the element is a complex type, pass a TreeView of the corresponding type. | ||
| /// If the element is a composite type, pass a TreeView of the corresponding type. | ||
| /// The caller transfers ownership of the `value` TreeView to this parent view. | ||
| /// The existing TreeView, if any, will be deinited by this function. | ||
| pub fn setElement(self: *Self, index: usize, value: Element) !void { | ||
| if (ST.kind != .vector and ST.kind != .list) { | ||
| @compileError("setElement can only be used with vector or list types"); | ||
| } | ||
| const child_gindex = Gindex.fromDepth(ST.chunk_depth, index); | ||
| try self.data.changed.put(child_gindex, void); | ||
| try self.markChildChanged(child_gindex); | ||
| if (comptime isBasicType(ST.Element)) { | ||
| const child_node = try self.getChildNode(child_gindex); | ||
| try self.data.children_nodes.put( | ||
|
|
@@ -214,19 +229,20 @@ pub fn TreeView(comptime ST: type) type { | |
| } else { | ||
| const child_data = try self.getChildData(child_gindex); | ||
|
|
||
| // TODO only update changed if the subview is mutable | ||
| try self.data.changed.put(child_gindex, {}); | ||
|
|
||
| return TreeView(ChildST){ | ||
| .allocator = self.allocator, | ||
| .pool = self.pool, | ||
| .data = child_data, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to This is a critical issue for correctness, as it prevents modifications to nested data structures from being persisted and committed. For the API to function as expected, |
||
| .parent_change = .{ | ||
| .map = &self.data.changed, | ||
| .gindex = child_gindex, | ||
| }, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| /// Set a field by name. If the field is a basic type, pass the value directly. | ||
| /// If the field is a complex type, pass a TreeView of the corresponding type. | ||
| /// If the field is a composite type, pass a TreeView of the corresponding type. | ||
| /// The caller transfers ownership of the `value` TreeView to this parent view. | ||
| /// The existing TreeView, if any, will be deinited by this function. | ||
| pub fn setField(self: *Self, comptime field_name: []const u8, value: Field(field_name)) !void { | ||
|
|
@@ -236,7 +252,7 @@ pub fn TreeView(comptime ST: type) type { | |
| const field_index = comptime ST.getFieldIndex(field_name); | ||
| const ChildST = ST.getFieldType(field_name); | ||
| const child_gindex = Gindex.fromDepth(ST.chunk_depth, field_index); | ||
| try self.data.changed.put(child_gindex, {}); | ||
| try self.markChildChanged(child_gindex); | ||
| if (comptime isBasicType(ChildST)) { | ||
| const opt_old_node = try self.data.children_nodes.fetchPut( | ||
| child_gindex, | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line copies the
child_datastruct into the newTreeView. This leads to a critical issue: any modifications made to the returned sub-view (e.g., by callingsetElementorsetFieldon it) are applied to this temporary copy and are lost when the view goes out of scope.While the new
parent_changemechanism correctly marks the parent as dirty, the actual data change within the sub-view is not persisted in the parent'schildren_datacache. Consequently, whencommitis called on the parent, the sub-view's changes are not included.To fix this,
TreeViewshould probably hold a pointer toData(data: *Data) instead of a value (data: Data). This would ensure that sub-views modify the sharedDatainstance stored in the parent's cache.