Skip to content

Commit b33dc3b

Browse files
committed
fix: prevent unnecessary subscriptions when using CombineSelector
1 parent 5fe2ae9 commit b33dc3b

File tree

11 files changed

+66
-105
lines changed

11 files changed

+66
-105
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ Store.Select(SelectIsPageSelected, "mainPage")
283283

284284
### Combine selectors
285285

286-
Sometimes, you need to consume multiple selectors. In some cases, you just want to combine them. This is what you can do with `CombineSelectors` function. It uses `CombineLatest` operator of the Rx.NET library. Here is an example:
286+
Sometimes, you need to consume multiple selectors. In some cases, you just want to combine them. This is what you can do with `CombineSelectors` function. Here is an example:
287287

288288
```csharp
289289
Store.Select(

ReduxSimple.DevTools/ReduxSimple.DevTools.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<RootNamespace>ReduxSimple.DevTools</RootNamespace>
66
<PackageId>ReduxSimple.DevTools</PackageId>
7-
<Version>3.3.0-preview001</Version>
7+
<Version>3.4.0-preview001</Version>
88
<Authors>David Bottiau</Authors>
99
<Title>ReduxSimple DevTools</Title>
1010
<Description>Simple Stupid Redux Store using Reactive Extensions</Description>

ReduxSimple.Entity/ReduxSimple.Entity.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<RootNamespace>ReduxSimple.Entity</RootNamespace>
66
<PackageId>ReduxSimple.Entity</PackageId>
7-
<Version>3.3.0-preview001</Version>
7+
<Version>3.4.0-preview001</Version>
88
<Authors>David Bottiau</Authors>
99
<Title>ReduxSimple Entity Management</Title>
1010
<Description>Simple Stupid Redux Store using Reactive Extensions</Description>

ReduxSimple.Tests.Setup/TodoListStore/Actions.cs

+2
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ public class SwitchUserAction
99
{
1010
public string? NewUser { get; set; }
1111
}
12+
13+
public class ResetStateAction { }
1214
}

ReduxSimple.Tests.Setup/TodoListStore/Functions.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ public static TodoListState CreateInitialTodoListState()
3232
NewUser = newUser
3333
});
3434
}
35-
35+
36+
public static void DispatchResetAction<T>(ReduxStore<T> store) where T : class, new()
37+
{
38+
store.Dispatch(new ResetStateAction());
39+
}
40+
3641
public static void DispatchAllActions<T>(ReduxStore<T> store) where T : class, new()
3742
{
3843
DispatchAddTodoItemAction(store, 1, "Create unit tests");

ReduxSimple.Tests.Setup/TodoListStore/Reducers.cs

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Converto;
22
using System.Collections.Generic;
33
using static ReduxSimple.Reducers;
4+
using static ReduxSimple.Tests.Setup.TodoListStore.Functions;
45

56
namespace ReduxSimple.Tests.Setup.TodoListStore
67
{
@@ -31,6 +32,9 @@ public static IEnumerable<On<TodoListState>> CreateReducers()
3132
CurrentUser = action.NewUser
3233
}
3334
)
35+
),
36+
On<ResetStateAction, TodoListState>(
37+
(state, action) => CreateInitialTodoListState()
3438
)
3539
};
3640
}

ReduxSimple.Tests/SelectorTest.cs

+34
Original file line numberDiff line numberDiff line change
@@ -228,5 +228,39 @@ public void CanCombineSelectorsWithOneUpdatedPropertyAndOneNonUpdateProperty()
228228
Assert.Equal(2, lastPartialState.todoList?.Count);
229229
Assert.Null(lastPartialState.uselessProperty);
230230
}
231+
232+
[Fact]
233+
public void CanCombineSelectorsWithSynchronousStateUpdates()
234+
{
235+
// Arrange
236+
var initialState = CreateInitialTodoListState();
237+
var store = new TodoListStore(
238+
Setup.TodoListStore.Reducers.CreateReducers(),
239+
initialState
240+
);
241+
242+
// Act
243+
int observeCount = 0;
244+
(IImmutableList<TodoItem>? todoList, string? currentUser) lastPartialState = (null, null);
245+
246+
store.Select(
247+
CombineSelectors(SelectTodoList, SelectCurrentUser)
248+
)
249+
.Subscribe(x =>
250+
{
251+
var (todolist, currentUser) = x;
252+
253+
observeCount++;
254+
lastPartialState = (todolist, currentUser);
255+
});
256+
257+
DispatchAllActions(store);
258+
DispatchResetAction(store);
259+
260+
// Assert
261+
Assert.Equal(6, observeCount);
262+
Assert.Equal(0, lastPartialState.todoList?.Count);
263+
Assert.Equal("David", lastPartialState.currentUser);
264+
}
231265
}
232266
}

ReduxSimple.Uwp.DevTools/ReduxSimple.Uwp.DevTools.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<TargetFramework>uap10.0.18362</TargetFramework>
44
<RootNamespace>ReduxSimple.Uwp.DevTools</RootNamespace>
55
<PackageId>ReduxSimple.Uwp.DevTools</PackageId>
6-
<Version>3.3.0-preview001</Version>
6+
<Version>3.4.0-preview001</Version>
77
<Authors>David Bottiau</Authors>
88
<Title>ReduxSimple DevTools for UWP</Title>
99
<Description>Simple Stupid Redux Store using Reactive Extensions - DevTools for UWP applications</Description>

ReduxSimple.Uwp.RouterStore/ReduxSimple.Uwp.RouterStore.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<TargetFramework>uap10.0.16299</TargetFramework>
44
<RootNamespace>ReduxSimple.Uwp.RouterStore</RootNamespace>
55
<PackageId>ReduxSimple.Uwp.RouterStore</PackageId>
6-
<Version>3.3.0-preview001</Version>
6+
<Version>3.4.0-preview001</Version>
77
<Authors>David Bottiau</Authors>
88
<Title>ReduxSimple Router Store for UWP</Title>
99
<Description>Simple Stupid Redux Store using Reactive Extensions - Binding between Store and Routing in UWP applications</Description>

ReduxSimple/ReduxSimple.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<PackageId>ReduxSimple</PackageId>
6-
<Version>3.3.0</Version>
6+
<Version>3.4.0</Version>
77
<Authors>David Bottiau</Authors>
88
<Description>Simple Stupid Redux Store using Reactive Extensions</Description>
99
<PackageProjectUrl>https://github.com/Odonno/ReduxSimple</PackageProjectUrl>

ReduxSimple/Selector.Memoized.cs

+14-98
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public TOutput Apply(TInput input)
3636
}
3737
public IObservable<TOutput> Apply(IObservable<TInput> input)
3838
{
39-
return Selector.Apply(input)
40-
.Select(ProjectorFunction)
39+
return input
40+
.Select(Apply)
4141
.DistinctUntilChanged();
4242
}
4343
}
@@ -86,17 +86,8 @@ public TOutput Apply(TInput input)
8686
}
8787
public IObservable<TOutput> Apply(IObservable<TInput> input)
8888
{
89-
return Observable.CombineLatest(
90-
Selector1.Apply(input),
91-
Selector2.Apply(input),
92-
Tuple.Create
93-
)
94-
.Select(x =>
95-
ProjectorFunction(
96-
x.Item1,
97-
x.Item2
98-
)
99-
)
89+
return input
90+
.Select(Apply)
10091
.DistinctUntilChanged();
10192
}
10293
}
@@ -154,19 +145,8 @@ public TOutput Apply(TInput input)
154145
}
155146
public IObservable<TOutput> Apply(IObservable<TInput> input)
156147
{
157-
return Observable.CombineLatest(
158-
Selector1.Apply(input),
159-
Selector2.Apply(input),
160-
Selector3.Apply(input),
161-
Tuple.Create
162-
)
163-
.Select(x =>
164-
ProjectorFunction(
165-
x.Item1,
166-
x.Item2,
167-
x.Item3
168-
)
169-
)
148+
return input
149+
.Select(Apply)
170150
.DistinctUntilChanged();
171151
}
172152
}
@@ -233,21 +213,8 @@ public TOutput Apply(TInput input)
233213
}
234214
public IObservable<TOutput> Apply(IObservable<TInput> input)
235215
{
236-
return Observable.CombineLatest(
237-
Selector1.Apply(input),
238-
Selector2.Apply(input),
239-
Selector3.Apply(input),
240-
Selector4.Apply(input),
241-
Tuple.Create
242-
)
243-
.Select(x =>
244-
ProjectorFunction(
245-
x.Item1,
246-
x.Item2,
247-
x.Item3,
248-
x.Item4
249-
)
250-
)
216+
return input
217+
.Select(Apply)
251218
.DistinctUntilChanged();
252219
}
253220
}
@@ -323,23 +290,8 @@ public TOutput Apply(TInput input)
323290
}
324291
public IObservable<TOutput> Apply(IObservable<TInput> input)
325292
{
326-
return Observable.CombineLatest(
327-
Selector1.Apply(input),
328-
Selector2.Apply(input),
329-
Selector3.Apply(input),
330-
Selector4.Apply(input),
331-
Selector5.Apply(input),
332-
Tuple.Create
333-
)
334-
.Select(x =>
335-
ProjectorFunction(
336-
x.Item1,
337-
x.Item2,
338-
x.Item3,
339-
x.Item4,
340-
x.Item5
341-
)
342-
)
293+
return input
294+
.Select(Apply)
343295
.DistinctUntilChanged();
344296
}
345297
}
@@ -424,25 +376,8 @@ public TOutput Apply(TInput input)
424376
}
425377
public IObservable<TOutput> Apply(IObservable<TInput> input)
426378
{
427-
return Observable.CombineLatest(
428-
Selector1.Apply(input),
429-
Selector2.Apply(input),
430-
Selector3.Apply(input),
431-
Selector4.Apply(input),
432-
Selector5.Apply(input),
433-
Selector6.Apply(input),
434-
Tuple.Create
435-
)
436-
.Select(x =>
437-
ProjectorFunction(
438-
x.Item1,
439-
x.Item2,
440-
x.Item3,
441-
x.Item4,
442-
x.Item5,
443-
x.Item6
444-
)
445-
)
379+
return input
380+
.Select(Apply)
446381
.DistinctUntilChanged();
447382
}
448383
}
@@ -536,27 +471,8 @@ public TOutput Apply(TInput input)
536471
}
537472
public IObservable<TOutput> Apply(IObservable<TInput> input)
538473
{
539-
return Observable.CombineLatest(
540-
Selector1.Apply(input),
541-
Selector2.Apply(input),
542-
Selector3.Apply(input),
543-
Selector4.Apply(input),
544-
Selector5.Apply(input),
545-
Selector6.Apply(input),
546-
Selector7.Apply(input),
547-
Tuple.Create
548-
)
549-
.Select(x =>
550-
ProjectorFunction(
551-
x.Item1,
552-
x.Item2,
553-
x.Item3,
554-
x.Item4,
555-
x.Item5,
556-
x.Item6,
557-
x.Item7
558-
)
559-
)
474+
return input
475+
.Select(Apply)
560476
.DistinctUntilChanged();
561477
}
562478
}

0 commit comments

Comments
 (0)