Skip to content

Commit

Permalink
added overload for EA to provide filter
Browse files Browse the repository at this point in the history
  • Loading branch information
brianlagunas committed Apr 6, 2020
1 parent 3ab9483 commit c1cdd32
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Prism.Core/Events/PubSubEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,17 @@ public SubscriptionToken Subscribe(Action<TPayload> action)
return Subscribe(action, ThreadOption.PublisherThread);
}

/// <summary>
/// Subscribes a delegate to an event that will be published on the <see cref="ThreadOption.PublisherThread"/>
/// </summary>
/// <param name="action">The delegate that gets executed when the event is raised.</param>
/// <param name="filter">Filter to evaluate if the subscriber should receive the event.</param>
/// <returns>A <see cref="SubscriptionToken"/> that uniquely identifies the added subscription.</returns>
public virtual SubscriptionToken Subscribe(Action<TPayload> action, Predicate<TPayload> filter)
{
return Subscribe(action, ThreadOption.PublisherThread, false, filter);
}

/// <summary>
/// Subscribes a delegate to an event.
/// PubSubEvent will maintain a <see cref="WeakReference"/> to the Target of the supplied <paramref name="action"/> delegate.
Expand Down
18 changes: 18 additions & 0 deletions tests/Prism.Core.Tests/Events/PubSubEventFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,24 @@ public void FilterEnablesActionTarget()

}

[Fact]
public void FilterEnablesActionTarget_Weak()
{
TestablePubSubEvent<string> pubSubEvent = new TestablePubSubEvent<string>();
var goodFilter = new MockFilter { FilterReturnValue = true };
var actionGoodFilter = new ActionHelper();
var badFilter = new MockFilter { FilterReturnValue = false };
var actionBadFilter = new ActionHelper();
pubSubEvent.Subscribe(actionGoodFilter.Action, goodFilter.FilterString);
pubSubEvent.Subscribe(actionBadFilter.Action, badFilter.FilterString);

pubSubEvent.Publish("test");

Assert.True(actionGoodFilter.ActionCalled);
Assert.False(actionBadFilter.ActionCalled);

}

[Fact]
public void SubscribeDefaultsThreadOptionAndNoFilter()
{
Expand Down

0 comments on commit c1cdd32

Please sign in to comment.