Skip to content

Commit

Permalink
Remove Event and send initial values
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperspacemark committed Sep 29, 2015
1 parent 5ead6a6 commit 683adec
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
8 changes: 4 additions & 4 deletions Notice/Observable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ public struct Observable<T> {

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)
}
}
}
Expand All @@ -36,6 +35,7 @@ public struct Observable<T> {

public mutating func subscribe(handler: SubscriptionType.EventHandler) -> SubscriptionType {
let subscription = Subscription(handler: handler)
subscription.handler(value)
subscribers.add(subscription)
return subscription
}
Expand Down
7 changes: 1 addition & 6 deletions Notice/Subscription.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
public struct Event<T> {
public let oldValue: T
public let newValue: T
}

public class Subscription<T>: Hashable {
public typealias EventHandler = (Event<T>) -> Void
public typealias EventHandler = T -> Void

let handler: EventHandler
private let UUID = NSUUID().UUIDString
Expand Down
10 changes: 5 additions & 5 deletions Notice/Subscriptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ protocol SubscriptionsType {

mutating func add(subscription: Subscription<ValueType>)
mutating func remove(subscription: Subscription<ValueType>)
func send(event: Event<ValueType>)
func send(value: ValueType)
}

extension SubscriptionsType {
Expand All @@ -23,8 +23,8 @@ class Subscriptions<T>: SubscriptionsType {

var subscriptions = Set<Subscription<ValueType>>()

func send(event: Event<ValueType>) {
subscriptions.forEach { $0.handler(event) }
func send(value: ValueType) {
subscriptions.forEach { $0.handler(value) }
}
}

Expand All @@ -33,8 +33,8 @@ class OnceSubscriptions<T>: SubscriptionsType {

var subscriptions = Set<Subscription<ValueType>>()

func send(event: Event<ValueType>) {
subscriptions.forEach { $0.handler(event) }
func send(value: ValueType) {
subscriptions.forEach { $0.handler(value) }
subscriptions.removeAll()
}
}

0 comments on commit 683adec

Please sign in to comment.