Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd9e8ded

Browse files
akaegiAndeas Kägi
and
Andeas Kägi
authored
ObserveAction with current state (#95)
* Pass state to overload of ObserveAction* New ObserveAction overload unit testCo-authored-by: Andeas Kägi <andreas.kaegi@scs.ch>
1 parent35c8a55 commitd9e8ded

File tree

4 files changed

+82
-17
lines changed

4 files changed

+82
-17
lines changed

‎ReduxSimple.Tests/ObserveActionTest.cs‎

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
usingReduxSimple.Tests.Setup.TodoListStore;
22
usingShouldly;
33
usingSystem;
4+
usingSystem.Reactive.Concurrency;
45
usingSystem.Threading.Tasks;
56
usingXunit;
67
usingstaticReduxSimple.Tests.Setup.TodoListStore.Functions;
@@ -11,13 +12,14 @@ namespace ReduxSimple.Tests
1112
publicclassObserveActionTest
1213
{
1314
[Fact]
14-
publicasyncTaskCanObserveActions()
15+
publicvoidCanObserveActions()
1516
{
1617
// Arrange
1718
varinitialState=CreateInitialTodoListState();
1819
varstore=newTodoListStore(
1920
Setup.TodoListStore.Reducers.CreateReducers(),
20-
initialState
21+
initialState,
22+
dispatchedActionScheduler:Scheduler.Immediate
2123
);
2224

2325
// Act
@@ -32,22 +34,21 @@ public async Task CanObserveActions()
3234
});
3335

3436
DispatchAllActions(store);
35-
36-
awaitTask.Delay(100);
37-
37+
3838
// Assert
3939
observeCount.ShouldBe(4);
4040
lastAction.ShouldBeOfType<AddTodoItemAction>();
4141
}
4242

4343
[Fact]
44-
publicasyncTaskCanObserveSingleActionType()
44+
publicvoidCanObserveSingleActionType()
4545
{
4646
// Arrange
4747
varinitialState=CreateInitialTodoListState();
4848
varstore=newTodoListStore(
4949
Setup.TodoListStore.Reducers.CreateReducers(),
50-
initialState
50+
initialState,
51+
dispatchedActionScheduler:Scheduler.Immediate
5152
);
5253

5354
// Act
@@ -62,12 +63,47 @@ public async Task CanObserveSingleActionType()
6263
});
6364

6465
DispatchAllActions(store);
65-
66-
awaitTask.Delay(100);
67-
66+
6867
// Assert
6968
observeCount.ShouldBe(1);
7069
lastAction.ShouldBeOfType<SwitchUserAction>();
7170
}
71+
72+
[Fact]
73+
publicvoidCanObserveActionAndStateWhenDispatchedIsPassed()
74+
{
75+
// Arrange
76+
varinitialState=CreateInitialTodoListState();
77+
varstore=newTodoListStore(
78+
Setup.TodoListStore.Reducers.CreateReducers(),
79+
initialState,
80+
dispatchedActionScheduler:Scheduler.Immediate
81+
);
82+
83+
TodoListState?stateBeforeAction=null;
84+
store.ObserveAction<AddTodoItemAction,ActionStatePair>((action,state)=>newActionStatePair
85+
{
86+
Action=action,State=state
87+
})
88+
.Subscribe(x=>
89+
{
90+
// Assert
91+
x.State.ShouldBe(stateBeforeAction);
92+
});
93+
94+
// Act 1
95+
stateBeforeAction=initialState;
96+
DispatchAddTodoItemAction(store,1,"Create unit tests");
97+
98+
stateBeforeAction=store.State;
99+
DispatchAddTodoItemAction(store,2,"Create Models");
100+
}
101+
102+
classActionStatePair
103+
{
104+
publicobject?Action{get;set;}
105+
106+
publicobject?State{get;set;}
107+
}
72108
}
73109
}

‎ReduxSimple/ActionDispatched.cs‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,22 @@
33
internalabstractclassActionDispatched
44
{
55
publicobjectAction{get;}
6+
7+
publicobjectStateWhenDispatched{get;}
68

7-
protectedActionDispatched(objectaction)
9+
protectedActionDispatched(objectaction,objectstate)
810
{
911
Action=action;
12+
StateWhenDispatched=state;
1013
}
1114
}
1215

