Created
November 6, 2016 01:02
-
-
Save andersio/9906e1e06027a1ff687ad7a61e70d5a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Result | |
import ReactiveSwift | |
import Foundation | |
/*: | |
## Sandbox | |
A place where you can build your sand castles 🏖. | |
*/ | |
public struct EventType<Value, Error: Swift.Error, U> { | |
internal let setUp: (Signal<Value, Error>, @escaping (U) -> Void) -> Void | |
public static var values: EventType<Value, Error, Value> { | |
return EventType<Value, Error, Value> { signal, action in | |
signal.observe(Observer<Value, Error>(value: action)) | |
} | |
} | |
public static var failed: EventType<Value, Error, Error> { | |
return EventType<Value, Error, Error> { signal, action in | |
signal.observe(Observer<Value, Error>(failed: action)) | |
} | |
} | |
public static var completed: EventType<Value, Error, ()> { | |
return EventType<Value, Error, ()> { signal, action in | |
signal.observe(Observer<Value, Error>(completed: action)) | |
} | |
} | |
public static var interrupted: EventType<Value, Error, ()> { | |
return EventType<Value, Error, ()> { signal, action in | |
signal.observe(Observer<Value, Error>(interrupted: action)) | |
} | |
} | |
public static var all: EventType<Value, Error, Event<Value, Error>> { | |
return EventType<Value, Error, Event<Value, Error>> { signal, action in | |
signal.observe(Observer<Value, Error>(action)) | |
} | |
} | |
private init(setUp: @escaping (Signal<Value, Error>, @escaping (U) -> Void) -> Void) { | |
self.setUp = setUp | |
} | |
} | |
extension SignalProtocol { | |
public func observe<U>(_ type: EventType<Value, Error, U>, action: @escaping (U) -> Void) { | |
type.setUp(signal, action) | |
} | |
} | |
Signal<Int, NoError>.empty.observe(.values) { | |
print($0) | |
} | |
Signal<Int, NoError>.empty.observe(.failed) { | |
print($0) | |
} | |
Signal<Int, NoError>.empty.observe(.completed) { | |
print($0) | |
} | |
Signal<Int, NoError>.empty.observe(.interrupted) { | |
print($0) | |
} | |
Signal<Int, NoError>.empty.observe(.all) { | |
print($0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment