@@ -84,6 +84,21 @@ def init_condition(condition: Callable[[], bool]):
8484"""
8585 )
8686
87+ def _add_binding (self , body : Callable [bool , bool , NoneType ]) -> None :
88+ """
89+ Adds a binding to the EventLoop.
90+
91+ :param body: The body of the binding to add.
92+ """
93+
94+ state = SimpleNamespace (previous = self ._condition ())
95+
96+ @self ._loop .bind
97+ def _ ():
98+ current = self ._condition ()
99+ body (state .previous , current )
100+ state .previous = current
101+
87102 def onTrue (self , command : Command ) -> Self :
88103 """
89104 Starts the given command whenever the condition changes from `False` to `True`.
@@ -92,14 +107,10 @@ def onTrue(self, command: Command) -> Self:
92107 :returns: this trigger, so calls can be chained
93108 """
94109
95- state = SimpleNamespace (pressed_last = self ._condition ())
96-
97- @self ._loop .bind
98- def _ ():
99- pressed = self ._condition ()
100- if not state .pressed_last and pressed :
110+ @self ._add_binding
111+ def _ (previous , current ):
112+ if not previous and current :
101113 command .schedule ()
102- state .pressed_last = pressed
103114
104115 return self
105116
@@ -111,14 +122,10 @@ def onFalse(self, command: Command) -> Self:
111122 :returns: this trigger, so calls can be chained
112123 """
113124
114- state = SimpleNamespace (pressed_last = self ._condition ())
115-
116- @self ._loop .bind
117- def _ ():
118- pressed = self ._condition ()
119- if state .pressed_last and not pressed :
125+ @self ._add_binding
126+ def _ (previous , current ):
127+ if previous and not current :
120128 command .schedule ()
121- state .pressed_last = pressed
122129
123130 return self
124131
@@ -130,17 +137,11 @@ def onChange(self, command: Command) -> Self:
130137 :returns: this trigger, so calls can be chained
131138 """
132139
133- state = SimpleNamespace (pressed_last = self ._condition ())
134-
135- @self ._loop .bind
136- def _ ():
137- pressed = self ._condition ()
138-
139- if state .pressed_last != pressed :
140+ @self ._add_binding
141+ def _ (previous , current ):
142+ if previous != current :
140143 command .schedule ()
141144
142- state .pressed_last = pressed
143-
144145 return self
145146
146147 def whileTrue (self , command : Command ) -> Self :
@@ -155,16 +156,12 @@ def whileTrue(self, command: Command) -> Self:
155156 :returns: this trigger, so calls can be chained
156157 """
157158
158- state = SimpleNamespace (pressed_last = self ._condition ())
159-
160- @self ._loop .bind
161- def _ ():
162- pressed = self ._condition ()
163- if not state .pressed_last and pressed :
159+ @self ._add_binding
160+ def _ (previous , current ):
161+ if not previous and current :
164162 command .schedule ()
165- elif state . pressed_last and not pressed :
163+ elif previous and not current :
166164 command .cancel ()
167- state .pressed_last = pressed
168165
169166 return self
170167
@@ -180,16 +177,12 @@ def whileFalse(self, command: Command) -> Self:
180177 :returns: this trigger, so calls can be chained
181178 """
182179
183- state = SimpleNamespace (pressed_last = self ._condition ())
184-
185- @self ._loop .bind
186- def _ ():
187- pressed = self ._condition ()
188- if state .pressed_last and not pressed :
180+ @self ._add_binding
181+ def _ (previous , current ):
182+ if previous and not current :
189183 command .schedule ()
190- elif not state . pressed_last and pressed :
184+ elif not previous and current :
191185 command .cancel ()
192- state .pressed_last = pressed
193186
194187 return self
195188
@@ -201,17 +194,13 @@ def toggleOnTrue(self, command: Command) -> Self:
201194 :returns: this trigger, so calls can be chained
202195 """
203196
204- state = SimpleNamespace (pressed_last = self ._condition ())
205-
206- @self ._loop .bind
207- def _ ():
208- pressed = self ._condition ()
209- if not state .pressed_last and pressed :
197+ @self ._add_binding
198+ def _ (previous , current ):
199+ if not previous and current :
210200 if command .isScheduled ():
211201 command .cancel ()
212202 else :
213203 command .schedule ()
214- state .pressed_last = pressed
215204
216205 return self
217206
@@ -223,17 +212,13 @@ def toggleOnFalse(self, command: Command) -> Self:
223212 :returns: this trigger, so calls can be chained
224213 """
225214
226- state = SimpleNamespace (pressed_last = self ._condition ())
227-
228- @self ._loop .bind
229- def _ ():
230- pressed = self ._condition ()
231- if state .pressed_last and not pressed :
215+ @self ._add_binding
216+ def _ (previous , current ):
217+ if previous and not current :
232218 if command .isScheduled ():
233219 command .cancel ()
234220 else :
235221 command .schedule ()
236- state .pressed_last = pressed
237222
238223 return self
239224
0 commit comments