From 683adeccedeef1dac4629ed531d98b9b9d9be3a7 Mon Sep 17 00:00:00 2001 From: Mark Adams Date: Tue, 29 Sep 2015 13:41:19 -0400 Subject: [PATCH] Remove Event and send initial values --- Notice/Observable.swift | 8 ++++---- Notice/Subscription.swift | 7 +------ Notice/Subscriptions.swift | 10 +++++----- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/Notice/Observable.swift b/Notice/Observable.swift index e850c70..a2d744f 100644 --- a/Notice/Observable.swift +++ b/Notice/Observable.swift @@ -13,10 +13,9 @@ public struct Observable { public var value: T { didSet { - dispatch_sync(eventQueue) { () -> Void in - let event = Event(oldValue: oldValue, newValue: self.value) - self.subscribers.send(event) - self.onceSubscribers.send(event) + dispatch_sync(eventQueue) { + self.subscribers.send(self.value) + self.onceSubscribers.send(self.value) } } } @@ -36,6 +35,7 @@ public struct Observable { public mutating func subscribe(handler: SubscriptionType.EventHandler) -> SubscriptionType { let subscription = Subscription(handler: handler) + subscription.handler(value) subscribers.add(subscription) return subscription } diff --git a/Notice/Subscription.swift b/Notice/Subscription.swift index 887a29d..9b603f8 100644 --- a/Notice/Subscription.swift +++ b/Notice/Subscription.swift @@ -1,10 +1,5 @@ -public struct Event { - public let oldValue: T - public let newValue: T -} - public class Subscription: Hashable { - public typealias EventHandler = (Event) -> Void + public typealias EventHandler = T -> Void let handler: EventHandler private let UUID = NSUUID().UUIDString diff --git a/Notice/Subscriptions.swift b/Notice/Subscriptions.swift index 4297666..88bfda1 100644 --- a/Notice/Subscriptions.swift +++ b/Notice/Subscriptions.swift @@ -5,7 +5,7 @@ protocol SubscriptionsType { mutating func add(subscription: Subscription) mutating func remove(subscription: Subscription) - func send(event: Event) + func send(value: ValueType) } extension SubscriptionsType { @@ -23,8 +23,8 @@ class Subscriptions: SubscriptionsType { var subscriptions = Set>() - func send(event: Event) { - subscriptions.forEach { $0.handler(event) } + func send(value: ValueType) { + subscriptions.forEach { $0.handler(value) } } } @@ -33,8 +33,8 @@ class OnceSubscriptions: SubscriptionsType { var subscriptions = Set>() - func send(event: Event) { - subscriptions.forEach { $0.handler(event) } + func send(value: ValueType) { + subscriptions.forEach { $0.handler(value) } subscriptions.removeAll() } }