0001 // 0002 // SchedulerServices+Emulation.swift 0003 // RxSwift 0004 // 0005 // Created by Krunoslav Zaher on 6/6/15. 0006 // Copyright © 2015 Krunoslav Zaher. All rights reserved. 0007 // 0008 0009 import Foundation 0010 0011 enum SchedulePeriodicRecursiveCommand{ 0012 case Tick
SchedulerServices+Emulation.swift:18 typealias RecursiveScheduler = AnyRecursiveScheduler<SchedulePeriodicRecursiveCommand>SchedulerServices+Emulation.swift:37 return _scheduler.scheduleRecursive(SchedulePeriodicRecursiveCommand.Tick, dueTime: _startAfter, action: self.tick)SchedulerServices+Emulation.swift:40 func tick(command: SchedulePeriodicRecursiveCommand, scheduler: RecursiveScheduler) -> Void {SchedulerServices+Emulation.swift:59 scheduler.schedule(SchedulePeriodicRecursiveCommand.DispatchStart)0013 case DispatchStart
SchedulerServices+Emulation.swift:37 return _scheduler.scheduleRecursive(SchedulePeriodicRecursiveCommand.Tick, dueTime: _startAfter, action: self.tick)SchedulerServices+Emulation.swift:45 case .Tick:SchedulerServices+Emulation.swift:46 scheduler.schedule(.Tick, dueTime: _period)0014 } 0015 0016 class SchedulePeriodicRecursive
SchedulerServices+Emulation.swift:51 self.tick(.DispatchStart, scheduler: scheduler)SchedulerServices+Emulation.swift:54 case .DispatchStart:SchedulerServices+Emulation.swift:59 scheduler.schedule(SchedulePeriodicRecursiveCommand.DispatchStart)<State
SchedulerType.swift:66 let schedule = SchedulePeriodicRecursive(scheduler: self, startAfter: startAfter, period: period, action: action, state: state)> { 0017 typealias RecursiveAction
SchedulerServices+Emulation.swift:17 typealias RecursiveAction = State -> StateSchedulerServices+Emulation.swift:17 typealias RecursiveAction = State -> StateSchedulerServices+Emulation.swift:25 private var _state: StateSchedulerServices+Emulation.swift:28 init(scheduler: SchedulerType, startAfter: RxTimeInterval, period: RxTimeInterval, action: RecursiveAction, state: State) {= State -> State 0018 typealias RecursiveScheduler
SchedulerServices+Emulation.swift:23 private let _action: RecursiveActionSchedulerServices+Emulation.swift:28 init(scheduler: SchedulerType, startAfter: RxTimeInterval, period: RxTimeInterval, action: RecursiveAction, state: State) {= AnyRecursiveScheduler<SchedulePeriodicRecursiveCommand> 0019 0020 private let _scheduler
SchedulerServices+Emulation.swift:40 func tick(command: SchedulePeriodicRecursiveCommand, scheduler: RecursiveScheduler) -> Void {: SchedulerType 0021 private let _startAfter
SchedulerServices+Emulation.swift:29 _scheduler = schedulerSchedulerServices+Emulation.swift:37 return _scheduler.scheduleRecursive(SchedulePeriodicRecursiveCommand.Tick, dueTime: _startAfter, action: self.tick): RxTimeInterval 0022 private let _period
SchedulerServices+Emulation.swift:30 _startAfter = startAfterSchedulerServices+Emulation.swift:37 return _scheduler.scheduleRecursive(SchedulePeriodicRecursiveCommand.Tick, dueTime: _startAfter, action: self.tick): RxTimeInterval 0023 private let _action
SchedulerServices+Emulation.swift:31 _period = periodSchedulerServices+Emulation.swift:46 scheduler.schedule(.Tick, dueTime: _period): RecursiveAction 0024 0025 private var _state
SchedulerServices+Emulation.swift:32 _action = actionSchedulerServices+Emulation.swift:55 _state = _action(_state): State 0026 private var _pendingTickCount
SchedulerServices+Emulation.swift:33 _state = stateSchedulerServices+Emulation.swift:55 _state = _action(_state)SchedulerServices+Emulation.swift:55 _state = _action(_state): AtomicInt = 0 0027 0028 init
SchedulerServices+Emulation.swift:50 if AtomicIncrement(&_pendingTickCount) == 1 {SchedulerServices+Emulation.swift:57 if AtomicDecrement(&_pendingTickCount) > 0 {(scheduler: SchedulerType, startAfter: RxTimeInterval, period: RxTimeInterval, action: RecursiveAction, state: State) { 0029 _scheduler = scheduler 0030 _startAfter = startAfter 0031 _period = period 0032 _action = action 0033 _state = state 0034 } 0035 0036 func start
SchedulerType.swift:66 let schedule = SchedulePeriodicRecursive(scheduler: self, startAfter: startAfter, period: period, action: action, state: state)() -> Disposable { 0037 return _scheduler.scheduleRecursive(SchedulePeriodicRecursiveCommand.Tick, dueTime: _startAfter, action: self.tick) 0038 } 0039 0040 func tick
SchedulerType.swift:68 return schedule.start()(command: SchedulePeriodicRecursiveCommand, scheduler: RecursiveScheduler) -> Void { 0041 // Tries to emulate periodic scheduling as best as possible. 0042 // The problem that could arise is if handling periodic ticks take too long, or 0043 // tick interval is short. 0044 switch command { 0045 case .Tick: 0046 scheduler.schedule(.Tick, dueTime: _period) 0047 0048 // The idea is that if on tick there wasn't any item enqueued, schedule to perform work immediatelly. 0049 // Else work will be scheduled after previous enqueued work completes. 0050 if AtomicIncrement(&_pendingTickCount) == 1 { 0051 self.tick(.DispatchStart, scheduler: scheduler) 0052 } 0053 0054 case .DispatchStart: 0055 _state = _action(_state) 0056 // Start work and schedule check is this last batch of work 0057 if AtomicDecrement(&_pendingTickCount) > 0 { 0058 // This gives priority to scheduler emulation, it's not perfect, but helps 0059 scheduler.schedule(SchedulePeriodicRecursiveCommand.DispatchStart) 0060 } 0061 } 0062 } 0063 } 0064
SchedulerServices+Emulation.swift:37 return _scheduler.scheduleRecursive(SchedulePeriodicRecursiveCommand.Tick, dueTime: _startAfter, action: self.tick)SchedulerServices+Emulation.swift:51 self.tick(.DispatchStart, scheduler: scheduler)