             }
             
             let action = self._lock.calculateLocked { () -> Action? in
-                if isAdded {
-                    self._group.remove(for: removeKey!)
-                }
-                else {
-                    isDone = true
-                }
+                switch scheduleState {
+                case let .added(removeKey):
+                    self._group.remove(for: removeKey)
+                case .initial:
+                    break
+                case .done:
+                    break
+                }
+
+                scheduleState = .done
                 
                 return self._action
             }

            }
            
            let action = self._lock.calculateLocked { () -> Action? in
                if isAdded {
                    self._group.remove(for: removeKey!)
                }
                else {
                    isDone = true
                }
                
                return self._action
            }
            


import Foundation

fileprivate enum ScheduleState {
    case initial
    case added(CompositeDisposable.DisposeKey)
    case done
}

/// Type erased recursive scheduler.
class AnyRecursiveScheduler<State> {
    
    typealias Action =  (State, AnyRecursiveScheduler<State>) -> Void

    private let _lock = NSRecursiveLock()
