Skip to content

Commit 225116d

Browse files
committed
Replace Observer.action with Observer.send.
1 parent 7b23858 commit 225116d

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

Sources/Observer.swift

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@ extension Signal {
1111
/// (typically from a Signal).
1212
public final class Observer {
1313
public typealias Action = (Event) -> Void
14+
private let _action: Action
1415

1516
/// An action that will be performed upon arrival of the event.
16-
public let action: Action
17+
@available(*, deprecated: 2.0, renamed:"send(_:)")
18+
public var action: Action {
19+
guard !interruptsOnDeinit && wrapped == nil else {
20+
return { self._action($0) }
21+
}
22+
return _action
23+
}
1724

1825
/// Whether the observer should send an `interrupted` event as it deinitializes.
1926
private let interruptsOnDeinit: Bool
@@ -40,9 +47,9 @@ extension Signal {
4047
) {
4148
var hasDeliveredTerminalEvent = false
4249

43-
self.action = transform { event in
50+
self._action = transform { event in
4451
if !hasDeliveredTerminalEvent {
45-
observer.action(event)
52+
observer._action(event)
4653

4754
if event.isTerminating {
4855
hasDeliveredTerminalEvent = true
@@ -63,7 +70,7 @@ extension Signal {
6370
/// - interruptsOnDeinit: `true` if the observer should send an `interrupted`
6471
/// event as it deinitializes. `false` otherwise.
6572
internal init(action: @escaping Action, interruptsOnDeinit: Bool) {
66-
self.action = action
73+
self._action = action
6774
self.wrapped = nil
6875
self.interruptsOnDeinit = interruptsOnDeinit
6976
}
@@ -74,7 +81,7 @@ extension Signal {
7481
/// - parameters:
7582
/// - action: A closure to lift over received event.
7683
public init(_ action: @escaping Action) {
77-
self.action = action
84+
self._action = action
7885
self.wrapped = nil
7986
self.interruptsOnDeinit = false
8087
}
@@ -128,34 +135,39 @@ extension Signal {
128135
// Since `Signal` would ensure that only one terminal event would ever be
129136
// sent for any given `Signal`, we do not need to assert any condition
130137
// here.
131-
action(.interrupted)
138+
_action(.interrupted)
132139
}
133140
}
134141

142+
/// Puts an event into `self`.
143+
public func send(_ event: Event) {
144+
_action(event)
145+
}
146+
135147
/// Puts a `value` event into `self`.
136148
///
137149
/// - parameters:
138150
/// - value: A value sent with the `value` event.
139151
public func send(value: Value) {
140-
action(.value(value))
152+
_action(.value(value))
141153
}
142154

143155
/// Puts a failed event into `self`.
144156
///
145157
/// - parameters:
146158
/// - error: An error object sent with failed event.
147159
public func send(error: Error) {
148-
action(.failed(error))
160+
_action(.failed(error))
149161
}
150162

151163
/// Puts a `completed` event into `self`.
152164
public func sendCompleted() {
153-
action(.completed)
165+
_action(.completed)
154166
}
155167

156168
/// Puts an `interrupted` event into `self`.
157169
public func sendInterrupted() {
158-
action(.interrupted)
170+
_action(.interrupted)
159171
}
160172
}
161173
}

0 commit comments

Comments
 (0)