diff --git a/src/hooked_containers/container.py b/src/hooked_containers/container.py index 9d37f55..d25cf7f 100644 --- a/src/hooked_containers/container.py +++ b/src/hooked_containers/container.py @@ -39,12 +39,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(self.new_path(s), value)) + self.hook(e.SetItemEvent(self._root, self.new_path(s), value)) def __delitem__(self, s): item = self._data.pop(s) if self.hook: - self.hook(e.RemoveItemEvent(self.new_path(s), item)) + self.hook(e.RemoveItemEvent(self._root, self.new_path(s), item)) # @abstractmethod # def insert(self, index, value): ... diff --git a/src/hooked_containers/sequence.py b/src/hooked_containers/sequence.py index 3c17cc9..74c994f 100644 --- a/src/hooked_containers/sequence.py +++ b/src/hooked_containers/sequence.py @@ -4,7 +4,8 @@ from typing import TypeVar from . import events as e from .container import HookedContainer -from .types import HookFunction +from .events import HookFunction +from .types import MutableNesting T = TypeVar("T") @@ -17,6 +18,7 @@ class HookedList(HookedContainer[T], MutableSequence[T]): self, existing: MutableSequence[T], hook: HookFunction | None = None, + root: MutableNesting[T] | None = None, path: MutableSequence[int] | None = None, ) -> None: self.hook = hook @@ -27,6 +29,7 @@ class HookedList(HookedContainer[T], MutableSequence[T]): self._data = seq case _ as it: self._data = list(it) + self._root = root if root is not None else self self._path = list(path) if path is not None else [] def __getitem__(self, s): @@ -34,13 +37,13 @@ class HookedList(HookedContainer[T], MutableSequence[T]): new_path.append(s) match self._data[s]: case HookedContainer(_data=seq): - return type(self)(seq, self.hook, path=new_path) + return type(self)(seq, self.hook, root=self._root, path=new_path) case MutableSequence() as seq: - return HookedList(seq, self.hook, path=new_path) + return HookedList(seq, self.hook, root=self._root, path=new_path) case _ as item: return item def insert(self, index, value): self._data.insert(index, value) if self.hook: - self.hook(e.AddItemEvent(self.new_path(index), value)) + self.hook(e.AddItemEvent(self._root, self.new_path(index), value))