Пример #1
0
        public virtual SubscriptionToken Subscribe(Action <TPayload> action, ThreadOption threadOption, bool keepSubscriberReferenceAlive, Predicate <TPayload> filter)
        {
            var actionReference = (IDelegateReference) new DelegateReference(action, keepSubscriberReferenceAlive);
            var filterReference = filter == null ? (IDelegateReference) new DelegateReference((Predicate <TPayload>)(predicate => true), true) : new DelegateReference(filter, keepSubscriberReferenceAlive);

            EventSubscription <TPayload> eventSubscription;

            switch (threadOption)
            {
            case ThreadOption.PublisherThread:
                eventSubscription = new EventSubscription <TPayload>(actionReference, filterReference);
                break;

            case ThreadOption.UIThread:
                if (this.SynchronizationContext == null)
                {
                    throw new InvalidOperationException(Resources.EventAggregatorNotConstructedOnUIThread);
                }

                eventSubscription = new DispatcherEventSubscription <TPayload>(actionReference, filterReference, this.SynchronizationContext);
                break;

            case ThreadOption.BackgroundThread:
                eventSubscription = new BackgroundEventSubscription <TPayload>(actionReference, filterReference);
                break;

            default:
                eventSubscription = new EventSubscription <TPayload>(actionReference, filterReference);
                break;
            }

            return(this.InternalSubscribe(eventSubscription));
        }
Пример #2
0
        /// <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)
        {
            var 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 (this.SynchronizationContext == null)
                {
                    throw new InvalidOperationException(Resources.EventAggregatorNotConstructedOnUIThread);
                }
                subscription = new DispatcherEventSubscription(actionReference, this.SynchronizationContext);
                break;

            default:
                subscription = new EventSubscription(actionReference);
                break;
            }

            return(this.InternalSubscribe(subscription));
        }