0001    // Signal.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    /**
0024        A Signal is value that will or will not contain a value in the future (just
0025        like the concept of futures). In contrast to futures, the value of a signal
0026        can change at any time.
0027    
0028        Use next to subscribe to .Success updates, .error for .Error updates and 
0029        update to update the current value of the signal.
0030    
0031            let text = Signal<String>()
0032    
0033            text.next { string in
0034                println("Hello \(string)")
0035            }
0036    
0037            text.update(.Success("World"))
0038    
0039    */
0040    import Foundation
0041    #if os(Linux)
0042        import Glibc
0043    #else
0044        import Darwin
0045    #endif
0046    
0047    public final class Signal
Debounce.swift:26
public extension Signal {
Debounce.swift:47
    public func debounce(seconds: NSTimeInterval) -> Signal<T> {
Debounce.swift:48
        let signal = Signal<T>()
Debounce.swift:52
            func updateIfNeeded(signal: Signal<T>) -> Result<T> -> Void {
Debounce.swift:62
                            let s = Signal<T>()
Delay.swift:25
public extension Signal {
Delay.swift:31
    public func delay(seconds: NSTimeInterval, queue: dispatch_queue_t = dispatch_get_main_queue()) -> Signal<T> {
Delay.swift:32
        let signal = Signal<T>()
Signal.swift:66
    public func map<U>(f: T -> U) -> Signal<U> {
Signal.swift:67
        let signal = Signal<U>()
Signal.swift:77
    public func flatMap<U>(f: T -> Result<U>) -> Signal<U> {
Signal.swift:78
        let signal = Signal<U>()
Signal.swift:88
    public func flatMap<U>(f: T throws -> U) -> Signal<U> {
Signal.swift:89
        let signal = Signal<U>()
Signal.swift:99
    public func flatMap<U>(f: (T, (Result<U>->Void))->Void) -> Signal<U> {
Signal.swift:100
        let signal = Signal<U>()
Signal.swift:111
    public func flatMap<U>(f: (T -> Signal<U>)) -> Signal<U> {
Signal.swift:111
    public func flatMap<U>(f: (T -> Signal<U>)) -> Signal<U> {
Signal.swift:112
        let signal = Signal<U>()
Signal.swift:133
    public func ensure<U>(f: (Result<T>, (Result<U>->Void))->Void) -> Signal<U> {
Signal.swift:134
        let signal = Signal<U>()
Signal.swift:145
    public func subscribe(f: Result<T> -> Void) -> Signal<T> {
Signal.swift:155
    public func filter(f: T -> Bool) -> Signal<T>{
Signal.swift:156
        let signal = Signal<T>()
Signal.swift:173
    public func next(g: T -> Void) -> Signal<T> {
Signal.swift:187
    public func error(g: ErrorType -> Void) -> Signal<T> {
Signal.swift:206
    public func merge<U>(merge: Signal<U>) -> Signal<(T,U)> {
Signal.swift:206
    public func merge<U>(merge: Signal<U>) -> Signal<(T,U)> {
Signal.swift:207
        let signal = Signal<(T,U)>()
Waiting.swift:41
public extension Signal {
<T
Signal.swift:49
    private var value: Result<T>?
Signal.swift:50
    private var callbacks: [Result<T> -> Void] = []
Signal.swift:54
    public convenience init(_ value: T){
Signal.swift:66
    public func map<U>(f: T -> U) -> Signal<U> {
Signal.swift:77
    public func flatMap<U>(f: T -> Result<U>) -> Signal<U> {
Signal.swift:88
    public func flatMap<U>(f: T throws -> U) -> Signal<U> {
Signal.swift:99
    public func flatMap<U>(f: (T, (Result<U>->Void))->Void) -> Signal<U> {
Signal.swift:111
    public func flatMap<U>(f: (T -> Signal<U>)) -> Signal<U> {
Signal.swift:133
    public func ensure<U>(f: (Result<T>, (Result<U>->Void))->Void) -> Signal<U> {
Signal.swift:145
    public func subscribe(f: Result<T> -> Void) -> Signal<T> {
Signal.swift:145
    public func subscribe(f: Result<T> -> Void) -> Signal<T> {
Signal.swift:155
    public func filter(f: T -> Bool) -> Signal<T>{
Signal.swift:155
    public func filter(f: T -> Bool) -> Signal<T>{
Signal.swift:156
        let signal = Signal<T>()
Signal.swift:173
    public func next(g: T -> Void) -> Signal<T> {
Signal.swift:173
    public func next(g: T -> Void) -> Signal<T> {
Signal.swift:187
    public func error(g: ErrorType -> Void) -> Signal<T> {
Signal.swift:206
    public func merge<U>(merge: Signal<U>) -> Signal<(T,U)> {
Signal.swift:207
        let signal = Signal<(T,U)>()
Signal.swift:230
    public func update(result: Result<T>) {
Signal.swift:241
    public func update(value: T) {
Signal.swift:257
    public func peek() -> T? {
> { 0048 0049 private var value
Signal.swift:56
        self.value = .Success(value)
Signal.swift:146
        if let value = value {
Signal.swift:232
            value = result
Signal.swift:258
        return value?.value
: Result<T>? 0050 private var callbacks
Signal.swift:150
            callbacks.append(f)
Signal.swift:233
            callbacks.forEach{$0(result)}
: [Result<T> -> Void] = [] 0051 private let mutex
Signal.swift:149
        mutex.lock {
Signal.swift:231
        mutex.lock {
= Mutex() 0052 0053 /// Automatically infer the type of the signal from the argument. 0054 public convenience init(_ value: T){ 0055 self.init() 0056 self.value = .Success(value) 0057 } 0058 0059 public init
Signal.swift:55
        self.init()
() { 0060 0061 } 0062 0063 /** 0064 Transform the signal into another signal using a function. 0065 */ 0066 public func map<U>(f: T -> U) -> Signal<U> { 0067 let signal = Signal<U>() 0068 subscribe { result in 0069 signal.update(result.map(f)) 0070 } 0071 return signal 0072 } 0073 0074 /** 0075 Transform the signal into another signal using a function. 0076 */ 0077 public func flatMap<U>(f: T -> Result<U>) -> Signal<U> { 0078 let signal = Signal<U>() 0079 subscribe { result in 0080 signal.update(result.flatMap(f)) 0081 } 0082 return signal 0083 } 0084 0085 /** 0086 Transform the signal into another signal using a function. 0087 */ 0088 public func flatMap<U>(f: T throws -> U) -> Signal<U> { 0089 let signal = Signal<U>() 0090 subscribe { result in 0091 signal.update(result.flatMap(f)) 0092 } 0093 return signal 0094 } 0095 0096 /** 0097 Transform the signal into another signal using a function. 0098 */ 0099 public func flatMap<U>(f: (T, (Result<U>->Void))->Void) -> Signal<U> { 0100 let signal = Signal<U>() 0101 subscribe { result in 0102 result.flatMap(f)(signal.update) 0103 } 0104 return signal 0105 } 0106 0107 /** 0108 Transform the signal into another signal using a function, return the 0109 value of the inner signal 0110 */ 0111 public func flatMap<U>(f: (T -> Signal<U>)) -> Signal<U> { 0112 let signal = Signal<U>() 0113 subscribe { result in 0114 switch(result) { 0115 case let .Success(value): 0116 let innerSignal = f(value) 0117 innerSignal.subscribe { innerResult in 0118 signal.update(innerResult) 0119 } 0120 case let .Error(error): 0121 signal.update(.Error(error)) 0122 } 0123 } 0124 return signal 0125 } 0126 0127 /** 0128 Call a function with the result as an argument. Use this if the function should be 0129 executed no matter if the signal is a success or not. 0130 This method can also be used to convert an .Error into a .Success which might be handy 0131 for retry logic. 0132 */ 0133 public func ensure<U>(f: (Result<T>, (Result<U>->Void))->Void) -> Signal<U> { 0134 let signal = Signal<U>() 0135 subscribe { result in 0136 f(result) { signal.update($0) } 0137 } 0138 return signal 0139 } 0140 0141 /** 0142 Subscribe to the changes of this signal (.Error and .Success). 0143 This method is chainable. 0144 */ 0145 public func subscribe
Debounce.swift:50
        subscribe { result in
Debounce.swift:63
                            s.delay(seconds - timeSinceLastCall!).subscribe(updateIfNeeded(signal))
Delay.swift:33
        subscribe { result in
Signal.swift:68
        subscribe { result in
Signal.swift:79
        subscribe { result in
Signal.swift:90
        subscribe { result in
Signal.swift:101
        subscribe { result in
Signal.swift:113
        subscribe { result in
Signal.swift:117
                innerSignal.subscribe { innerResult in
Signal.swift:135
        subscribe { result in
Signal.swift:157
        subscribe { result in
Signal.swift:174
        subscribe { result in
Signal.swift:188
        subscribe { result in
Waiting.swift:52
        subscribe { r in
(f: Result<T> -> Void) -> Signal<T> { 0146 if let value = value { 0147 f(value) 0148 } 0149 mutex.lock { 0150 callbacks.append(f) 0151 } 0152 return self 0153 } 0154 0155 public func filter(f: T -> Bool) -> Signal<T>{ 0156 let signal = Signal<T>() 0157 subscribe { result in 0158 switch(result) { 0159 case let .Success(value): 0160 if f(value) { 0161 signal.update(result) 0162 } 0163 case let .Error(error): signal.update(.Error(error)) 0164 } 0165 } 0166 return signal 0167 } 0168 0169 /** 0170 Subscribe to the changes of this signal (.Success only). 0171 This method is chainable. 0172 */ 0173 public func next
Signal.swift:208
        self.next { a in
Signal.swift:213
        merge.next { b in
(g: T -> Void) -> Signal<T> { 0174 subscribe { result in 0175 switch(result) { 0176 case let .Success(value): g(value) 0177 case .Error(_): return 0178 } 0179 } 0180 return self 0181 } 0182 0183 /** 0184 Subscribe to the changes of this signal (.Error only). 0185 This method is chainable. 0186 */ 0187 public func error
Signal.swift:221
        self.error(errorHandler)
Signal.swift:222
        merge.error(errorHandler)
(g: ErrorType -> Void) -> Signal<T> { 0188 subscribe { result in 0189 switch(result) { 0190 case .Success(_): return 0191 case let .Error(error): g(error) 0192 } 0193 } 0194 return self 0195 } 0196 0197 /** 0198 Merge another signal into the current signal. This creates a signal that is 0199 a success if both source signals are a success. The value of the signal is a 0200 Tuple of the values of the contained signals. 0201 0202 let signal = Signal("Hello").merge(Signal("World")) 0203 signal.value! == ("Hello", "World") 0204 0205 */ 0206 public func merge<U>(merge: Signal<U>) -> Signal<(T,U)> { 0207 let signal = Signal<(T,U)>() 0208 self.next { a in 0209 if let b = merge.peek() { 0210 signal.update(.Success((a,b))) 0211 } 0212 } 0213 merge.next { b in 0214 if let a = self.peek() { 0215 signal.update(.Success((a,b))) 0216 } 0217 } 0218 let errorHandler = { (error: ErrorType) in 0219 signal.update(.Error(error)) 0220 } 0221 self.error(errorHandler) 0222 merge.error(errorHandler) 0223 return signal 0224 } 0225 0226 /** 0227 Update the content of the signal. This will notify all subscribers of this signal 0228 about the new value. 0229 */ 0230 public func update
Debounce.swift:58
                        signal.update(result)
Debounce.swift:64
                            s.update(result)
Delay.swift:35
                signal.update(result)
Signal.swift:69
            signal.update(result.map(f))
Signal.swift:80
            signal.update(result.flatMap(f))
Signal.swift:91
            signal.update(result.flatMap(f))
Signal.swift:102
            result.flatMap(f)(signal.update)
Signal.swift:118
                    signal.update(innerResult)
Signal.swift:121
                signal.update(.Error(error))
Signal.swift:136
            f(result) { signal.update($0) }
Signal.swift:161
                    signal.update(result)
Signal.swift:163
            case let .Error(error): signal.update(.Error(error))
Signal.swift:210
                signal.update(.Success((a,b)))
Signal.swift:215
                signal.update(.Success((a,b)))
Signal.swift:219
            signal.update(.Error(error))
Signal.swift:242
        update(.Success(value))
Signal.swift:250
        update(.Error(error))
(result: Result<T>) { 0231 mutex.lock { 0232 value = result 0233 callbacks.forEach{$0(result)} 0234 } 0235 } 0236 0237 /** 0238 Update the content of the signal. This will notify all subscribers of this signal 0239 about the new value. 0240 */ 0241 public func update(value: T) { 0242 update(.Success(value)) 0243 } 0244 0245 /** 0246 Update the content of the signal. This will notify all subscribers of this signal 0247 about the new value. 0248 */ 0249 public func update(error: ErrorType) { 0250 update(.Error(error)) 0251 } 0252 0253 /** 0254 Direct access to the content of the signal as an optional. If the result was a success, 0255 the optional will contain the value of the result. 0256 */ 0257 public func peek
Signal.swift:209
            if let b = merge.peek() {
Signal.swift:214
            if let a = self.peek() {
() -> T? { 0258 return value?.value 0259 } 0260 } 0261 0262 0263 private class Mutex
Signal.swift:51
    private let mutex = Mutex()
{ 0264 private var mutex
Signal.swift:267
        pthread_mutex_init(&mutex, nil)
Signal.swift:271
        pthread_mutex_destroy(&mutex)
Signal.swift:275
        return pthread_mutex_lock(&mutex)
Signal.swift:279
        return pthread_mutex_unlock(&mutex)
= pthread_mutex_t() 0265 0266 init
Signal.swift:51
    private let mutex = Mutex()
() { 0267 pthread_mutex_init(&mutex, nil) 0268 } 0269 0270 deinit { 0271 pthread_mutex_destroy(&mutex) 0272 } 0273 0274 func lock
Signal.swift:283
        let status = lock()
() -> Int32 { 0275 return pthread_mutex_lock(&mutex) 0276 } 0277 0278 func unlock
Signal.swift:285
        defer { unlock() }
() -> Int32 { 0279 return pthread_mutex_unlock(&mutex) 0280 } 0281 0282 func lock
Signal.swift:149
        mutex.lock {
Signal.swift:231
        mutex.lock {
(@noescape closure: () -> Void) { 0283 let status = lock() 0284 assert(status == 0, "pthread_mutex_lock: \(strerror(status))") 0285 defer { unlock() } 0286 closure() 0287 } 0288 } 0289