Open beeth0ven opened 5 years ago
Hi @beeth0ven , thanks for this proposal. I was thinking how you would be able to test effects/feedbacks?
Hi @eliekarouz, thanks for your interest.
Effects can be tested as before with TestScheduler
:
TestScheduler
let events = [
"tm" : Event.throwToMachine,
"th" : .throwToHuman,
]
let states = [
"h" : State.humanHasIt,
"m" : .machineHasIt
]
// 1. create `TestScheduler`
let scheduler = TestScheduler(initialClock: 0, resolution: resolution, simulateProcessingDelay: false)
// 2. create mocked effects
let mockedEffects: (PitchRequest) -> Observable<Event> = scheduler.mock(values: events) { _ -> String in
return "----th"
};
// 3. create mocked events
let (
inputEvents,
expectedStates
) = (
scheduler.parseEventsAndTimes(timeline: "------tm------tm------tm-------", values: events).first!,
scheduler.parseEventsAndTimes(timeline: "h-----m---h---m---h---m---h----", values: states).first!
)
// 4. inject mocked effects and events to system
let observableSystem = ObservableSystem.create(
initialState: State.humanHasIt,
reduce: { (state: State, event: Event) -> State in
switch event {
case .throwToMachine:
return .machineHasIt
case .throwToHuman:
return .humanHasIt
}
},
scheduler: scheduler
)
.reacted(request: { $0.machinePitching }, effects: mockedEffects)
let state = observableSystem.system([{ _ in scheduler.createHotObservable(inputEvents).asObservable() }])
let recordedState = scheduler.record(source: state)
scheduler.start()
// 5. assert output states
XCTAssertEqual(recordedState.events, expectedStates)
This example use MarbleTests which can be found in RxExample_iOSTests.
Hi there!
Long time no see, hoping every one is doing well. I missed all of you!
Things get evoluted after this proposal. I'm happy to see swift-composable-architecture use a similar pattern and become popular, that's pretty cool!
Then I tried to evolute this idea, and open source a library called love.dart π. Yeah it's written in dart since I developed flutter apps recently.
If you are still interested with this "operator pattern". Feel free to take a look. Feedback π are also welcome!
Thank you!
Best Wishes!
Overview
We can introduce
ObservableSystem
, this will make system chainable:PlayCatch Example
Before:
After:
Evolution
The solution is inspired by
Rx
. Let's get in.What do we have currently in
Rx
?I will show minimal type inferface in
Rx
, as it will help us move fast to destination:I've removed unrelate logic to make our evolution "pure".
Now we can adds some operators which are free functions:
As far as we can tell, Operator behaiver like a
Middleware
:We can change operator a little bit to:
That's what we have now in
Rx
.Port to
RxFeedback
We can find a way to port all these stuff to
RxFeedback
:What do we have in
RxFeedback
?We may add a
createSystem
function:By comparing function
system
withcreateSystem
, It's not hard to find the return type has been changed formObservable<State>
to([Feedback<State, Event>]) -> Observable<State>
.Ok. This will open a new world, let's call the new return type
System
:Then
createSystem
becomes:Next we can introduce
SystemMiddleware
:The feedback creator funtion like
react
andbind
inRxFeedback
now becomes operator:Real
Let's bring this to real.
Introduce
ObservableSystem
toRxFeedback
:The
ObservableSystem
is likeObservable
inRx
.And
reacted
,binded
is like Operators inRx
.Now the system can be chainable:
It will bring us some benefits:
ObservableSystem
Rx
With the benefits, I proposal to add this feature.
A running example can be found here with commit: introduce ObservableSystem. It also handle driver version (DriverSystem).
I'm open to disccuss π, If this is accepted, I will make a PR.
Thanks.