SchedulerType.swift 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //
  2. // SchedulerType.swift
  3. // RxSwift
  4. //
  5. // Created by Krunoslav Zaher on 2/8/15.
  6. // Copyright © 2015 Krunoslav Zaher. All rights reserved.
  7. //
  8. import Dispatch
  9. import Foundation
  10. // Type that represents time interval in the context of RxSwift.
  11. public typealias RxTimeInterval = DispatchTimeInterval
  12. /// Type that represents absolute time in the context of RxSwift.
  13. public typealias RxTime = Date
  14. /// Represents an object that schedules units of work.
  15. public protocol SchedulerType: ImmediateSchedulerType {
  16. /// - returns: Current time.
  17. var now : RxTime {
  18. get
  19. }
  20. /**
  21. Schedules an action to be executed.
  22. - parameter state: State passed to the action to be executed.
  23. - parameter dueTime: Relative time after which to execute the action.
  24. - parameter action: Action to be executed.
  25. - returns: The disposable object used to cancel the scheduled action (best effort).
  26. */
  27. func scheduleRelative<StateType>(_ state: StateType, dueTime: RxTimeInterval, action: @escaping (StateType) -> Disposable) -> Disposable
  28. /**
  29. Schedules a periodic piece of work.
  30. - parameter state: State passed to the action to be executed.
  31. - parameter startAfter: Period after which initial work should be run.
  32. - parameter period: Period for running the work periodically.
  33. - parameter action: Action to be executed.
  34. - returns: The disposable object used to cancel the scheduled action (best effort).
  35. */
  36. func schedulePeriodic<StateType>(_ state: StateType, startAfter: RxTimeInterval, period: RxTimeInterval, action: @escaping (StateType) -> StateType) -> Disposable
  37. }
  38. extension SchedulerType {
  39. /**
  40. Periodic task will be emulated using recursive scheduling.
  41. - parameter state: Initial state passed to the action upon the first iteration.
  42. - parameter startAfter: Period after which initial work should be run.
  43. - parameter period: Period for running the work periodically.
  44. - returns: The disposable object used to cancel the scheduled recurring action (best effort).
  45. */
  46. public func schedulePeriodic<StateType>(_ state: StateType, startAfter: RxTimeInterval, period: RxTimeInterval, action: @escaping (StateType) -> StateType) -> Disposable {
  47. let schedule = SchedulePeriodicRecursive(scheduler: self, startAfter: startAfter, period: period, action: action, state: state)
  48. return schedule.start()
  49. }
  50. func scheduleRecursive<State>(_ state: State, dueTime: RxTimeInterval, action: @escaping (State, AnyRecursiveScheduler<State>) -> Void) -> Disposable {
  51. let scheduler = AnyRecursiveScheduler(scheduler: self, action: action)
  52. scheduler.schedule(state, dueTime: dueTime)
  53. return Disposables.create(with: scheduler.dispose)
  54. }
  55. }