]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blobdiff - helpers/action.py
Use machine for key handling
[perso/Immae/Projets/Python/MusicSampler.git] / helpers / action.py
index a6c48e98d5140ca3460fe7103472f8cffbd9968a..010a6cafb1f93db7442e22cc8999d44aec54bb94 100644 (file)
@@ -37,24 +37,23 @@ class Action:
         {
             'trigger': 'fail',
             'source': 'loading',
-            'dest': 'failed'
+            'dest': 'failed',
+            'after': 'poll_loaded'
         },
         {
             'trigger': 'success',
             'source': 'loading',
-            'dest': 'loaded'
+            'dest': 'loaded',
+            'after': 'poll_loaded'
         },
         {
             'trigger': 'run',
             'source': 'loaded',
             'dest': 'loaded_running',
-            'after': 'finish_action'
-        },
-        {
-            'trigger': 'interrupt',
-            'source': 'loaded_running',
-            'dest': 'loaded',
-            'before': 'trigger_interrupt'
+            'after': 'finish_action',
+            # if a child has no transitions, then it is bubbled to the parent,
+            # and we don't want that. Not useful in that machine precisely.
+            'conditions': ['is_loaded']
         },
         {
             'trigger': 'finish_action',
@@ -74,12 +73,11 @@ class Action:
         self.arguments = kwargs
         self.sleep_event = None
         self.waiting_music = None
-        self.load()
 
-    def ready(self):
-        return self.is_loaded(allow_substates=True)
+    def is_loaded_or_failed(self):
+        return self.is_loaded(allow_substates=True) or self.is_failed()
 
-    def callback_loaded(self, success):
+    def callback_music_loaded(self, success):
         if success:
             self.success()
         else:
@@ -89,19 +87,24 @@ class Action:
     def on_enter_loading(self):
         if self.action in self.ACTION_TYPES:
             if 'music' in self.arguments:
-                self.arguments['music'].subscribe_loaded(self.callback_loaded)
+                self.arguments['music'].subscribe_loaded(self.callback_music_loaded)
             else:
                 self.success()
         else:
             error_print("Unknown action {}".format(self.action))
             self.fail()
 
-
     def on_enter_loaded_running(self):
         debug_print(self.description())
         getattr(self, self.action)(**self.arguments)
 
-    def trigger_interrupt(self):
+    def poll_loaded(self):
+        self.key.callback_action_ready(self,
+                self.is_loaded(allow_substates=True))
+
+    # This one cannot be in the Machine state since it would be queued to run
+    # *after* the wait is ended...
+    def interrupt(self):
         if getattr(self, self.action + "_interrupt", None):
             return getattr(self, self.action + "_interrupt")(**self.arguments)
 
@@ -191,11 +194,12 @@ class Action:
             self.mapping.add_wait_id(set_wait_id, self)
 
         self.sleep_event = threading.Event()
+        self.sleep_event_timer = threading.Timer(duration, self.sleep_event.set)
 
         if music is not None:
             music.wait_end()
 
-        threading.Timer(duration, self.sleep_event.set).start()
+        self.sleep_event_timer.start()
         self.sleep_event.wait()
 
     # Action messages
@@ -326,6 +330,7 @@ class Action:
     def wait_interrupt(self, duration=0, music=None, **kwargs):
         if self.sleep_event is not None:
             self.sleep_event.set()
+            self.sleep_event_timer.cancel()
         if music is not None:
             music.wait_event.set()