Esempio n. 1
0
        private static Action NewMethod <TEventArg>(IEventContainer <TEventArg> eventContainer, Action action, Func <bool> predicate)
        {
            eventContainer.Subscribe(OnReservedEventTriggered);
            if (predicate())
            {
                UnsubscribeAndAction();
            }

            return(() => eventContainer.Unsubscribe(OnReservedEventTriggered));

            #region Local Methods

            void OnReservedEventTriggered(object sender, TEventArg arg)
            {
                if (predicate())
                {
                    UnsubscribeAndAction();
                }
            }

            void UnsubscribeAndAction()
            {
                eventContainer.Unsubscribe(OnReservedEventTriggered);
                action();
            }

            #endregion
        }
Esempio n. 2
0
        public static Action DoOrReserve(
            this IEventContainer eventContainer,
            Action action,
            Func <bool> predicate)
        {
            eventContainer.Subscribe(OnReservedEventTriggered);
            if (predicate())
            {
                UnsubscribeAndAction();
            }

            return(() => eventContainer.Unsubscribe(OnReservedEventTriggered));

            #region Local Methods

            void OnReservedEventTriggered(object sender, EventArgs arg)
            {
                if (predicate())
                {
                    UnsubscribeAndAction();
                }
            }

            void UnsubscribeAndAction()
            {
                eventContainer.Unsubscribe(OnReservedEventTriggered);
                action();
            }

            #endregion
        }
Esempio n. 3
0
        public static Action PendingSubscribeWhileActing <TEventArg>(
            this IEventContainer <TEventArg> eventContainer,
            Action <object, TEventArg> action,
            Func <object, TEventArg, bool> predicate = null)
        {
            predicate = predicate ?? Predicates.True;
            eventContainer.Subscribe(OnTriggered);

            return(() => eventContainer.Unsubscribe(OnTriggered));

            void OnTriggered(object sender, TEventArg arg)
            {
                if (predicate(sender, arg))
                {
                    eventContainer.Unsubscribe(OnTriggered);
                    action(sender, arg);
                    eventContainer.Subscribe(OnTriggered);
                }
            }
        }