Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract common trigger binding logic #79

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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], None]) -> None:
"""
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
Loading