passing thru root reference
This commit is contained in:
@@ -39,12 +39,12 @@ class HookedContainer(ABC, Sized, Iterable[T], Container[T]):
|
|||||||
def __setitem__(self, s, value):
|
def __setitem__(self, s, value):
|
||||||
self._data[s] = value
|
self._data[s] = value
|
||||||
if self.hook:
|
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):
|
def __delitem__(self, s):
|
||||||
item = self._data.pop(s)
|
item = self._data.pop(s)
|
||||||
if self.hook:
|
if self.hook:
|
||||||
self.hook(e.RemoveItemEvent(self.new_path(s), item))
|
self.hook(e.RemoveItemEvent(self._root, self.new_path(s), item))
|
||||||
|
|
||||||
# @abstractmethod
|
# @abstractmethod
|
||||||
# def insert(self, index, value): ...
|
# def insert(self, index, value): ...
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ from typing import TypeVar
|
|||||||
|
|
||||||
from . import events as e
|
from . import events as e
|
||||||
from .container import HookedContainer
|
from .container import HookedContainer
|
||||||
from .types import HookFunction
|
from .events import HookFunction
|
||||||
|
from .types import MutableNesting
|
||||||
|
|
||||||
T = TypeVar("T")
|
T = TypeVar("T")
|
||||||
|
|
||||||
@@ -17,6 +18,7 @@ class HookedList(HookedContainer[T], MutableSequence[T]):
|
|||||||
self,
|
self,
|
||||||
existing: MutableSequence[T],
|
existing: MutableSequence[T],
|
||||||
hook: HookFunction | None = None,
|
hook: HookFunction | None = None,
|
||||||
|
root: MutableNesting[T] | None = None,
|
||||||
path: MutableSequence[int] | None = None,
|
path: MutableSequence[int] | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
self.hook = hook
|
self.hook = hook
|
||||||
@@ -27,6 +29,7 @@ class HookedList(HookedContainer[T], MutableSequence[T]):
|
|||||||
self._data = seq
|
self._data = seq
|
||||||
case _ as it:
|
case _ as it:
|
||||||
self._data = list(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 []
|
self._path = list(path) if path is not None else []
|
||||||
|
|
||||||
def __getitem__(self, s):
|
def __getitem__(self, s):
|
||||||
@@ -34,13 +37,13 @@ class HookedList(HookedContainer[T], MutableSequence[T]):
|
|||||||
new_path.append(s)
|
new_path.append(s)
|
||||||
match self._data[s]:
|
match self._data[s]:
|
||||||
case HookedContainer(_data=seq):
|
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:
|
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:
|
case _ as item:
|
||||||
return item
|
return item
|
||||||
|
|
||||||
def insert(self, index, value):
|
def insert(self, index, value):
|
||||||
self._data.insert(index, value)
|
self._data.insert(index, value)
|
||||||
if self.hook:
|
if self.hook:
|
||||||
self.hook(e.AddItemEvent(self.new_path(index), value))
|
self.hook(e.AddItemEvent(self._root, self.new_path(index), value))
|
||||||
|
|||||||
Reference in New Issue
Block a user