# Path: rx/disposables/disposable.py
# class Disposable(object):
#     """Main disposable class"""
#     def __init__(self, action):
#         """Creates a disposable object that invokes the specified action when 
#         disposed.
#         
#         Keyword arguments:
#         dispose -- Action to run during the first call to Disposable.dispose. 
#             The action is guaranteed to be run at most once.
#         
#         Returns the disposable object that runs the given action upon disposal.
#         """
#         self.is_disposed = False
#         self.action = action
# 
#     def dispose(self):
#         """Performs the task of cleaning up resources."""
# 
#         if not self.is_disposed:
#             self.action()
#             self.is_disposed = True
# 
#     @classmethod
#     def empty(cls):
#         return cls(noop)
# 
# Path: rx/disposables/compositedisposable.py
# class CompositeDisposable(Disposable):
#     def __init__(self, *args):
#         if args and isinstance(args[0], list):
#             self.disposables = args[0]
#         else:
#             self.disposables = list(args)
#         self.is_disposed = False
#         self.length = len(self.disposables)
# 
#     def add(self, item):
#         if self.is_disposed:
#             item.dispose()
#         else:
#             self.disposables.append(item)
#             self.length += 1
# 
#     def remove(self, item):
#         should_dispose = False
#         if not self.is_disposed and item in self.disposables:
#             self.disposables.remove(item)
#             should_dispose = True
#             self.length -= 1
#             item.dispose()
# 
#         return should_dispose
# 
#     def dispose(self):
#         if self.is_disposed:
#             return
#         
#         self.is_disposed = True
#         current_disposables = self.disposables[:]
#         self.disposables = []
#         self.length = 0
# 
#         for disposable in current_disposables:
#             disposable.dispose()
#             
#     def clear(self):
#         current_disposables = self.disposables[:]
#         self.disposables = []
#         self.length = 0
#         
#         for disposable in current_disposables:
#             disposable.dispose()
#             
#     def contains(self, item):
#         return item in self.disposables
# 
#     def to_array(self):
#         return self.disposables[:]
# 
#     def __len__(self):
#         return len(self.disposables)


from threading import Timer
from datetime import datetime, timedelta
from rx.disposables import Disposable, CompositeDisposable
        timer.start()
        
        def dispose():
            timer.cancel()

        return Disposable(dispose)

    def invoke_rec_immediate(self, scheduler, pair):
        #print "invoke_rec_immediate", scheduler, pair
        state = pair.get('state')
        action = pair.get('action')
        group = CompositeDisposable()
        
        def recursive_action(state1):
            # FIXME: need a better name for this function
            def action2(state2=None):
                #print "action2", state2
                is_added = False
                is_done = False
                
                def action(scheduler, state=None):
                    nonlocal is_done

                    #print "action", scheduler1, state3
                    if is_added:
                        group.remove(d)
                    else:
                        is_done = True
                    
                    recursive_action(state)

# Path: rx/disposables/disposable.py
# class Disposable(object):
#     """Main disposable class"""
#     def __init__(self, action):
#         """Creates a disposable object that invokes the specified action when 
#         disposed.
#         
#         Keyword arguments:
#         dispose -- Action to run during the first call to Disposable.dispose. 
#             The action is guaranteed to be run at most once.
#         
#         Returns the disposable object that runs the given action upon disposal.
#         """
#         self.is_disposed = False
#         self.action = action
# 
#     def dispose(self):
#         """Performs the task of cleaning up resources."""
# 
#         if not self.is_disposed:
#             self.action()
#             self.is_disposed = True
# 
#     @classmethod
#     def empty(cls):
#         return cls(noop)
# 
# Path: rx/disposables/compositedisposable.py
# class CompositeDisposable(Disposable):
#     def __init__(self, *args):
#         if args and isinstance(args[0], list):
#             self.disposables = args[0]
#         else:
#             self.disposables = list(args)
#         self.is_disposed = False
#         self.length = len(self.disposables)
# 
#     def add(self, item):
#         if self.is_disposed:
#             item.dispose()
#         else:
#             self.disposables.append(item)
#             self.length += 1
# 
#     def remove(self, item):
#         should_dispose = False
#         if not self.is_disposed and item in self.disposables:
#             self.disposables.remove(item)
#             should_dispose = True
#             self.length -= 1
#             item.dispose()
# 
#         return should_dispose
# 
#     def dispose(self):
#         if self.is_disposed:
#             return
#         
#         self.is_disposed = True
#         current_disposables = self.disposables[:]
#         self.disposables = []
#         self.length = 0
# 
#         for disposable in current_disposables:
#             disposable.dispose()
#             
#     def clear(self):
#         current_disposables = self.disposables[:]
#         self.disposables = []
#         self.length = 0
#         
#         for disposable in current_disposables:
#             disposable.dispose()
#             
#     def contains(self, item):
#         return item in self.disposables
# 
#     def to_array(self):
#         return self.disposables[:]
# 
#     def __len__(self):
#         return len(self.disposables)


# Path: rx/concurrency/scheduler.py
from threading import Timer
from datetime import datetime, timedelta
from rx.disposables import Disposable, CompositeDisposable

        timer.start()
        
        def dispose():
            timer.cancel()

        return Disposable(dispose)

    def invoke_rec_immediate(self, scheduler, pair):
        #print "invoke_rec_immediate", scheduler, pair
        state = pair.get('state')
        action = pair.get('action')
        group = CompositeDisposable()
        
        def recursive_action(state1):
            # FIXME: need a better name for this function
            def action2(state2=None):
                #print "action2", state2
                is_added = False
                is_done = False
                
                def action(scheduler, state=None):
                    nonlocal is_done

                    #print "action", scheduler1, state3
                    if is_added:
                        group.remove(d)
                    else:
                        is_done = True
                    
                    recursive_action(state)

                    return Disposable.empty()