Skip to content

Commit

Permalink
Extract common trigger binding logic
Browse files Browse the repository at this point in the history
Also use previous and current
  • Loading branch information
KangarooKoala committed Jan 3, 2025
1 parent 23a4f87 commit ecc63bd
Showing 1 changed file with 38 additions and 53 deletions.
91 changes: 38 additions & 53 deletions commands2/button/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ def init_condition(condition: Callable[[], bool]):
"""
)

def _add_binding(self, body: Callable[bool, bool, NoneType]) -> None:

Check failure on line 87 in commands2/button/trigger.py

View workflow job for this annotation

GitHub Actions / check-mypy

Please use "Callable[[<parameters>], <return type>]" or "Callable"

Check failure on line 87 in commands2/button/trigger.py

View workflow job for this annotation

GitHub Actions / check-mypy

Name "NoneType" is not defined
"""
Adds a binding to the EventLoop.
:param body: The body of the binding to add.
"""

state = SimpleNamespace(previous=self._condition())

@self._loop.bind
def _():
current = self._condition()
body(state.previous, current)
state.previous = current

def onTrue(self, command: Command) -> Self:
"""
Starts the given command whenever the condition changes from `False` to `True`.
Expand All @@ -92,14 +107,10 @@ def onTrue(self, command: Command) -> Self:
:returns: this trigger, so calls can be chained
"""

state = SimpleNamespace(pressed_last=self._condition())

@self._loop.bind
def _():
pressed = self._condition()
if not state.pressed_last and pressed:
@self._add_binding
def _(previous, current):
if not previous and current:
command.schedule()
state.pressed_last = pressed

return self

Expand All @@ -111,14 +122,10 @@ def onFalse(self, command: Command) -> Self:
:returns: this trigger, so calls can be chained
"""

state = SimpleNamespace(pressed_last=self._condition())

@self._loop.bind
def _():
pressed = self._condition()
if state.pressed_last and not pressed:
@self._add_binding
def _(previous, current):
if previous and not current:
command.schedule()
state.pressed_last = pressed

return self

Expand All @@ -130,17 +137,11 @@ def onChange(self, command: Command) -> Self:
:returns: this trigger, so calls can be chained
"""

state = SimpleNamespace(pressed_last=self._condition())

@self._loop.bind
def _():
pressed = self._condition()

if state.pressed_last != pressed:
@self._add_binding
def _(previous, current):
if previous != current:
command.schedule()

state.pressed_last = pressed

return self

def whileTrue(self, command: Command) -> Self:
Expand All @@ -155,16 +156,12 @@ def whileTrue(self, command: Command) -> Self:
:returns: this trigger, so calls can be chained
"""

state = SimpleNamespace(pressed_last=self._condition())

@self._loop.bind
def _():
pressed = self._condition()
if not state.pressed_last and pressed:
@self._add_binding
def _(previous, current):
if not previous and current:
command.schedule()
elif state.pressed_last and not pressed:
elif previous and not current:
command.cancel()
state.pressed_last = pressed

return self

Expand All @@ -180,16 +177,12 @@ def whileFalse(self, command: Command) -> Self:
:returns: this trigger, so calls can be chained
"""

state = SimpleNamespace(pressed_last=self._condition())

@self._loop.bind
def _():
pressed = self._condition()
if state.pressed_last and not pressed:
@self._add_binding
def _(previous, current):
if previous and not current:
command.schedule()
elif not state.pressed_last and pressed:
elif not previous and current:
command.cancel()
state.pressed_last = pressed

return self

Expand All @@ -201,17 +194,13 @@ def toggleOnTrue(self, command: Command) -> Self:
:returns: this trigger, so calls can be chained
"""

state = SimpleNamespace(pressed_last=self._condition())

@self._loop.bind
def _():
pressed = self._condition()
if not state.pressed_last and pressed:
@self._add_binding
def _(previous, current):
if not previous and current:
if command.isScheduled():
command.cancel()
else:
command.schedule()
state.pressed_last = pressed

return self

Expand All @@ -223,17 +212,13 @@ def toggleOnFalse(self, command: Command) -> Self:
:returns: this trigger, so calls can be chained
"""

state = SimpleNamespace(pressed_last=self._condition())

@self._loop.bind
def _():
pressed = self._condition()
if state.pressed_last and not pressed:
@self._add_binding
def _(previous, current):
if previous and not current:
if command.isScheduled():
command.cancel()
else:
command.schedule()
state.pressed_last = pressed

return self

Expand Down

0 comments on commit ecc63bd

Please sign in to comment.