set/add event

This commit is contained in:
John Lancaster
2026-02-22 19:04:00 -06:00
parent 977e87d7f5
commit 2efdf19ee1
2 changed files with 29 additions and 33 deletions

View File

@@ -15,6 +15,7 @@ class HookFunction(Protocol):
class HookedContainer(ABC, Sized, Iterable[T], Container[T]): class HookedContainer(ABC, Sized, Iterable[T], Container[T]):
_data: MutableNesting[T]
_path: MutableSequence[int] _path: MutableSequence[int]
_root: MutableMapping[T] | MutableSequence[T] | None = None _root: MutableMapping[T] | MutableSequence[T] | None = None
hook: HookFunction hook: HookFunction
@@ -22,15 +23,21 @@ class HookedContainer(ABC, Sized, Iterable[T], Container[T]):
def __repr__(self): def __repr__(self):
return f"{self.__class__.__name__}({self._data!r})" return f"{self.__class__.__name__}({self._data!r})"
def __len__(self):
return len(self._data)
def __iter__(self):
return iter(self._data)
def __contains__(self, x):
return x in self._data
# Sequence Methods # Sequence Methods
# __contains__, __iter__, __reversed__, index, and count # __contains__, __iter__, __reversed__, index, and count
@abstractmethod @abstractmethod
def __getitem__(self, key): ... def __getitem__(self, key): ...
def __len__(self):
return len(self._data)
# MutableSequence Methods # MutableSequence Methods
# append, reverse, extend, pop, remove, and __iadd__ # append, reverse, extend, pop, remove, and __iadd__
@@ -44,8 +51,8 @@ class HookedContainer(ABC, Sized, Iterable[T], Container[T]):
if self.hook: if self.hook:
self.hook(e.RemoveItemEvent(self.new_path(s), item)) self.hook(e.RemoveItemEvent(self.new_path(s), item))
@abstractmethod # @abstractmethod
def insert(self, index, value): ... # def insert(self, index, value): ...
# Custom Methods # Custom Methods

View File

@@ -16,52 +16,41 @@ class HookedMapping(HookedContainer[T], MutableMapping[T, MutableNesting[T]]):
hook: HookFunction | None = None, hook: HookFunction | None = None,
path: MutableSequence[int] | None = None, path: MutableSequence[int] | None = None,
): ):
self.hook = hook
match existing: match existing:
case HookedMapping(_data=seq): case HookedMapping(_data=seq):
self._data = seq self._data = seq
case MutableMapping() as seq: case MutableMapping() as seq:
self._data = seq self._data = seq
case _ as it: case _:
self._data = dict(it) raise TypeError(f"Expected a mapping, got {type(existing)}")
# self._data = dict(it)
self._data = existing self._data = existing
self.hook = hook
self._path = list(path) if path is not None else [] self._path = list(path) if path is not None else []
# MutableMapping Methods
def __iter__(self):
return iter(self._data)
def insert(self, key: T, value: MutableNesting[T]) -> None:
self._data[key] = value
if self.hook:
self.hook(e.AddItemEvent(self.new_path(key), value))
# HookedContainer methods
def __getitem__(self, key: T) -> MutableNesting[T]: def __getitem__(self, key: T) -> MutableNesting[T]:
if key not in self._data: if key not in self._data:
self.insert(key, {}) return self.__setitem__(key, {})
return HookedMapping(self._data[key], hook=self.hook, path=self.new_path(key))
value = self._data[key] value = self._data[key]
match value: match value:
case HookedMapping(_data=seq):
return type(self)(seq, hook=self.hook, path=self.new_path(key))
case MutableMapping() as mapping: case MutableMapping() as mapping:
return HookedMapping(mapping, hook=self.hook, path=self.new_path(key)) return HookedMapping(mapping, hook=self.hook, path=self.new_path(key))
case _ as item: case _ as item:
return item return item
def __setitem__(self, key: T, value: MutableNesting[T]) -> None: def __setitem__(self, key: T, value: MutableNesting[T]) -> MutableNesting[T] | None:
if key not in self._data: new_path = self.new_path(key)
self.insert(key, value) event = e.SetItemEvent(new_path, value) if key in self._data else e.AddItemEvent(new_path, value)
else: match value:
case HookedMapping(_data=value):
self._data[key] = value
case _:
self._data[key] = value self._data[key] = value
if self.hook: if self.hook:
self.hook(e.SetItemEvent(self.new_path(key), value)) self.hook(event)
return value
def __delitem__(self, key: T) -> None: def __delitem__(self, key: T) -> None:
item = self._data[key] item = self._data.pop(key)
del self._data[key]
if self.hook: if self.hook:
self.hook(e.RemoveItemEvent(self.new_path(key), item)) self.hook(e.RemoveItemEvent(self.new_path(key), item))