0001 // Threading.swift 0002 // 0003 // Copyright (c) 2015 Jens Ravens (http://jensravens.de) 0004 // 0005 // Permission is hereby granted, free of charge, to any person obtaining a copy 0006 // of this software and associated documentation files (the "Software"), to deal 0007 // in the Software without restriction, including without limitation the rights 0008 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 0009 // copies of the Software, and to permit persons to whom the Software is 0010 // furnished to do so, subject to the following conditions: 0011 // 0012 // The above copyright notice and this permission notice shall be included in 0013 // all copies or substantial portions of the Software. 0014 // 0015 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 0016 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 0017 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 0018 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 0019 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 0020 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 0021 // THE SOFTWARE. 0022 0023 import Foundation 0024 0025 /** 0026 This error is thrown if the signal doesn't complete within the specified timeout in a wait function. 0027 */ 0028 public struct TimeoutError: ErrorType { 0029 internal init
Waiting.swift:58 throw TimeoutError()() {} 0030 } 0031 0032 #if os(Linux) 0033 #else 0034 internal extension NSTimeInterval { 0035 var dispatchTime
Waiting.swift:58 throw TimeoutError(): dispatch_time_t { 0036 return dispatch_time(DISPATCH_TIME_NOW, Int64(self * Double(NSEC_PER_SEC))) 0037 } 0038 } 0039 #endif 0040 0041 public extension Signal { 0042 #if os(Linux) 0043 #else 0044 /** 0045 Wait until the signal updates the next time. This will block the current thread until there 0046 is an error or a successfull value. In case of an error, the error will be thrown. 0047 */ 0048 public func wait(timeout: NSTimeInterval? = nil) throws -> T { 0049 let group = dispatch_group_create() 0050 var result: Result<T>? 0051 dispatch_group_enter(group) 0052 subscribe { r in 0053 result = r 0054 dispatch_group_leave(group) 0055 } 0056 let timestamp = timeout.map{ $0.dispatchTime } ?? DISPATCH_TIME_FOREVER 0057 if dispatch_group_wait(group, timestamp) != 0 { 0058 throw TimeoutError() 0059 } 0060 switch result! { 0061 case let .Success(t): return t 0062 case let .Error(e): throw e 0063 } 0064 } 0065 #endif 0066 }
Delay.swift:34 dispatch_after(seconds.dispatchTime, queue) {Waiting.swift:56 let timestamp = timeout.map{ $0.dispatchTime } ?? DISPATCH_TIME_FOREVER