Skip to content
This repository has been archived by the owner on Feb 19, 2020. It is now read-only.

Commit

Permalink
Exit transition immediately if already in the desired state.
Browse files Browse the repository at this point in the history
  • Loading branch information
legastero committed Aug 10, 2012
1 parent 4e12e22 commit d11a677
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions sleekxmpp/thirdparty/statemachine.py
Expand Up @@ -29,7 +29,7 @@ def addStates(self, states):
if state in self.__states:
raise IndexError("The state '%s' is already in the StateMachine." % state)
self.__states.append(state)
finally:
finally:
self.lock.release()


Expand Down Expand Up @@ -90,6 +90,9 @@ def transition_any(self, from_states, to_state, wait=0.0, func=None, args=[], kw
log.debug("Could not acquire lock")
return False

if self.__current_state == to_state:
return True

while not self.__current_state in from_states:
# detect timeout:
remainder = start + wait - time.time()
Expand All @@ -108,7 +111,7 @@ def transition_any(self, from_states, to_state, wait=0.0, func=None, args=[], kw

# some 'false' value returned from func,
# indicating that transition should not occur:
if not return_val:
if not return_val:
return return_val

log.debug(' ==== TRANSITION %s -> %s', self.__current_state, to_state)
Expand Down Expand Up @@ -193,9 +196,9 @@ def ensure_any(self, states, wait=0.0, block_on_transition=False):
while not self.__current_state in states:
# detect timeout:
remainder = start + wait - time.time()
if remainder > 0:
if remainder > 0:
self.lock.wait(remainder)
else:
else:
self.lock.release()
return False
self.lock.release()
Expand Down Expand Up @@ -241,7 +244,7 @@ def __enter__(self):
while not self.state_machine[self.from_state] or not self.state_machine.lock.acquire(False):
# detect timeout:
remainder = start + self.wait - time.time()
if remainder > 0:
if remainder > 0:
self.state_machine.lock.wait(remainder)
else:
log.debug('StateMachine timeout while waiting for state: %s', self.from_state)
Expand Down

0 comments on commit d11a677

Please sign in to comment.