-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from bfcrampton/master
Allow for subscriptions which contain oldValue from didSet
- Loading branch information
Showing
5 changed files
with
83 additions
and
30 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
public protocol Event { | ||
typealias ValueType | ||
typealias HandlerType | ||
|
||
var value: ValueType { get } | ||
func handle(handler: HandlerType) | ||
} | ||
|
||
public struct NewEvent<T>: Event { | ||
public let value: T | ||
|
||
public func handle(handler: T -> Void) { | ||
handler(value) | ||
} | ||
} | ||
|
||
public struct NewOldEvent<T>: Event { | ||
public let value: T | ||
public let oldValue: T? | ||
|
||
init(value: T, oldValue: T? = nil) { | ||
self.value = value | ||
self.oldValue = oldValue | ||
} | ||
|
||
public func handle(handler: (old: T?, new: T) -> Void) { | ||
handler(old: oldValue, new: value) | ||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,27 @@ | ||
protocol SubscriptionsType { | ||
typealias ValueType | ||
typealias EventType: Event | ||
|
||
var subscriptions: Set<Subscription<ValueType>> { get set } | ||
var subscriptions: Set<Subscription<EventType>> { get set } | ||
|
||
mutating func add(subscription: Subscription<ValueType>) | ||
mutating func remove(subscription: Subscription<ValueType>) | ||
func send(value: ValueType) | ||
mutating func add(subscription: Subscription<EventType>) | ||
mutating func remove(subscription: Subscription<EventType>) | ||
func send(value: EventType) | ||
} | ||
|
||
extension SubscriptionsType { | ||
mutating func add(subscription: Subscription<ValueType>) { | ||
mutating func add(subscription: Subscription<EventType>) { | ||
subscriptions.insert(subscription) | ||
} | ||
|
||
mutating func remove(subscription: Subscription<ValueType>) { | ||
mutating func remove(subscription: Subscription<EventType>) { | ||
subscriptions.remove(subscription) | ||
} | ||
} | ||
|
||
class Subscriptions<T>: SubscriptionsType { | ||
typealias ValueType = T | ||
class Subscriptions<EventType: Event>: SubscriptionsType { | ||
var subscriptions = Set<Subscription<EventType>>() | ||
|
||
var subscriptions = Set<Subscription<ValueType>>() | ||
|
||
func send(value: ValueType) { | ||
subscriptions.forEach { $0.handler(value) } | ||
func send(event: EventType) { | ||
subscriptions.forEach { $0.handle(event) } | ||
} | ||
} |