Skip to content

Instantly share code, notes, and snippets.

@DivineDominion
Created January 25, 2016 16:29
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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