This commit is contained in:
John Lancaster
2026-02-21 19:01:03 -06:00
parent 2405d670fb
commit 89f533d9bf
2 changed files with 21 additions and 10 deletions

View File

@@ -0,0 +1,20 @@
from collections.abc import Callable, MutableMapping, MutableSequence, Sequence
from typing import Generic, TypeVar
from . import events as e
T = TypeVar("T")
class HookedMapping(Generic[T], MutableMapping[T, T | MutableSequence[T], MutableMapping[T]]):
_data: MutableSequence[T]
_path: MutableSequence[int]
hook: Callable[[e.ChangeEvent[T]], None] | None
def __init__(self, hook, existing: MutableMapping[T], path: Sequence[int] | None = None):
self.hook = hook
self._data = existing
self._path = list(path) if path is not None else []
def __setitem__(self, key):
return

View File

@@ -1,6 +1,5 @@
from collections.abc import Callable, Iterable, MutableSequence, Sequence from collections.abc import Iterable, MutableSequence, Sequence
from copy import copy from copy import copy
from enum import Enum, auto
from typing import TypeVar from typing import TypeVar
from . import events as e from . import events as e
@@ -9,16 +8,8 @@ from .common import HookedContainer
T = TypeVar("T") T = TypeVar("T")
class ListChange(Enum):
ADD_ITEM = auto()
REMOVE_ITEM = auto()
SET_ITEM = auto()
class HookedList(HookedContainer[T], MutableSequence[T]): class HookedList(HookedContainer[T], MutableSequence[T]):
_data: MutableSequence[T] _data: MutableSequence[T]
_path: MutableSequence[int]
hook: Callable[[e.ChangeEvent[T]], None] | None
def __init__(self, hook, existing: Iterable[T], path: Sequence[int] | None = None): def __init__(self, hook, existing: Iterable[T], path: Sequence[int] | None = None):
self.hook = hook self.hook = hook