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

Commit3ec922e

Browse files
akaegiAndeas Kägi
and
Andeas Kägi
authored
Feature/custom_dispatchedactionscheduler (#89)
* Added test that highlights previous ordering problem of actions in effects (issue#87)* Added possibility to use custom dispatchedActionScheduler with ReduxStore* Use store argument in CreateEffectCo-authored-by: Andeas Kägi <andreas.kaegi@scs.ch>
1 parentc4af6b2 commit3ec922e

File tree

6 files changed

+110
-14
lines changed

6 files changed

+110
-14
lines changed

‎ReduxSimple.Tests/CreateReduxStoreTest.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void CreatingAStoreShouldDispatchInitializeAction()
4646
varstore=newTodoListStore(
4747
Setup.TodoListStore.Reducers.CreateReducers(),
4848
initialState,
49-
true
49+
enableTimeTravel:true
5050
);
5151

5252
// Act
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
usingSystem;
2+
usingSystem.Collections.Generic;
3+
usingSystem.Linq;
4+
usingSystem.Reactive.Linq;
5+
usingSystem.Threading;
6+
usingSystem.Threading.Tasks;
7+
usingShouldly;
8+
usingXunit;
9+
usingXunit.Abstractions;
10+
11+
namespaceReduxSimple.Tests
12+
{
13+
publicclassEffectsActionOrderTest
14+
{
15+
privatereadonlyITestOutputHelper_output;
16+
privatereadonlyList<string>_trackedActions=newList<string>();
17+
18+
publicEffectsActionOrderTest(ITestOutputHelperoutput)
19+
{
20+
_output=output;
21+
}
22+
23+
[Fact]
24+
publicasyncTaskEffectsSeeActionsInCorrectOrder()
25+
{
26+
ReduxStore<RootState>store=newReduxStore<RootState>(CreateReducers(),RootState.InitialState,
27+
enableTimeTravel:false);
28+
29+
store.RegisterEffects(
30+
GetIncRandomActionEffect(),
31+
TrackActionEffect());
32+
33+
_output.WriteLine($"Main thread:{Thread.CurrentThread.ManagedThreadId}");
34+
35+
store.Dispatch(newIncRandomAction());
36+
37+
awaitTask.Delay(100);
38+
39+
_trackedActions.ShouldBe(new[]
40+
{
41+
"IncRandomAction",
42+
"IncRandomFulfilledAction"
43+
});
44+
}
45+
46+
privateIEnumerable<On<RootState>>CreateReducers()
47+
{
48+
yieldreturnReducers.On<IncRandomAction,RootState>(
49+
(state,_)=>state);
50+
51+
yieldreturnReducers.On<IncRandomFulfilledAction,RootState>(
52+
(state,action)=>state.WithCounter(action.RandomValue));
53+
}
54+
55+
privateEffect<RootState>GetIncRandomActionEffect()=>
56+
Effects.CreateEffect<RootState>(
57+
store=>store.ObserveAction<IncRandomAction>()
58+
.Do(_=>_output.WriteLine($"GetIncRandomActionEffect on thread{Thread.CurrentThread.ManagedThreadId}"))
59+
.Select(_=>newIncRandomFulfilledAction{RandomValue=newRandom().Next()}),
60+
dispatch:true);
61+
62+
privateEffect<RootState>TrackActionEffect()=>
63+
Effects.CreateEffect<RootState>(
64+
store=>store.ObserveAction()
65+
.Do(action=>
66+
{
67+
stringactionType=action.GetType().Name;
68+
_output.WriteLine($"TrackActionEffect with action{actionType} on thread{Thread.CurrentThread.ManagedThreadId}");
69+
_trackedActions.Add(actionType);
70+
}),
71+
false);
72+
73+
classRootState
74+
{
75+
intCounter{get;set;}
76+
77+
publicstaticRootStateInitialState=>newRootState{Counter=0};
78+
79+
publicRootStateWithCounter(intval)
80+
{
81+
returnnewRootState{Counter=val};
82+
}
83+
}
84+
85+
classIncRandomAction
86+
{
87+
}
88+
89+
classIncRandomFulfilledAction
90+
{
91+
publicintRandomValue{get;set;}
92+
}
93+
}
94+
}

‎ReduxSimple.Tests/RedoTest.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void CanRedo()
1717
varstore=newTodoListStore(
1818
Setup.TodoListStore.Reducers.CreateReducers(),
1919
initialState,
20-
true
20+
enableTimeTravel:true
2121
);
2222

2323
// Act
@@ -56,7 +56,7 @@ public void CannotRedo()
5656
varstore=newTodoListStore(
5757
Setup.TodoListStore.Reducers.CreateReducers(),
5858
initialState,
59-
true
59+
enableTimeTravel:true
6060
);
6161

6262
// Act
@@ -74,7 +74,7 @@ public async Task RedoAndObserveNormalTimelineActionsOnly()
7474
varstore=newTodoListStore(
7575
Setup.TodoListStore.Reducers.CreateReducers(),
7676
initialState,
77-
true
77+
enableTimeTravel:true
7878
);
7979

8080
// Act
@@ -108,7 +108,7 @@ public async Task RedoAndObserveRedoneActionsOnly()
108108
varstore=newTodoListStore(
109109
Setup.TodoListStore.Reducers.CreateReducers(),
110110
initialState,
111-
true
111+
enableTimeTravel:true
112112
);
113113

114114
// Act

‎ReduxSimple.Tests/ResetTest.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public void CanResetStoreWithUndoneActions()
1818
varstore=newTodoListStore(
1919
Setup.TodoListStore.Reducers.CreateReducers(),
2020
initialState,
21-
true
21+
enableTimeTravel:true
2222
);
2323

2424
// Act
@@ -56,7 +56,7 @@ public void CanResetStore()
5656
varstore=newTodoListStore(
5757
Setup.TodoListStore.Reducers.CreateReducers(),
5858
initialState,
59-
true
59+
enableTimeTravel:true
6060
);
6161

6262
// Act
@@ -88,7 +88,7 @@ public async Task ObserveHistoryOnReset()
8888
varstore=newTodoListStore(
8989
Setup.TodoListStore.Reducers.CreateReducers(),
9090
initialState,
91-
true
91+
enableTimeTravel:true
9292
);
9393

9494
// Act

‎ReduxSimple.Tests/UndoTest.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void CannotUndoIfNoActionDispatched()
1717
varstore=newTodoListStore(
1818
Setup.TodoListStore.Reducers.CreateReducers(),
1919
initialState,
20-
true
20+
enableTimeTravel:true
2121
);
2222

2323
// Act
@@ -37,7 +37,7 @@ public void CanUndoAndObserveUndoneActions()
3737
varstore=newTodoListStore(
3838
Setup.TodoListStore.Reducers.CreateReducers(),
3939
initialState,
40-
true
40+
enableTimeTravel:true
4141
);
4242

4343
// Act
@@ -76,7 +76,7 @@ public void CanUndoAndObserveUndoneActionsOfASingleType()
7676
varstore=newTodoListStore(
7777
Setup.TodoListStore.Reducers.CreateReducers(),
7878
initialState,
79-
true
79+
enableTimeTravel:true
8080
);
8181

8282
// Act
@@ -119,7 +119,7 @@ public void CannotUndo()
119119
varstore=newTodoListStore(
120120
Setup.TodoListStore.Reducers.CreateReducers(),
121121
initialState,
122-
true
122+
enableTimeTravel:true
123123
);
124124

125125
// Act

‎ReduxSimple/ReduxStore.Core.cs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ public ReduxStore(
3939
/// <param name="reducers">A list of reducers to update state when an action is triggered.</param>
4040
/// <param name="initialState">The initial state to put the store in; if <c>null</c>, a default value is constructed using <c>new TState()</c>.</param>
4141
/// <param name="enableTimeTravel">Enable time-travel operations (undo, redo).</param>
42+
/// <param name="dispatchedActionScheduler">Dispatched actions will be dispatched using the given scheduler. Defaults to <c>Scheduler.Default</c>.</param>
4243
publicReduxStore(
4344
IEnumerable<On<TState>>reducers,
4445
TState?initialState,
45-
boolenableTimeTravel=false
46+
boolenableTimeTravel=false,
47+
IScheduler?dispatchedActionScheduler=null
4648
)
4749
{
4850
AddReducers(reducers.ToArray());
@@ -51,7 +53,7 @@ public ReduxStore(
5153
TimeTravelEnabled=enableTimeTravel;
5254

5355
_dispatchedAction=_dispatchedSubject
54-
.ObserveOn(Scheduler.Default)
56+
.ObserveOn(dispatchedActionScheduler??Scheduler.Default)
5557
.Publish();
5658
_dispatchedAction.Connect();
5759

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp