revert to older effect_of_N style
This commit is contained in:
parent
6d1a6907d2
commit
488d1e8bff
@ -1,36 +1,40 @@
|
|||||||
from __future__ import annotations
|
from gears import Gear
|
||||||
from typing import Any, Callable, cast
|
from typing import Any, Generic, Hashable, TypeVar, Callable, TypeVarTuple
|
||||||
from .wrap_aware_tuple import WrapAwareTuple
|
|
||||||
|
|
||||||
|
|
||||||
class Effect[*Ts]:
|
class Effect:
|
||||||
def __init__(self, fn: Callable[[*Ts], None], gears: WrapAwareTuple[Gear, *Ts]):
|
def __init__(self, fn: Callable[..., None], *gears: Gear):
|
||||||
for gear in gears:
|
|
||||||
gear.effects.append(self)
|
|
||||||
self._fn = fn
|
self._fn = fn
|
||||||
self._gears = gears
|
self._gears = gears
|
||||||
|
for gear in gears:
|
||||||
|
gear.effects.append(self)
|
||||||
|
|
||||||
self.on_change()
|
self.on_change(gears[0]())
|
||||||
|
|
||||||
def on_change(self):
|
def on_change(self, _value: Any):
|
||||||
self._fn(*self._gears.unwrap(lambda x: x()))
|
self._fn(*(gear() for gear in self._gears))
|
||||||
|
|
||||||
|
|
||||||
def effect_of[*Ts](
|
def effect_of[T0: Hashable](g0: Gear[T0]) -> Callable[[Callable[[T0], None]], Effect]:
|
||||||
*gears: Gear[Any],
|
def decorator(fn: Callable[[T0], None]) -> Effect:
|
||||||
) -> Callable[[Callable[[*Ts], None]], Effect]:
|
return Effect(fn, g0)
|
||||||
# No deduction, no tears, only cast now
|
|
||||||
ts_gears = cast(WrapAwareTuple[Gear, *Ts], WrapAwareTuple.prewrapped(gears))
|
|
||||||
return effect_of_typechecked(ts_gears)
|
|
||||||
|
|
||||||
|
|
||||||
def effect_of_typechecked[*Ts](
|
|
||||||
gears: WrapAwareTuple[Gear, *Ts],
|
|
||||||
) -> Callable[[Callable[[*Ts], None]], Effect]:
|
|
||||||
def decorator(fn: Callable[[*Ts], None]) -> Effect:
|
|
||||||
return Effect(fn, gears)
|
|
||||||
|
|
||||||
return decorator
|
return decorator
|
||||||
|
|
||||||
|
|
||||||
from gears import Gear
|
def effect_of_2[T0: Hashable, T1: Hashable](
|
||||||
|
g0: Gear[T0], g1: Gear[T1]
|
||||||
|
) -> Callable[[Callable[[T0, T1], None]], Effect]:
|
||||||
|
def decorator(fn: Callable[[T0, T1], None]) -> Effect:
|
||||||
|
return Effect(fn, g0, g1)
|
||||||
|
|
||||||
|
return decorator
|
||||||
|
|
||||||
|
|
||||||
|
def effect_of_3[T0: Hashable, T1: Hashable, T2: Hashable](
|
||||||
|
g0: Gear[T0], g1: Gear[T1], g2: Gear[T2]
|
||||||
|
) -> Callable[[Callable[[T0, T1, T2], None]], Effect]:
|
||||||
|
def decorator(fn: Callable[[T0, T1, T2], None]) -> Effect:
|
||||||
|
return Effect(fn, g0, g1, g2)
|
||||||
|
|
||||||
|
return decorator
|
||||||
|
@ -1,14 +1,10 @@
|
|||||||
from __future__ import annotations
|
from typing import Any, Generic, Hashable, TypeVar, Callable
|
||||||
from typing import Any, Hashable, TypeVar, TYPE_CHECKING
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
|
||||||
from gears.effect import Effect
|
|
||||||
|
|
||||||
|
|
||||||
class Gear[T: Hashable]:
|
class Gear[T: Hashable]:
|
||||||
def __init__(self, value: T):
|
def __init__(self, value: T):
|
||||||
self._value = value
|
self._value = value
|
||||||
self.effects: list[Effect] = []
|
self.effects = []
|
||||||
|
|
||||||
def get(self) -> T:
|
def get(self) -> T:
|
||||||
return self._value
|
return self._value
|
||||||
@ -21,4 +17,4 @@ class Gear[T: Hashable]:
|
|||||||
return
|
return
|
||||||
self._value = value
|
self._value = value
|
||||||
for effect in self.effects:
|
for effect in self.effects:
|
||||||
effect.on_change()
|
effect.on_change(value)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user