ImmediateSchedulerType.swift 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. //
  2. // ImmediateSchedulerType.swift
  3. // RxSwift
  4. //
  5. // Created by Krunoslav Zaher on 5/31/15.
  6. // Copyright © 2015 Krunoslav Zaher. All rights reserved.
  7. //
  8. /// Represents an object that immediately schedules units of work.
  9. public protocol ImmediateSchedulerType {
  10. /**
  11. Schedules an action to be executed immediately.
  12. - parameter state: State passed to the action to be executed.
  13. - parameter action: Action to be executed.
  14. - returns: The disposable object used to cancel the scheduled action (best effort).
  15. */
  16. func schedule<StateType>(_ state: StateType, action: @escaping (StateType) -> Disposable) -> Disposable
  17. }
  18. extension ImmediateSchedulerType {
  19. /**
  20. Schedules an action to be executed recursively.
  21. - parameter state: State passed to the action to be executed.
  22. - parameter action: Action to execute recursively. The last parameter passed to the action is used to trigger recursive scheduling of the action, passing in recursive invocation state.
  23. - returns: The disposable object used to cancel the scheduled action (best effort).
  24. */
  25. public func scheduleRecursive<State>(_ state: State, action: @escaping (_ state: State, _ recurse: (State) -> Void) -> Void) -> Disposable {
  26. let recursiveScheduler = RecursiveImmediateScheduler(action: action, scheduler: self)
  27. recursiveScheduler.schedule(state)
  28. return Disposables.create(with: recursiveScheduler.dispose)
  29. }
  30. }