/// <summary> /// Subscribes a delegate to an event. /// </summary> /// <param name="action">The delegate that gets executed when the event is published.</param> /// <param name="threadOption">Specifies on which thread to receive the delegate callback.</param> /// <param name="keepSubscriberReferenceAlive">When <see langword="true"/>, the <see cref="PubSubEvent"/> keeps a reference to the subscriber so it does not get garbage collected.</param> /// <returns>A <see cref="SubscriptionToken"/> that uniquely identifies the added subscription.</returns> /// <remarks> /// If <paramref name="keepSubscriberReferenceAlive"/> is set to <see langword="false" />, <see cref="PubSubEvent"/> will maintain a <see cref="WeakReference"/> to the Target of the supplied <paramref name="action"/> delegate. /// If not using a WeakReference (<paramref name="keepSubscriberReferenceAlive"/> is <see langword="true" />), the user must explicitly call Unsubscribe for the event when disposing the subscriber in order to avoid memory leaks or unexpected behavior. /// <para/> /// The PubSubEvent collection is thread-safe. /// </remarks> public virtual SubscriptionToken Subscribe(Action action, ThreadOption threadOption, bool keepSubscriberReferenceAlive) { IDelegateReference actionReference = new DelegateReference(action, keepSubscriberReferenceAlive); EventSubscription subscription; switch (threadOption) { case ThreadOption.PublisherThread: subscription = new EventSubscription(actionReference); break; case ThreadOption.BackgroundThread: subscription = new BackgroundEventSubscription(actionReference); break; case ThreadOption.UIThread: if (SynchronizationContext == null) { throw new InvalidOperationException(Resources.EventAggregatorNotConstructedOnUIThread); } subscription = new DispatcherEventSubscription(actionReference, SynchronizationContext); break; default: subscription = new EventSubscription(actionReference); break; } return(InternalSubscribe(subscription)); }
/// <summary> /// Subscribes a delegate to an event. /// </summary> /// <param name="action">The delegate that gets executed when the event is published.</param> /// <param name="threadOption">Specifies on which thread to receive the delegate callback.</param> /// <param name="keepSubscriberReferenceAlive">When <see langword="true"/>, the <see cref="PubSubEvent"/> keeps a reference to the subscriber so it does not get garbage collected.</param> /// <returns>A <see cref="SubscriptionToken"/> that uniquely identifies the added subscription.</returns> /// <remarks> /// If <paramref name="keepSubscriberReferenceAlive"/> is set to <see langword="false" />, <see cref="PubSubEvent"/> will maintain a <see cref="WeakReference"/> to the Target of the supplied <paramref name="action"/> delegate. /// If not using a WeakReference (<paramref name="keepSubscriberReferenceAlive"/> is <see langword="true" />), the user must explicitly call Unsubscribe for the event when disposing the subscriber in order to avoid memory leaks or unexpected behavior. /// <para/> /// The PubSubEvent collection is thread-safe. /// </remarks> public SubscriptionToken Subscribe(Action action, ThreadOption threadOption, bool keepSubscriberReferenceAlive) { IDelegateReference actionReference = new DelegateReference(action, keepSubscriberReferenceAlive); EventSubscription subscription; switch (threadOption) { case ThreadOption.PublisherThread: subscription = new EventSubscription(actionReference); break; case ThreadOption.BackgroundThread: subscription = new BackgroundEventSubscription(actionReference); break; case ThreadOption.UIThread: if (SynchronizationContext == null) throw new InvalidOperationException(Resources.EventAggregatorNotConstructedOnUIThread); subscription = new DispatcherEventSubscription(actionReference, SynchronizationContext); break; default: subscription = new EventSubscription(actionReference); break; } return base.InternalSubscribe(subscription); }
public void ShouldPassParametersCorrectly() { IDelegateReference actionDelegateReference = new MockDelegateReference() { Target = (Action<object>)(arg1 => { return; }) }; IDelegateReference filterDelegateReference = new MockDelegateReference { Target = (Predicate<object>)(arg => true) }; var mockSyncContext = new MockSynchronizationContext(); DispatcherEventSubscription<object> eventSubscription = new DispatcherEventSubscription<object>(actionDelegateReference, filterDelegateReference, mockSyncContext); var executionStrategy = eventSubscription.GetExecutionStrategy(); Assert.IsNotNull(executionStrategy); object argument1 = new object(); executionStrategy.Invoke(new[] { argument1 }); Assert.AreSame(argument1, mockSyncContext.InvokeArg); }
public void ShouldCallInvokeOnDispatcherNonGeneric() { DispatcherEventSubscription eventSubscription = null; IDelegateReference actionDelegateReference = new MockDelegateReference() { Target = (Action)(() => { }) }; var mockSyncContext = new MockSynchronizationContext(); eventSubscription = new DispatcherEventSubscription(actionDelegateReference, mockSyncContext); eventSubscription.GetExecutionStrategy().Invoke(new object[0]); Assert.True(mockSyncContext.InvokeCalled); }
/// <summary> /// Subscribes a delegate to an event. /// </summary> /// <param name="action">The delegate that gets executed when the event is published.</param> /// <param name="threadOption">Specifies on which thread to receive the delegate callback.</param> /// <param name="keepSubscriberReferenceAlive">When <see langword="true"/>, the <see cref="PubSubEvent{TPayload}"/> keeps a reference to the subscriber so it does not get garbage collected.</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> /// <remarks> /// If <paramref name="keepSubscriberReferenceAlive"/> is set to <see langword="false" />, <see cref="PubSubEvent{TPayload}"/> will maintain a <see cref="WeakReference"/> to the Target of the supplied <paramref name="action"/> delegate. /// If not using a WeakReference (<paramref name="keepSubscriberReferenceAlive"/> is <see langword="true" />), the user must explicitly call Unsubscribe for the event when disposing the subscriber in order to avoid memory leaks or unexpected behavior. /// /// The PubSubEvent collection is thread-safe. /// </remarks> public virtual SubscriptionToken Subscribe(Action <TPayload> action, ThreadOption threadOption, bool keepSubscriberReferenceAlive, Predicate <TPayload> filter) { IDelegateReference actionReference = new DelegateReference(action, keepSubscriberReferenceAlive); IDelegateReference filterReference; if (filter != null) { filterReference = new DelegateReference(filter, keepSubscriberReferenceAlive); } else { filterReference = new DelegateReference(new Predicate <TPayload>(delegate { return(true); }), true); } EventSubscription <TPayload> subscription; switch (threadOption) { case ThreadOption.PublisherThread: subscription = new EventSubscription <TPayload>(actionReference, filterReference); break; case ThreadOption.BackgroundThread: subscription = new BackgroundEventSubscription <TPayload>(actionReference, filterReference); break; case ThreadOption.UIThread: if (SynchronizationContext == null) { throw new InvalidOperationException(Resources.EventAggregatorNotConstructedOnUIThread); } subscription = new DispatcherEventSubscription <TPayload>(actionReference, filterReference, SynchronizationContext); break; default: subscription = new EventSubscription <TPayload>(actionReference, filterReference); break; } return(base.InternalSubscribe(subscription)); }
public void ShouldCallInvokeOnDispatcher() { DispatcherEventSubscription<object> eventSubscription = null; IDelegateReference actionDelegateReference = new MockDelegateReference() { Target = (Action<object>)(arg => { return; }) }; IDelegateReference filterDelegateReference = new MockDelegateReference { Target = (Predicate<object>)(arg => true) }; var mockSyncContext = new MockSynchronizationContext(); eventSubscription = new DispatcherEventSubscription<object>(actionDelegateReference, filterDelegateReference, mockSyncContext); eventSubscription.GetExecutionStrategy().Invoke(new object[0]); Assert.IsTrue(mockSyncContext.InvokeCalled); }