0001 // 0002 // CurrentThreadScheduler.swift 0003 // Rx 0004 // 0005 // Created by Krunoslav Zaher on 8/30/15. 0006 // Copyright © 2015 Krunoslav Zaher. All rights reserved. 0007 // 0008 0009 import Foundation 0010 0011 #if os(Linux) 0012 let CurrentThreadSchedulerKeyInstance = "RxSwift.CurrentThreadScheduler.SchedulerKey" 0013 let CurrentThreadSchedulerQueueKeyInstance = "RxSwift.CurrentThreadScheduler.Queue" 0014 0015 typealias CurrentThreadSchedulerValue = NSString 0016 let CurrentThreadSchedulerValueInstance = "RxSwift.CurrentThreadScheduler.SchedulerKey" as NSString 0017 #else 0018 let CurrentThreadSchedulerKeyInstance= CurrentThreadSchedulerKey() 0019 let CurrentThreadSchedulerQueueKeyInstance
CurrentThreadScheduler.swift:22 let CurrentThreadSchedulerValueInstance = CurrentThreadSchedulerKeyInstanceCurrentThreadScheduler.swift:26 return object === CurrentThreadSchedulerKeyInstanceCurrentThreadScheduler.swift:32 return CurrentThreadSchedulerKeyInstanceCurrentThreadScheduler.swift:36 return CurrentThreadSchedulerKeyInstanceCurrentThreadScheduler.swift:86 let value: CurrentThreadSchedulerValue? = NSThread.getThreadLocalStorageValueForKey(CurrentThreadSchedulerKeyInstance)CurrentThreadScheduler.swift:90 NSThread.setThreadLocalStorageValue(isScheduleRequired ? nil : CurrentThreadSchedulerValueInstance, forKey: CurrentThreadSchedulerKeyInstance)= CurrentThreadSchedulerQueueKey() 0020 0021 typealias CurrentThreadSchedulerValue
CurrentThreadScheduler.swift:42 return object === CurrentThreadSchedulerQueueKeyInstanceCurrentThreadScheduler.swift:48 return CurrentThreadSchedulerQueueKeyInstanceCurrentThreadScheduler.swift:52 return CurrentThreadSchedulerQueueKeyInstanceCurrentThreadScheduler.swift:74 return NSThread.getThreadLocalStorageValueForKey(CurrentThreadSchedulerQueueKeyInstance)CurrentThreadScheduler.swift:77 NSThread.setThreadLocalStorageValue(newValue, forKey: CurrentThreadSchedulerQueueKeyInstance)= CurrentThreadSchedulerKey 0022 let CurrentThreadSchedulerValueInstance
CurrentThreadScheduler.swift:86 let value: CurrentThreadSchedulerValue? = NSThread.getThreadLocalStorageValueForKey(CurrentThreadSchedulerKeyInstance)= CurrentThreadSchedulerKeyInstance 0023 0024 class CurrentThreadSchedulerKey
CurrentThreadScheduler.swift:90 NSThread.setThreadLocalStorageValue(isScheduleRequired ? nil : CurrentThreadSchedulerValueInstance, forKey: CurrentThreadSchedulerKeyInstance): NSObject, NSCopying { 0025 override func isEqual(object: AnyObject?) -> Bool { 0026 return object === CurrentThreadSchedulerKeyInstance 0027 } 0028 0029 override var hash: Int { return -904739208 } 0030 0031 override func copy() -> AnyObject { 0032 return CurrentThreadSchedulerKeyInstance 0033 } 0034 0035 func copyWithZone(zone: NSZone) -> AnyObject { 0036 return CurrentThreadSchedulerKeyInstance 0037 } 0038 } 0039 0040 class CurrentThreadSchedulerQueueKey
CurrentThreadScheduler.swift:18 let CurrentThreadSchedulerKeyInstance = CurrentThreadSchedulerKey()CurrentThreadScheduler.swift:21 typealias CurrentThreadSchedulerValue = CurrentThreadSchedulerKey: NSObject, NSCopying { 0041 override func isEqual(object: AnyObject?) -> Bool { 0042 return object === CurrentThreadSchedulerQueueKeyInstance 0043 } 0044 0045 override var hash: Int { return -904739207 } 0046 0047 override func copy() -> AnyObject { 0048 return CurrentThreadSchedulerQueueKeyInstance 0049 } 0050 0051 func copyWithZone(zone: NSZone) -> AnyObject { 0052 return CurrentThreadSchedulerQueueKeyInstance 0053 } 0054 } 0055 #endif 0056 0057 /** 0058 Represents an object that schedules units of work on the current thread. 0059 0060 This is the default scheduler for operators that generate elements. 0061 0062 This scheduler is also sometimes called `trampoline scheduler`. 0063 */ 0064 public class CurrentThreadScheduler
CurrentThreadScheduler.swift:19 let CurrentThreadSchedulerQueueKeyInstance = CurrentThreadSchedulerQueueKey(): ImmediateSchedulerType { 0065 typealias ScheduleQueue
CurrentThreadScheduler.swift:70 public static let instance = CurrentThreadScheduler()CurrentThreadScheduler.swift:105 if CurrentThreadScheduler.isScheduleRequired {CurrentThreadScheduler.swift:106 CurrentThreadScheduler.isScheduleRequired = falseCurrentThreadScheduler.swift:111 CurrentThreadScheduler.isScheduleRequired = trueCurrentThreadScheduler.swift:112 CurrentThreadScheduler.queue = nilCurrentThreadScheduler.swift:115 guard let queue = CurrentThreadScheduler.queue else {CurrentThreadScheduler.swift:129 let existingQueue = CurrentThreadScheduler.queueCurrentThreadScheduler.swift:137 CurrentThreadScheduler.queue = queueObservable+Creation.swift:143 public static func generate(initialState initialState: E, condition: E throws -> Bool, scheduler: ImmediateSchedulerType = CurrentThreadScheduler.instance, iterate: E throws -> E) -> Observable<E> {Observable+Creation.swift:157 public static func repeatElement(element: E, scheduler: ImmediateSchedulerType = CurrentThreadScheduler.instance) -> Observable<E> {Observable+Creation.swift:188 public static func range(start start: E, count: E, scheduler: ImmediateSchedulerType = CurrentThreadScheduler.instance) -> Observable<E> {Producer.swift:17 if !CurrentThreadScheduler.isScheduleRequired {Producer.swift:21 return CurrentThreadScheduler.instance.schedule(()) { _ in= RxMutableBox<Queue<ScheduledItemType>> 0066 0067 /** 0068 The singleton instance of the current thread scheduler. 0069 */ 0070 public static let instance
CurrentThreadScheduler.swift:72 static var queue : ScheduleQueue? {= CurrentThreadScheduler() 0071 0072 static var queue
Observable+Creation.swift:143 public static func generate(initialState initialState: E, condition: E throws -> Bool, scheduler: ImmediateSchedulerType = CurrentThreadScheduler.instance, iterate: E throws -> E) -> Observable<E> {Observable+Creation.swift:157 public static func repeatElement(element: E, scheduler: ImmediateSchedulerType = CurrentThreadScheduler.instance) -> Observable<E> {Observable+Creation.swift:188 public static func range(start start: E, count: E, scheduler: ImmediateSchedulerType = CurrentThreadScheduler.instance) -> Observable<E> {Producer.swift:21 return CurrentThreadScheduler.instance.schedule(()) { _ in: ScheduleQueue? { 0073 get { 0074 return NSThread.getThreadLocalStorageValueForKey(CurrentThreadSchedulerQueueKeyInstance) 0075 } 0076 set { 0077 NSThread.setThreadLocalStorageValue(newValue, forKey: CurrentThreadSchedulerQueueKeyInstance) 0078 } 0079 } 0080 0081 /** 0082 Gets a value that indicates whether the caller must call a `schedule` method. 0083 */ 0084 public static private(set) var isScheduleRequired
CurrentThreadScheduler.swift:112 CurrentThreadScheduler.queue = nilCurrentThreadScheduler.swift:115 guard let queue = CurrentThreadScheduler.queue else {CurrentThreadScheduler.swift:129 let existingQueue = CurrentThreadScheduler.queueCurrentThreadScheduler.swift:137 CurrentThreadScheduler.queue = queue: Bool { 0085 get { 0086 let value: CurrentThreadSchedulerValue? = NSThread.getThreadLocalStorageValueForKey(CurrentThreadSchedulerKeyInstance) 0087 return value == nil 0088 } 0089 set(isScheduleRequired) { 0090 NSThread.setThreadLocalStorageValue(isScheduleRequired ? nil : CurrentThreadSchedulerValueInstance, forKey: CurrentThreadSchedulerKeyInstance) 0091 } 0092 } 0093 0094 /** 0095 Schedules an action to be executed as soon as possible on current thread. 0096 0097 If this method is called on some thread that doesn't have `CurrentThreadScheduler` installed, scheduler will be 0098 automatically installed and uninstalled after all work is performed. 0099 0100 - parameter state: State passed to the action to be executed. 0101 - parameter action: Action to be executed. 0102 - returns: The disposable object used to cancel the scheduled action (best effort). 0103 */ 0104 public func schedule
CurrentThreadScheduler.swift:105 if CurrentThreadScheduler.isScheduleRequired {CurrentThreadScheduler.swift:106 CurrentThreadScheduler.isScheduleRequired = falseCurrentThreadScheduler.swift:111 CurrentThreadScheduler.isScheduleRequired = trueProducer.swift:17 if !CurrentThreadScheduler.isScheduleRequired {<StateType>(state: StateType, action: (StateType) -> Disposable) -> Disposable { 0105 if CurrentThreadScheduler.isScheduleRequired { 0106 CurrentThreadScheduler.isScheduleRequired = false 0107 0108 let disposable = action(state) 0109 0110 defer { 0111 CurrentThreadScheduler.isScheduleRequired = true 0112 CurrentThreadScheduler.queue = nil 0113 } 0114 0115 guard let queue = CurrentThreadScheduler.queue else { 0116 return disposable 0117 } 0118 0119 while let latest = queue.value.dequeue() { 0120 if latest.disposed { 0121 continue 0122 } 0123 latest.invoke() 0124 } 0125 0126 return disposable 0127 } 0128 0129 let existingQueue = CurrentThreadScheduler.queue 0130 0131 let queue: RxMutableBox<Queue<ScheduledItemType>> 0132 if let existingQueue = existingQueue { 0133 queue = existingQueue 0134 } 0135 else { 0136 queue = RxMutableBox(Queue<ScheduledItemType>(capacity: 1)) 0137 CurrentThreadScheduler.queue = queue 0138 } 0139 0140 let scheduledItem = ScheduledItem(action: action, state: state) 0141 queue.value.enqueue(scheduledItem) 0142 return scheduledItem 0143 } 0144 } 0145
Producer.swift:21 return CurrentThreadScheduler.instance.schedule(()) { _ in