Ejemplo n.º 1
0
        // Until - it will handle till the condition is true, note this

        public static HandlerBase <E, H> Until <E, H>(this HandlerBase <E, H> handler, Func <Event <E>, bool> condition)
            where
        E : EventArgs
        {
            Guard.ArgumentNotNull(handler, "handler");

            if (handler.PostHandle != null)
            {
                var _existingHandle = handler.PostHandle;
                handler.PostHandle = (h, e) =>
                {
                    _existingHandle(h, e);
                    if (!condition(e))
                    {
                        h.Dispose();
                    }
                };
            }
            else
            {
                handler.PostHandle = (h, e) => { if (!condition(e))
                                                 {
                                                     h.Dispose();
                                                 }
                }
            };
            return(handler);
        }
Ejemplo n.º 2
0
        public static HandlerBase <E, H> Timeout <E, H>(this HandlerBase <E, H> handler, TimeSpan period, Action onTimeout)
            where
        E : EventArgs
        {
            Guard.ArgumentNotNull(handler, "handler");
            Guard.ArgumentOutOfRange((period <= TimeSpan.Zero), "period", HANDLER_PERIODLESSTHANZERO);

            // if the timer finishes before the first event, it will dispose the handler
            var _timer = new DispatcherTimer {
                Interval = period
            };

            _timer.Tick += (s, e) =>
            {
                if (handler != null)
                {
                    handler.Dispose();
                }
                handler = null;
                if (onTimeout != null)
                {
                    onTimeout();
                }
                onTimeout = null;
            };

            if (handler.PreHandle != null)
            {
                var _existingHandle = handler.PreHandle;
                handler.PreHandle = (h, e) =>
                {
                    _existingHandle(h, e);
                    if (_timer != null)
                    {
                        _timer.Stop();
                        _timer    = null;
                        onTimeout = null;
                    }
                };
            }
            else
            {
                handler.PreHandle = (h, e) =>
                {
                    if (_timer != null)
                    {
                        _timer.Stop();
                        _timer = null;
                    }
                }
            };

            _timer.Start();
            return(handler);
        }
Ejemplo n.º 3
0
        // Handle Once, allows the handled to be called once and thereafer it will be disposed off

        public static HandlerBase <E, H> HandleOnce <E, H>(this HandlerBase <E, H> handler)
            where
        E : EventArgs
        {
            Guard.ArgumentNotNull(handler, "handler");

            if (handler.PostHandle != null)
            {
                var _existingHandle = handler.PostHandle;
                handler.PostHandle = (h, e) => { _existingHandle(h, e); h.Dispose(); };
            }
            else
            {
                handler.PostHandle = (h, e) => h.Dispose();
            }
            return(handler);
        }
Ejemplo n.º 4
0
        // Skip is the oppose of when

        public static HandlerBase <E, H> SkipWhen <E, H>(this HandlerBase <E, H> handler, Func <Event <E>, bool> predicate)
            where
        E : EventArgs
        {
            Guard.ArgumentNotNull(handler, "handler");
            Guard.ArgumentNotNull(predicate, "predicate");

            if (handler.Predicate != null)
            {
                var _existingPredicate = handler.Predicate;
                handler.Predicate = (e) => _existingPredicate(e) && !predicate(e);
            }
            else
            {
                handler.Predicate = (e) => !predicate(e);
            }
            return(handler);
        }
Ejemplo n.º 5
0
        // Handle exactly - allows the handle to be called an exact number of times

        public static HandlerBase <E, H> HandleExactly <E, H>(this HandlerBase <E, H> handler, int count)
            where
        E : EventArgs
        {
            Guard.ArgumentNotNull(handler, "handler");
            Guard.ArgumentOutOfRange((count < 1), "count", HANDLECOUNT_OUTOFRANGE);

            int _count = count - 1;

            if (handler.PostHandle != null)
            {
                var _existingHandle = handler.PostHandle;
                handler.PostHandle = (h, e) =>
                {
                    _existingHandle(h, e);
                    if (_count == 0)
                    {
                        h.Dispose();
                    }
                    else
                    {
                        _count -= 1;
                    }
                };
            }
            else
            {
                handler.PostHandle = (h, e) =>
                {
                    if (_count == 0)
                    {
                        h.Dispose();
                    }
                    else
                    {
                        _count -= 1;
                    }
                }
            };
            return(handler);
        }
Ejemplo n.º 6
0
        // For - specifically for property changed event args

        public static HandlerBase <PropertyChangedEventArgs, PropertyChangedEventHandler> HandleFor
            (this HandlerBase <PropertyChangedEventArgs, PropertyChangedEventHandler> handler, Expression <Func <Object> > propertySelector)
        {
            Guard.ArgumentNotNull(handler, "handler");
            Guard.ArgumentNotNull(propertySelector, "propertySelector");

            // get the property name
            var _propertyName = PropertyChangedExtensions.GetPropertyName(propertySelector);

            if (handler.Predicate != null)
            {
                var _existingPredicate = handler.Predicate;
                handler.Predicate = (e) => _existingPredicate(e) &&
                                    (string.Equals(e.EventArgs.PropertyName, _propertyName, StringComparison.InvariantCulture));
            }
            else
            {
                handler.Predicate = (e) => string.Equals(e.EventArgs.PropertyName, _propertyName, StringComparison.InvariantCulture);
            }
            return(handler);
        }
Ejemplo n.º 7
0
        // Throttle

        public static HandlerBase <E, H> Throttle <E, H>(this HandlerBase <E, H> handler, TimeSpan period)
            where
        E : EventArgs
        {
            Guard.ArgumentNotNull(handler, "handler");
            Guard.ArgumentOutOfRange((period <= TimeSpan.Zero), "period", HANDLER_PERIODLESSTHANZERO);

            var _lastAllow = DateTime.MinValue;

            if (handler.Predicate != null)
            {
                var _existingPredicate = handler.Predicate;
                handler.Predicate = (e) =>
                {
                    var _allow = (DateTime.Now.Subtract(_lastAllow) > period);
                    if (_allow)
                    {
                        _lastAllow = DateTime.Now;
                    }
                    return(_existingPredicate(e) && _allow);
                };
            }
            else
            {
                handler.Predicate = (e) =>
                {
                    var _allow = (DateTime.Now.Subtract(_lastAllow) > period);
                    if (_allow)
                    {
                        _lastAllow = DateTime.Now;
                    }
                    return(_allow);
                };
            }
            return(handler);
        }
Ejemplo n.º 8
0
        // Recurring Timeout

        public static HandlerBase <E, H> RecurringTimeout <E, H>(this HandlerBase <E, H> handler, TimeSpan period)
            where
        E : EventArgs
        {
            return(RecurringTimeout(handler, period, null));
        }