-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
178 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import ReactiveSwift | ||
|
||
extension Property { | ||
public convenience init<Event>( | ||
initial: Value, | ||
reduce: @escaping (Value, Event) -> Value, | ||
feedbacks: [Feedback<Value, Event>] | ||
) { | ||
let state = MutableProperty(initial) | ||
StateMachine.bootstrap(state: state.producer, | ||
process: { [weak state] event in state?.modify { $0 = reduce($0, event) } }, | ||
feedbacks: feedbacks, | ||
lifetime: state.lifetime) | ||
self.init(capturing: state) | ||
} | ||
|
||
public convenience init<Event>( | ||
initial: Value, | ||
reduce: @escaping (Value, Event) -> Value, | ||
feedbacks: Feedback<Value, Event>... | ||
) { | ||
self.init(initial: initial, reduce: reduce, feedbacks: feedbacks) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import ReactiveSwift | ||
import Result | ||
|
||
/// An `EventBuffer` manages the deferred processing of recursive events. | ||
/// | ||
/// When a given event is processed by a reducer, a new state would be computed together | ||
/// with new feedbacks, which could potentially yield a new event synchronously. Since | ||
/// the ReactiveSwift primitives do not support value recursion, the state machine would | ||
/// need to specifically cater for this scenario so as to be synchronous by default. | ||
private struct EventBuffer<Event> { | ||
var isTransitioning: Bool | ||
var events: ContiguousArray<Event> | ||
|
||
/// Dequeue the first event in the queue, or complete the transitioning if the queue | ||
/// is empty. | ||
mutating func completeOrDequeue() -> Event? { | ||
assert(isTransitioning) | ||
guard events.isEmpty | ||
else { return events.removeFirst() } | ||
isTransitioning = false | ||
return nil | ||
} | ||
} | ||
|
||
internal enum StateMachine { | ||
internal static func bootstrap<State, Event>( | ||
state: SignalProducer<State, NoError>, | ||
process: @escaping (Event) -> Void, | ||
feedbacks: [Feedback<State, Event>], | ||
lifetime: Lifetime | ||
) { | ||
// Treat initialisation as an implicit event, and buffer any event yielded during | ||
// the initialisation. | ||
let buffer = Atomic(EventBuffer<Event>(isTransitioning: true, events: [])) | ||
|
||
state.startWithSignal { state, interruptHandle in | ||
lifetime += interruptHandle | ||
|
||
let events = feedbacks.map { feedback in | ||
return feedback.events(ImmediateScheduler(), state) | ||
} | ||
|
||
lifetime += Signal.merge(events) | ||
.observeValues { event in | ||
let shouldProceed: Bool = buffer.modify { buffer in | ||
guard buffer.isTransitioning else { | ||
// This event is not recursively sent during the processing | ||
// of another event. | ||
buffer.isTransitioning = true | ||
return true | ||
} | ||
|
||
buffer.events.append(event) | ||
return false | ||
} | ||
|
||
guard shouldProceed else { return } | ||
|
||
process(event) | ||
|
||
// Drain any event recursively yielded during `process(event)` above. | ||
while let event = buffer.modify({ $0.completeOrDequeue() }) { | ||
process(event) | ||
} | ||
} | ||
} | ||
|
||
// Drain any event yielded by the initial state. | ||
while let event = buffer.modify({ $0.completeOrDequeue() }) { | ||
process(event) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters