Skip to content

Commit a4dfdab

Browse files
committed
Nest Event inside Signal.
1 parent 3c762b6 commit a4dfdab

File tree

7 files changed

+131
-126
lines changed

7 files changed

+131
-126
lines changed

Sources/Action.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public final class Action<Input, Output, Error: Swift.Error> {
2121
private let deinitToken: Lifetime.Token
2222

2323
private let executeClosure: (_ state: Any, _ input: Input) -> SignalProducer<Output, Error>
24-
private let eventsObserver: Signal<Event<Output, Error>, NoError>.Observer
24+
private let eventsObserver: Signal<Signal<Output, Error>.Event, NoError>.Observer
2525
private let disabledErrorsObserver: Signal<(), NoError>.Observer
2626

2727
/// The lifetime of the `Action`.
@@ -31,7 +31,7 @@ public final class Action<Input, Output, Error: Swift.Error> {
3131
///
3232
/// In other words, this sends every `Event` from every unit of work that the `Action`
3333
/// executes.
34-
public let events: Signal<Event<Output, Error>, NoError>
34+
public let events: Signal<Signal<Output, Error>.Event, NoError>
3535

3636
/// A signal of all values generated from all units of work of the `Action`.
3737
///
@@ -93,7 +93,7 @@ public final class Action<Input, Output, Error: Swift.Error> {
9393

9494
executeClosure = { state, input in execute(state as! State.Value, input) }
9595

96-
(events, eventsObserver) = Signal<Event<Output, Error>, NoError>.pipe()
96+
(events, eventsObserver) = Signal<Signal<Output, Error>.Event, NoError>.pipe()
9797
(disabledErrors, disabledErrorsObserver) = Signal<(), NoError>.pipe()
9898

9999
values = events.filterMap { $0.value }

Sources/Deprecations+Removals.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ public func timer(interval: DispatchTimeInterval, on scheduler: DateScheduler) -
9797
public func timer(interval: DispatchTimeInterval, on scheduler: DateScheduler, leeway: DispatchTimeInterval) -> SignalProducer<Date, NoError> { fatalError() }
9898

9999
// MARK: Obsolete types in ReactiveSwift 2.0.
100+
@available(*, deprecated, message:"Use `Signal<Value, Error>.Event` instead.")
101+
public typealias Event<Value, Error: Swift.Error> = Signal<Value, Error>.Event
102+
100103
extension Action {
101104
@available(*, unavailable, renamed:"init(state:enabledIf:execute:)")
102105
public convenience init<State: PropertyProtocol>(state property: State, enabledIf isEnabled: @escaping (State.Value) -> Bool, _ execute: @escaping (State.Value, Input) -> SignalProducer<Output, Error>) { fatalError() }
@@ -236,18 +239,18 @@ extension Action {
236239

237240
// Renamed Enum cases
238241

239-
extension Event {
242+
extension Signal.Event {
240243
@available(*, unavailable, renamed:"value")
241-
public static var Next: Event<Value, Error> { fatalError() }
244+
public static var Next: Signal<Value, Error>.Event { fatalError() }
242245

243246
@available(*, unavailable, renamed:"failed")
244-
public static var Failed: Event<Value, Error> { fatalError() }
247+
public static var Failed: Signal<Value, Error>.Event { fatalError() }
245248

246249
@available(*, unavailable, renamed:"completed")
247-
public static var Completed: Event<Value, Error> { fatalError() }
250+
public static var Completed: Signal<Value, Error>.Event { fatalError() }
248251

249252
@available(*, unavailable, renamed:"interrupted")
250-
public static var Interrupted: Event<Value, Error> { fatalError() }
253+
public static var Interrupted: Signal<Value, Error>.Event { fatalError() }
251254
}
252255

253256
extension ActionError {

Sources/Event.swift

Lines changed: 106 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -6,122 +6,124 @@
66
// Copyright (c) 2015 GitHub. All rights reserved.
77
//
88

9-
/// Represents a signal event.
10-
///
11-
/// Signals must conform to the grammar:
12-
/// `value* (failed | completed | interrupted)?`
13-
public enum Event<Value, Error: Swift.Error> {
14-
/// A value provided by the signal.
15-
case value(Value)
16-
17-
/// The signal terminated because of an error. No further events will be
18-
/// received.
19-
case failed(Error)
20-
21-
/// The signal successfully terminated. No further events will be received.
22-
case completed
23-
24-
/// Event production on the signal has been interrupted. No further events
25-
/// will be received.
9+
extension Signal {
10+
/// Represents a signal event.
2611
///
27-
/// - important: This event does not signify the successful or failed
28-
/// completion of the signal.
29-
case interrupted
30-
31-
/// Whether this event is a completed event.
32-
public var isCompleted: Bool {
33-
switch self {
34-
case .completed:
35-
return true
36-
37-
case .value, .failed, .interrupted:
38-
return false
12+
/// Signals must conform to the grammar:
13+
/// `value* (failed | completed | interrupted)?`
14+
public enum Event {
15+
/// A value provided by the signal.
16+
case value(Value)
17+
18+
/// The signal terminated because of an error. No further events will be
19+
/// received.
20+
case failed(Error)
21+
22+
/// The signal successfully terminated. No further events will be received.
23+
case completed
24+
25+
/// Event production on the signal has been interrupted. No further events
26+
/// will be received.
27+
///
28+
/// - important: This event does not signify the successful or failed
29+
/// completion of the signal.
30+
case interrupted
31+
32+
/// Whether this event is a completed event.
33+
public var isCompleted: Bool {
34+
switch self {
35+
case .completed:
36+
return true
37+
38+
case .value, .failed, .interrupted:
39+
return false
40+
}
3941
}
40-
}
4142

42-
/// Whether this event indicates signal termination (i.e., that no further
43-
/// events will be received).
44-
public var isTerminating: Bool {
45-
switch self {
46-
case .value:
47-
return false
43+
/// Whether this event indicates signal termination (i.e., that no further
44+
/// events will be received).
45+
public var isTerminating: Bool {
46+
switch self {
47+
case .value:
48+
return false
4849

49-
case .failed, .completed, .interrupted:
50-
return true
50+
case .failed, .completed, .interrupted:
51+
return true
52+
}
5153
}
52-
}
53-
54-
/// Lift the given closure over the event's value.
55-
///
56-
/// - important: The closure is called only on `value` type events.
57-
///
58-
/// - parameters:
59-
/// - f: A closure that accepts a value and returns a new value
60-
///
61-
/// - returns: An event with function applied to a value in case `self` is a
62-
/// `value` type of event.
63-
public func map<U>(_ f: (Value) -> U) -> Event<U, Error> {
64-
switch self {
65-
case let .value(value):
66-
return .value(f(value))
67-
68-
case let .failed(error):
69-
return .failed(error)
70-
71-
case .completed:
72-
return .completed
7354

74-
case .interrupted:
75-
return .interrupted
55+
/// Lift the given closure over the event's value.
56+
///
57+
/// - important: The closure is called only on `value` type events.
58+
///
59+
/// - parameters:
60+
/// - f: A closure that accepts a value and returns a new value
61+
///
62+
/// - returns: An event with function applied to a value in case `self` is a
63+
/// `value` type of event.
64+
public func map<U>(_ f: (Value) -> U) -> Signal<U, Error>.Event {
65+
switch self {
66+
case let .value(value):
67+
return .value(f(value))
68+
69+
case let .failed(error):
70+
return .failed(error)
71+
72+
case .completed:
73+
return .completed
74+
75+
case .interrupted:
76+
return .interrupted
77+
}
7678
}
77-
}
78-
79-
/// Lift the given closure over the event's error.
80-
///
81-
/// - important: The closure is called only on failed type event.
82-
///
83-
/// - parameters:
84-
/// - f: A closure that accepts an error object and returns
85-
/// a new error object
86-
///
87-
/// - returns: An event with function applied to an error object in case
88-
/// `self` is a `.Failed` type of event.
89-
public func mapError<F>(_ f: (Error) -> F) -> Event<Value, F> {
90-
switch self {
91-
case let .value(value):
92-
return .value(value)
93-
94-
case let .failed(error):
95-
return .failed(f(error))
96-
97-
case .completed:
98-
return .completed
9979

100-
case .interrupted:
101-
return .interrupted
80+
/// Lift the given closure over the event's error.
81+
///
82+
/// - important: The closure is called only on failed type event.
83+
///
84+
/// - parameters:
85+
/// - f: A closure that accepts an error object and returns
86+
/// a new error object
87+
///
88+
/// - returns: An event with function applied to an error object in case
89+
/// `self` is a `.Failed` type of event.
90+
public func mapError<F>(_ f: (Error) -> F) -> Signal<Value, F>.Event {
91+
switch self {
92+
case let .value(value):
93+
return .value(value)
94+
95+
case let .failed(error):
96+
return .failed(f(error))
97+
98+
case .completed:
99+
return .completed
100+
101+
case .interrupted:
102+
return .interrupted
103+
}
102104
}
103-
}
104105

105-
/// Unwrap the contained `value` value.
106-
public var value: Value? {
107-
if case let .value(value) = self {
108-
return value
109-
} else {
110-
return nil
106+
/// Unwrap the contained `value` value.
107+
public var value: Value? {
108+
if case let .value(value) = self {
109+
return value
110+
} else {
111+
return nil
112+
}
111113
}
112-
}
113114

114-
/// Unwrap the contained `Error` value.
115-
public var error: Error? {
116-
if case let .failed(error) = self {
117-
return error
118-
} else {
119-
return nil
115+
/// Unwrap the contained `Error` value.
116+
public var error: Error? {
117+
if case let .failed(error) = self {
118+
return error
119+
} else {
120+
return nil
121+
}
120122
}
121123
}
122124
}
123125

124-
public func == <Value: Equatable, Error: Equatable> (lhs: Event<Value, Error>, rhs: Event<Value, Error>) -> Bool {
126+
public func == <Value: Equatable, Error: Equatable> (lhs: Signal<Value, Error>.Event, rhs: Signal<Value, Error>.Event) -> Bool {
125127
switch (lhs, rhs) {
126128
case let (.value(left), .value(right)):
127129
return left == right
@@ -140,7 +142,7 @@ public func == <Value: Equatable, Error: Equatable> (lhs: Event<Value, Error>, r
140142
}
141143
}
142144

143-
extension Event: CustomStringConvertible {
145+
extension Signal.Event: CustomStringConvertible {
144146
public var description: String {
145147
switch self {
146148
case let .value(value):
@@ -166,11 +168,11 @@ public protocol EventProtocol {
166168
/// be used.
167169
associatedtype Error: Swift.Error
168170
/// Extracts the event from the receiver.
169-
var event: Event<Value, Error> { get }
171+
var event: Signal<Value, Error>.Event { get }
170172
}
171173

172-
extension Event: EventProtocol {
173-
public var event: Event<Value, Error> {
174+
extension Signal.Event: EventProtocol {
175+
public var event: Signal<Value, Error>.Event {
174176
return self
175177
}
176178
}

Sources/Observer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/// An Observer is a simple wrapper around a function which can receive Events
1010
/// (typically from a Signal).
1111
public final class Observer<Value, Error: Swift.Error> {
12-
public typealias Action = (Event<Value, Error>) -> Void
12+
public typealias Action = (Signal<Value, Error>.Event) -> Void
1313

1414
/// An action that will be performed upon arrival of the event.
1515
public let action: Action

Sources/Signal.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -392,14 +392,14 @@ private final class TerminatingState<Value, Error: Swift.Error> {
392392
fileprivate let observers: Bag<Signal<Value, Error>.Observer>
393393

394394
/// The termination event.
395-
fileprivate let event: Event<Value, Error>
395+
fileprivate let event: Signal<Value, Error>.Event
396396

397397
/// Create a terminating state.
398398
///
399399
/// - parameters:
400400
/// - observers: The latest bag of observers.
401401
/// - event: The termination event.
402-
init(observers: Bag<Signal<Value, Error>.Observer>, event: Event<Value, Error>) {
402+
init(observers: Bag<Signal<Value, Error>.Observer>, event: Signal<Value, Error>.Event) {
403403
self.observers = observers
404404
self.event = event
405405
}
@@ -574,7 +574,7 @@ extension Signal {
574574
/// - returns: A signal that forwards the values passing the given closure.
575575
public func filter(_ isIncluded: @escaping (Value) -> Bool) -> Signal<Value, Error> {
576576
return Signal { observer in
577-
return self.observe { (event: Event<Value, Error>) -> Void in
577+
return self.observe { (event: Event) -> Void in
578578
guard let value = event.value else {
579579
observer.action(event)
580580
return
@@ -595,7 +595,7 @@ extension Signal {
595595
/// - returns: A signal that will send new values, that are non `nil` after the transformation.
596596
public func filterMap<U>(_ transform: @escaping (Value) -> U?) -> Signal<U, Error> {
597597
return Signal<U, Error> { observer in
598-
return self.observe { (event: Event<Value, Error>) -> Void in
598+
return self.observe { (event: Event) -> Void in
599599
switch event {
600600
case let .value(value):
601601
if let mapped = transform(value) {
@@ -1019,8 +1019,8 @@ extension Signal {
10191019
/// the Event itself and then interrupt.
10201020
///
10211021
/// - returns: A signal that sends events as its values.
1022-
public func materialize() -> Signal<Event<Value, Error>, NoError> {
1023-
return Signal<Event<Value, Error>, NoError> { observer in
1022+
public func materialize() -> Signal<Event, NoError> {
1023+
return Signal<Event, NoError> { observer in
10241024
return self.observe { event in
10251025
observer.send(value: event)
10261026

@@ -1081,7 +1081,7 @@ extension Signal {
10811081
///
10821082
/// - returns: A signal with attached side-effects for given event cases.
10831083
public func on(
1084-
event: ((Event<Value, Error>) -> Void)? = nil,
1084+
event: ((Event) -> Void)? = nil,
10851085
failed: ((Error) -> Void)? = nil,
10861086
completed: (() -> Void)? = nil,
10871087
interrupted: (() -> Void)? = nil,
@@ -1887,7 +1887,7 @@ extension Signal {
18871887
}
18881888

18891889
disposable += self.observe { event in
1890-
let eventToSend = state.modify { state -> Event<Value, Error>? in
1890+
let eventToSend = state.modify { state -> Event? in
18911891
switch event {
18921892
case let .value(value):
18931893
switch state {

0 commit comments

Comments
 (0)