-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathObservableType+RxFeedback.swift
More file actions
172 lines (147 loc) · 7.03 KB
/
ObservableType+RxFeedback.swift
File metadata and controls
172 lines (147 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//
// ObservableType+Extensions.swift
// RxFeedback
//
// Created by Krunoslav Zaher on 4/30/17.
// Copyright © 2017 Krunoslav Zaher. All rights reserved.
//
import RxCocoa
import RxSwift
extension ObservableType where Element == Any {
/// Feedback loop
public typealias Feedback<State, Event> = (ObservableSchedulerContext<State>) -> Observable<Event>
public typealias FeedbackLoop = Feedback
/**
The system simulation will be started upon subscription and stopped after subscription is disposed.
System state is represented as a `State` parameter.
Events are represented by the `Event` parameter.
- parameter initialState: The initial state of the system.
- parameter reduce: Calculates the new system state from the existing state and a transition event (system integrator, reducer).
- parameter feedback: The feedback loops that produce events depending on the current system state.
- returns: The current state of the system.
*/
public static func system<State, Event>(
initialState: State,
reduce: @escaping (State, Event) -> State,
scheduler: ImmediateSchedulerType,
feedback: [Feedback<State, Event>]
) -> Observable<State> {
return Observable<State>.deferred {
let replaySubject = ReplaySubject<State>.create(bufferSize: 1)
let asyncScheduler = scheduler.async
let events: Observable<Event> = Observable.merge(
feedback.map { feedback in
let state = ObservableSchedulerContext(source: replaySubject.asObservable(), scheduler: asyncScheduler)
return feedback(state)
}
)
// This is protection from accidental ignoring of scheduler so
// reentracy errors can be avoided
.observeOn(CurrentThreadScheduler.instance)
return events.scan(initialState, accumulator: reduce)
.do(
onNext: { output in
replaySubject.onNext(output)
}, onSubscribed: {
replaySubject.onNext(initialState)
}
)
.subscribeOn(scheduler)
.startWith(initialState)
.observeOn(scheduler)
}
}
/**
The system simulation will be started upon subscription and stopped after subscription is disposed.
System state is represented as a `State` parameter.
Events are represented by the `Event` parameter.
- parameter initialState: The initial state of the system.
- parameter reduce: Calculates the new system state from the existing state and a transition event (system integrator, reducer).
- parameter feedback: The feedback loops that produce events depending on the current system state.
- returns: The current state of the system.
*/
public static func system<State, Event>(
initialState: State,
reduce: @escaping (State, Event) -> State,
scheduler: ImmediateSchedulerType,
feedback: Feedback<State, Event>...
) -> Observable<State> {
return system(initialState: initialState, reduce: reduce, scheduler: scheduler, feedback: feedback)
}
}
extension SharedSequenceConvertibleType where Element == Any, SharingStrategy == DriverSharingStrategy {
/// Feedback loop
public typealias Feedback<State, Event> = (Driver<State>) -> Signal<Event>
/**
The system simulation will be started upon subscription and stopped after subscription is disposed.
System state is represented as a `State` parameter.
Events are represented by the `Event` parameter.
- parameter initialState: The initial state of the system.
- parameter reduce: Calculates the new system state from the existing state and a transition event (system integrator, reducer).
- parameter feedback: The feedback loops that produce events depending on the current system state.
- returns: The current state of the system.
*/
public static func system<State, Event>(
initialState: State,
reduce: @escaping (State, Event) -> State,
feedback: [Feedback<State, Event>]
) -> Driver<State> {
let observableFeedbacks: [(ObservableSchedulerContext<State>) -> Observable<Event>] = feedback.map { feedback in
return { sharedSequence in
feedback(sharedSequence.source.asDriver(onErrorDriveWith: Driver<State>.empty()))
.asObservable()
}
}
return Observable<Any>.system(
initialState: initialState,
reduce: reduce,
scheduler: SharingStrategy.scheduler,
feedback: observableFeedbacks
)
.asDriver(onErrorDriveWith: .empty())
}
/**
The system simulation will be started upon subscription and stopped after subscription is disposed.
System state is represented as a `State` parameter.
Events are represented by the `Event` parameter.
- parameter initialState: The initial state of the system.
- parameter reduce: Calculates the new system state from the existing state and a transition event (system integrator, reducer).
- parameter feedback: The feedback loops that produce events depending on the current system state.
- returns: The current state of the system.
*/
public static func system<State, Event>(
initialState: State,
reduce: @escaping (State, Event) -> State,
feedback: Feedback<State, Event>...
) -> Driver<State> {
return system(initialState: initialState, reduce: reduce, feedback: feedback)
}
}
extension ImmediateSchedulerType {
var async: ImmediateSchedulerType {
// This is a hack because of reentrancy. We need to make sure events are being sent async.
// In case MainScheduler is being used MainScheduler.asyncInstance is used to make sure state is modified async.
// If there is some unknown scheduler instance (like TestScheduler), just use it.
return (self as? MainScheduler).map { _ in MainScheduler.asyncInstance } ?? self
}
}
/// Tuple of observable sequence and corresponding scheduler context on which that observable
/// sequence receives elements.
public struct ObservableSchedulerContext<Element>: ObservableType {
public typealias E = Element
/// Source observable sequence
public let source: Observable<Element>
/// Scheduler on which observable sequence receives elements
public let scheduler: ImmediateSchedulerType
/// Initializes self with source observable sequence and scheduler
///
/// - parameter source: Source observable sequence.
/// - parameter scheduler: Scheduler on which source observable sequence receives elements.
public init(source: Observable<Element>, scheduler: ImmediateSchedulerType) {
self.source = source
self.scheduler = scheduler
}
public func subscribe<Observer: ObserverType>(_ observer: Observer) -> Disposable where Observer.Element == Element {
return source.subscribe(observer)
}
}