Created
January 25, 2016 16:29
-
-
Save DivineDominion/3ef05d45f660d1027191 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 Foundation | |
public struct Subscription<State: Any> { | |
let subscriber: AnyStoreSubscriber | |
let selector: (State -> Any) | |
init(aSubscriber: AnyStoreSubscriber) { | |
self.subscriber = aSubscriber | |
self.selector = { (state: Any) in state } | |
} | |
init(subscriber: AnyStoreSubscriber, filteringState selector: (State -> Any)) { | |
self.subscriber = subscriber | |
self.selector = selector | |
} | |
init<S: StoreSubscriber where S.StoreSubscriberStateType == State>(subscriber: S) { | |
self.subscriber = subscriber | |
self.selector = { $0 } | |
} | |
init<S: StoreSubscriber where S.StoreSubscriberStateType == State>(subscriber: S, filteringState selector: (State -> Any)) { | |
self.subscriber = subscriber | |
self.selector = selector | |
} | |
} | |
class Foo: AnyStoreSubscriber { | |
func _newState(state: Any) { | |
} | |
} | |
extension String: StateType {} | |
class StringSubscriber: StoreSubscriber { | |
typealias StoreSubscriberStateType = String | |
func newState(state: StoreSubscriberStateType) { } | |
} | |
func bar() { | |
//Subscription(aSubscriber: Foo()) // doesnt compile, cannot infer State | |
_ = Subscription(subscriber: StringSubscriber()) | |
_ = Subscription(subscriber: StringSubscriber()) { state in | |
return state.lowercaseString | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment