passing thru root reference

This commit is contained in:
John Lancaster
2026-02-22 21:47:11 -06:00
parent 66b4c1274f
commit 0567766b6c
2 changed files with 9 additions and 6 deletions

View File

@@ -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): ...

View File

@@ -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))