Skip to content

Commit 1b190e9

Browse files
authored
Allow observer callback code to unsubscribe from action notifications (Fixes #84)
1 parent 118de24 commit 1b190e9

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

Docs/releases.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Releases
22

3+
### New in 3.7
4+
* Fix for ([#84](https://github.com/mrpmorris/Fluxor/issues/84) -
5+
Allow observer to unsubscribe from all subscriptions whilst executing
6+
the callback from a previous subscription
7+
38
### New in 3.6
49
* Ensure synchronous effects are executed synchronously ([#76](https://github.com/mrpmorris/fluxor/issues/76)) -
510
Reverts changes for [(#74) Endless loop redirects](https://github.com/mrpmorris/Fluxor/issues/74) as

Source/Fluxor/ActionSubscriber.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public void Notify(object action)
3434
SubscriptionsForType
3535
.Where(x => x.Key.IsAssignableFrom(action.GetType()))
3636
.SelectMany(x => x.Value)
37-
.Select(x => x.Callback);
37+
.Select(x => x.Callback)
38+
.ToArray();
3839
foreach (Action<object> callback in callbacks)
3940
callback(action);
4041
});

Tests/Fluxor.UnitTests/ActionSubscriberTests/UnsubscribeFromAllActionsTests.cs

+19
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,24 @@ public void WhenExecuted_ThenOnlyUnsubscribesTheSpecifiedSubscriber()
3838
Assert.Null(actionReceivedBySubscriber1);
3939
Assert.Same(dispatchedAction, actionReceivedBySubscriber2);
4040
}
41+
42+
[Fact]
43+
public void WhenExecutedFromSubscriptionCallback_ThenDoesNotThrowAnError()
44+
{
45+
int executionCount = 0;
46+
var subscriber = new object();
47+
var dispatchedAction = new TestAction();
48+
49+
Subject.SubscribeToAction<TestAction>(subscriber, x =>
50+
{
51+
executionCount++;
52+
Subject.UnsubscribeFromAllActions(subscriber);
53+
});
54+
55+
Subject.Dispatch(dispatchedAction);
56+
Subject.Dispatch(dispatchedAction);
57+
58+
Assert.Equal(1, executionCount);
59+
}
4160
}
4261
}

0 commit comments

Comments
 (0)