1316
internalsealedclassActionDispatchedWithOrigin:ActionDispatched
1417
{
1518
publicActionOriginOrigin{get;}
16-
17-
publicActionDispatchedWithOrigin(objectaction,ActionOriginorigin):base(action)
19+
20+
publicActionDispatchedWithOrigin(objectaction,objectstate,ActionOriginorigin)
21+
:base(action,state)
1822
{
1923
Origin=origin;
2024
}
@@ -24,7 +28,8 @@ internal sealed class ActionDispatchedWithRewriteHistory : ActionDispatched
2428
{
2529
publicboolRewriteHistory{get;}
2630

27-
publicActionDispatchedWithRewriteHistory(objectaction,boolrewriteHistory):base(action)
31+
publicActionDispatchedWithRewriteHistory(objectaction,objectstate,boolrewriteHistory)
32+
:base(action,state)
2833
{
2934
RewriteHistory=rewriteHistory;
3035
}

‎ReduxSimple/ReduxStore.Dispatch.cs‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
usingSystem;
22
usingSystem.Diagnostics;
3+
usingSystem.Linq;
34
usingSystem.Reactive.Linq;
45
usingSystem.Reactive.Subjects;
56

@@ -31,11 +32,11 @@ public void Dispatch(object action)
3132

3233
if(TimeTravelEnabled)
3334
{
34-
_toDispatchSubject.OnNext(newActionDispatchedWithRewriteHistory(action,true));
35+
_toDispatchSubject.OnNext(newActionDispatchedWithRewriteHistory(action,State,true));
3536
}
3637
else
3738
{
38-
_toDispatchSubject.OnNext(newActionDispatchedWithOrigin(action,ActionOrigin.Normal));
39+
_toDispatchSubject.OnNext(newActionDispatchedWithOrigin(action,State,ActionOrigin.Normal));
3940
}
4041
}
4142

@@ -66,6 +67,7 @@ private void ExecuteDispatch(ActionDispatchedWithRewriteHistory actionWithRewrit
6667
ExecuteDispatch(
6768
newActionDispatchedWithOrigin(
6869
actionWithRewriteHistory.Action,
70+
actionWithRewriteHistory.StateWhenDispatched,
6971
actionWithRewriteHistory.RewriteHistory?ActionOrigin.Normal:ActionOrigin.Redone
7072
)
7173
);
@@ -97,5 +99,27 @@ public IObservable<T> ObserveAction<T>(ActionOriginFilter filter = ActionOriginF
9799
.Select(x=>x.Action)
98100
.OfType<T>();
99101
}
102+
103+
/// <summary>
104+
/// Observes actions of a specific type being performed on the store.
105+
/// </summary>
106+
/// <typeparam name="TAction">The type of actions that the subscriber is interested in.</typeparam>
107+
/// <typeparam name="TResult">The type of objects in the returned observable.</typeparam>
108+
/// <param name="resultSelector">Builder for objects in the result observe being passed the action and state when the action was dispatched.</param>
109+
/// <param name="filter">Filter action by origin.</param>
110+
/// <returns>
111+
/// An <see cref="IObservable{T}"/> that can be subscribed to in order to receive updates whenever an action of <typeparamref name="TAction"/> is performed on the store.
112+
/// </returns>
113+
publicIObservable<TResult>ObserveAction<TAction,TResult>(
114+
Func<TAction,TState,TResult>resultSelector,
115+
ActionOriginFilterfilter=ActionOriginFilter.Normal)
116+
{
117+
if(resultSelector==null)
118+
thrownewArgumentNullException(nameof(resultSelector));
119+
120+
return_dispatchedAction
121+
.Where(x=>filter.HasFlag((ActionOriginFilter)x.Origin)&&x.ActionisTAction)
122+
.Select(x=>resultSelector((TAction)x.Action,(TState)x.StateWhenDispatched));
123+
}
100124
}
101125
}

‎ReduxSimple/ReduxStore.TimeTravel.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public bool Redo()
8282
returnfalse;
8383
}
8484

85-
ExecuteDispatch(newActionDispatchedWithRewriteHistory(_futureActions.Pop(),false));
85+
ExecuteDispatch(newActionDispatchedWithRewriteHistory(_futureActions.Pop(),State,false));
8686

8787
returntrue;
8888
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp