Skip to content

Commit 706f670

Browse files
committed
Simplified protocol conformance and removed the weird identity.
1 parent 5551502 commit 706f670

2 files changed

Lines changed: 38 additions & 35 deletions

File tree

Sources/ColdSignal.swift

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88

99
import Foundation
1010

11-
public final class ColdSignal<Value, ErrorType: Error>: ColdSignalType, InternalSignalType, SpecialSignalGenerator {
11+
public final class ColdSignal<T, E: Error>: ColdSignalType, InternalSignalType, SpecialSignalGenerator {
12+
13+
public typealias Value = T
14+
public typealias ErrorType = E
15+
1216
internal var observers = Bag<Observer<Value, ErrorType>>()
1317

1418
public var coldSignal: ColdSignal {
@@ -77,20 +81,6 @@ public final class ColdSignal<Value, ErrorType: Error>: ColdSignalType, Internal
7781
started = false
7882
}
7983

80-
/// Adds an observer to the ColdSignal which observes any future events from the
81-
/// ColdSignal. If the Signal has already terminated, the observer will immediately
82-
/// receive an `Interrupted` event.
83-
///
84-
/// Returns a Disposable which can be used to disconnect the observer. Disposing
85-
/// of the Disposable will have no effect on the Signal itself.
86-
@discardableResult
87-
public func add(observer: Observer<Value, ErrorType>) -> Disposable? {
88-
let token = self.observers.insert(value: observer)
89-
return ActionDisposable {
90-
self.observers.removeValueForToken(token: token)
91-
}
92-
}
93-
9484
}
9585

9686
extension ColdSignal: CustomDebugStringConvertible {
@@ -121,7 +111,9 @@ public protocol ColdSignalType: SignalType {
121111
public extension ColdSignalType {
122112

123113
public var signal: Signal<Value, ErrorType> {
124-
return self.identity
114+
return Signal { observer in
115+
self.coldSignal.add(observer: observer)
116+
}
125117
}
126118

127119
/// Invokes the closure provided upon initialization, and passes in a newly
@@ -141,6 +133,20 @@ public extension ColdSignalType {
141133

142134
public extension ColdSignalType {
143135

136+
/// Adds an observer to the ColdSignal which observes any future events from the
137+
/// ColdSignal. If the Signal has already terminated, the observer will immediately
138+
/// receive an `Interrupted` event.
139+
///
140+
/// Returns a Disposable which can be used to disconnect the observer. Disposing
141+
/// of the Disposable will have no effect on the Signal itself.
142+
@discardableResult
143+
public func add(observer: Observer<Value, ErrorType>) -> Disposable? {
144+
let token = coldSignal.observers.insert(value: observer)
145+
return ActionDisposable {
146+
self.coldSignal.observers.removeValueForToken(token: token)
147+
}
148+
}
149+
144150
/// Creates a ColdSignal, adds exactly one observer, and then immediately
145151
/// invokes start on the ColdSignal.
146152
///

Sources/Signal.swift

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,6 @@ public final class Signal<Value, ErrorType: Error>: SignalType, InternalSignalTy
4040
generatorDisposable.innerDisposable = generator(inputObserver)
4141
}
4242

43-
/// Adds an observer to the Signal which observes any future events from the Signal.
44-
/// If the Signal has already terminated, the observer will immediately receive an
45-
/// `Interrupted` event.
46-
///
47-
/// Returns a Disposable which can be used to disconnect the observer. Disposing
48-
/// of the Disposable will have no effect on the Signal itself.
49-
@discardableResult
50-
public func add(observer: Observer<Value, ErrorType>) -> Disposable? {
51-
let token = observers.insert(value: observer)
52-
return ActionDisposable { [weak self] in
53-
self?.observers.removeValueForToken(token: token)
54-
}
55-
56-
}
57-
5843
/// Creates a Signal that will be controlled by sending events to the returned
5944
/// observer.
6045
///
@@ -156,17 +141,14 @@ public extension SpecialSignalGenerator {
156141
}
157142

158143
public protocol SignalType {
144+
159145
/// The type of values being sent on the signal.
160146
associatedtype Value
161147

162148
/// The type of error that can occur on the signal. If errors aren't possible
163149
/// then `NoError` can be used.
164150
associatedtype ErrorType: Error
165151

166-
/// Observes the Signal by sending any future events to the given observer.
167-
@discardableResult
168-
func add(observer: Observer<Value, ErrorType>) -> Disposable?
169-
170152
/// The exposed raw signal that underlies the ColdSignalType
171153
var signal: Signal<Value, ErrorType> { get }
172154

@@ -192,6 +174,21 @@ internal extension InternalSignalType {
192174
}
193175

194176
public extension SignalType {
177+
178+
/// Adds an observer to the Signal which observes any future events from the Signal.
179+
/// If the Signal has already terminated, the observer will immediately receive an
180+
/// `Interrupted` event.
181+
///
182+
/// Returns a Disposable which can be used to disconnect the observer. Disposing
183+
/// of the Disposable will have no effect on the Signal itself.
184+
@discardableResult
185+
public func add(observer: Observer<Value, ErrorType>) -> Disposable? {
186+
let token = signal.observers.insert(value: observer)
187+
return ActionDisposable {
188+
self.signal.observers.removeValueForToken(token: token)
189+
}
190+
191+
}
195192

196193
/// Convenience override for add(observer:) to allow trailing-closure style
197194
/// invocations.

0 commit comments

Comments
 (0)