Skip to content

Commit

Permalink
Add ability to write entity to storage when inserted/removed.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmeyer committed Nov 26, 2024
1 parent c6ea9ce commit 364796d
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions nion/swift/model/Schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1119,9 +1119,6 @@ def _set_field_value(self, name: str, value: typing.Any) -> None:
else:
raise AttributeError()

def _field_value_changed(self, name: str, value: typing.Any) -> None:
pass

def _get_array_item(self, name: str, index: int) -> typing.Any:
array_field = typing.cast(typing.Optional[ArrayField], self.__get_field(name))
if array_field:
Expand All @@ -1142,6 +1139,8 @@ def _insert_item(self, name: str, index: int, item: ItemProxyEntity) -> None:
entity = typing.cast(Entity, item)
entity._set_entity_parent(EntityParent(self, relationship_name=name))
array_field.insert_value(self, index, item) # passing self for container
self._set_modified(DateTime.utcnow())
self._item_inserted(name, index, item)
self.item_inserted_event.fire(name, item, index)
else:
raise AttributeError()
Expand All @@ -1156,10 +1155,23 @@ def _remove_item(self, name: str, item: ItemProxyEntity) -> None:
array_field.remove_value_at_index(index) # passing self for container
entity = typing.cast(Entity, item)
entity._set_entity_parent(None)
self._set_modified(DateTime.utcnow())
self._item_removed(name, index, item)
self.item_removed_event.fire(name, item, index)
else:
raise AttributeError()

# these methods can be overridden to provide custom behavior for storage and notification

def _field_value_changed(self, name: str, value: typing.Any) -> None:
pass

def _item_inserted(self, name: str, index: int, item: ItemProxyEntity) -> None:
pass

def _item_removed(self, name: str, index: int, item: ItemProxyEntity) -> None:
pass

# compatibility functions for persistent object

def begin_reading(self) -> None:
Expand Down

0 comments on commit 364796d

Please sign in to comment.