0001 // 0002 // ConcurrentMainScheduler.swift 0003 // Rx 0004 // 0005 // Created by Krunoslav Zaher on 10/17/15. 0006 // Copyright © 2015 Krunoslav Zaher. All rights reserved. 0007 // 0008 0009 import Foundation 0010 0011 /** 0012 Abstracts work that needs to be performed on `MainThread`. In case `schedule` methods are called from main thread, it will perform action immediately without scheduling. 0013 0014 This scheduler is optimized for `subscribeOn` operator. If you want to observe observable sequence elements on main thread using `observeOn` operator, 0015 `MainScheduler` is more suitable for that purpose. 0016 */ 0017 public final class ConcurrentMainScheduler: SchedulerType { 0018 public typealias TimeInterval
ConcurrentMainScheduler.swift:41 public static let instance = ConcurrentMainScheduler(mainScheduler: MainScheduler.instance)= NSTimeInterval 0019 public typealias Time = NSDate 0020 0021 private let _mainScheduler
ConcurrentMainScheduler.swift:89 public func schedulePeriodic<StateType>(state: StateType, startAfter: TimeInterval, period: TimeInterval, action: (StateType) -> StateType) -> Disposable {ConcurrentMainScheduler.swift:89 public func schedulePeriodic<StateType>(state: StateType, startAfter: TimeInterval, period: TimeInterval, action: (StateType) -> StateType) -> Disposable {: MainScheduler 0022 private let _mainQueue
ConcurrentMainScheduler.swift:29 return _mainScheduler.nowConcurrentMainScheduler.swift:35 _mainScheduler = mainSchedulerConcurrentMainScheduler.swift:77 return _mainScheduler.scheduleRelative(state, dueTime: dueTime, action: action)ConcurrentMainScheduler.swift:90 return _mainScheduler.schedulePeriodic(state, startAfter: startAfter, period: period, action: action): dispatch_queue_t 0023 0024 /** 0025 - returns: Current time. 0026 */ 0027 public var now : NSDate { 0028 get { 0029 return _mainScheduler.now 0030 } 0031 } 0032 0033 private init
ConcurrentMainScheduler.swift:34 _mainQueue = dispatch_get_main_queue()ConcurrentMainScheduler.swift:57 dispatch_async(_mainQueue) {(mainScheduler: MainScheduler) { 0034 _mainQueue = dispatch_get_main_queue() 0035 _mainScheduler = mainScheduler 0036 } 0037 0038 /** 0039 Singleton instance of `ConcurrentMainScheduler` 0040 */ 0041 public static let instance = ConcurrentMainScheduler(mainScheduler: MainScheduler.instance) 0042 0043 /** 0044 Schedules an action to be executed immediatelly. 0045 0046 - parameter state: State passed to the action to be executed. 0047 - parameter action: Action to be executed. 0048 - returns: The disposable object used to cancel the scheduled action (best effort). 0049 */ 0050 public func schedule<StateType>(state: StateType, action: (StateType) -> Disposable) -> Disposable { 0051 if NSThread.currentThread().isMainThread { 0052 return action(state) 0053 } 0054 0055 let cancel = SingleAssignmentDisposable() 0056 0057 dispatch_async(_mainQueue) { 0058 if cancel.disposed { 0059 return 0060 } 0061 0062 cancel.disposable = action(state) 0063 } 0064 0065 return cancel 0066 } 0067 0068 /** 0069 Schedules an action to be executed. 0070 0071 - parameter state: State passed to the action to be executed. 0072 - parameter dueTime: Relative time after which to execute the action. 0073 - parameter action: Action to be executed. 0074 - returns: The disposable object used to cancel the scheduled action (best effort). 0075 */ 0076 public final func scheduleRelative<StateType>(state: StateType, dueTime: NSTimeInterval, action: (StateType) -> Disposable) -> Disposable { 0077 return _mainScheduler.scheduleRelative(state, dueTime: dueTime, action: action) 0078 } 0079 0080 /** 0081 Schedules a periodic piece of work. 0082 0083 - parameter state: State passed to the action to be executed. 0084 - parameter startAfter: Period after which initial work should be run. 0085 - parameter period: Period for running the work periodically. 0086 - parameter action: Action to be executed. 0087 - returns: The disposable object used to cancel the scheduled action (best effort). 0088 */ 0089 public func schedulePeriodic<StateType>(state: StateType, startAfter: TimeInterval, period: TimeInterval, action: (StateType) -> StateType) -> Disposable { 0090 return _mainScheduler.schedulePeriodic(state, startAfter: startAfter, period: period, action: action) 0091 } 0092 }
ConcurrentMainScheduler.swift:41 public static let instance = ConcurrentMainScheduler(mainScheduler: MainScheduler.instance)