diff --git a/src/hooked_containers/container.py b/src/hooked_containers/container.py index afe727c..e80ea15 100644 --- a/src/hooked_containers/container.py +++ b/src/hooked_containers/container.py @@ -16,6 +16,7 @@ class HookFunction(Protocol): class HookedContainer(ABC, Sized, Iterable[T], Container[T]): _path: MutableSequence[int] + _root: MutableMapping[T] | MutableSequence[T] | None = None hook: HookFunction def __repr__(self): @@ -36,12 +37,12 @@ class HookedContainer(ABC, Sized, Iterable[T], Container[T]): def __setitem__(self, s, value): self._data[s] = value if self.hook: - self.hook(e.SetItemEvent(index=s, item=value, path=self._path)) + self.hook(e.SetItemEvent(self.new_path(s), value)) def __delitem__(self, s): item = self._data.pop(s) if self.hook: - self.hook(e.RemoveItemEvent(index=s, item=item, path=self._path)) + self.hook(e.RemoveItemEvent(self.new_path(s), item)) @abstractmethod def insert(self, index, value): ... diff --git a/src/hooked_containers/events.py b/src/hooked_containers/events.py index e4536db..70cc102 100644 --- a/src/hooked_containers/events.py +++ b/src/hooked_containers/events.py @@ -7,9 +7,8 @@ T = TypeVar("T") @dataclass(frozen=True) class ChangeEvent(Generic[T]): - index: int + path: MutableSequence[int] item: T - path: MutableSequence[int] | None = None @dataclass(frozen=True) diff --git a/src/hooked_containers/mapping.py b/src/hooked_containers/mapping.py index 56a708e..dc669c6 100644 --- a/src/hooked_containers/mapping.py +++ b/src/hooked_containers/mapping.py @@ -35,13 +35,14 @@ class HookedMapping(HookedContainer[T], MutableMapping[T, MutableNesting[T]]): def insert(self, key: T, value: MutableNesting[T]) -> None: self._data[key] = value if self.hook: - self.hook(e.AddItemEvent(index=key, item=value, path=self.new_path(key))) + self.hook(e.AddItemEvent(value, path=self.new_path(key))) # HookedContainer methods def __getitem__(self, key: T) -> MutableNesting[T]: if key not in self._data: self.insert(key, {}) + return HookedMapping(self._data[key], hook=self.hook, path=self.new_path(key)) value = self._data[key] match value: case HookedMapping(_data=seq): @@ -54,13 +55,13 @@ class HookedMapping(HookedContainer[T], MutableMapping[T, MutableNesting[T]]): def __setitem__(self, key: T, value: MutableNesting[T]) -> None: if key not in self._data: self.insert(key, value) - return - self._data[key] = value - if self.hook: - self.hook(e.SetItemEvent(index=key, item=value, path=self.new_path(key))) + else: + self._data[key] = value + if self.hook: + self.hook(e.SetItemEvent(value, path=self.new_path(key))) def __delitem__(self, key: T) -> None: item = self._data[key] del self._data[key] if self.hook: - self.hook(e.RemoveItemEvent(index=key, item=item, path=self.new_path(key))) + self.hook(e.RemoveItemEvent(item, path=self.new_path(key))) diff --git a/src/hooked_containers/sequence.py b/src/hooked_containers/sequence.py index 2c854f5..bd57a6c 100644 --- a/src/hooked_containers/sequence.py +++ b/src/hooked_containers/sequence.py @@ -42,4 +42,4 @@ class HookedList(HookedContainer[T], MutableSequence[T]): def insert(self, index, value): self._data.insert(index, value) if self.hook: - self.hook(e.AddItemEvent(index=index, item=value, path=self._path)) + self.hook(e.AddItemEvent(self.new_path(index), value))