Beispiel #1
0
        /// <summary>
        /// Delays until this lifetime is complete
        /// </summary>
        /// <returns>an async task</returns>
        public static Task ToTask(this ILifetimeManager manager)
        {
            var tcs = new TaskCompletionSource();

            manager.OnDisposed(() => tcs.SetResult());
            return(tcs.Task);
        }
Beispiel #2
0
 /// <summary>
 /// Subscribes to this event such that the given handler will be called when the event fires. Notifications will stop
 /// when the lifetime associated with the given lifetime manager is disposed.
 /// </summary>
 /// <param name="handler">the action to run when the event fires</param>
 /// <param name="lifetimeManager">the lifetime manager that determines when to stop being notified</param>
 public void SubscribeForLifetime(Action handler, ILifetimeManager lifetimeManager)
 {
     if (lifetimeManager.IsExpired == false)
     {
         subscribers.Add(handler, lifetimeManager);
         lifetimeManager.OnDisposed(() => subscribers.Remove(handler));
     }
 }
Beispiel #3
0
        /// <summary>
        /// Subscribes to the given event for the given lifetime
        /// </summary>
        /// <param name="route">the event to subscribe to</param>
        /// <param name="handler">the event handler</param>
        /// <param name="lifetimeManager">defines the lifetime of the subscription</param>
        public void Register(string route, Action <RoutedEvent <T> > handler, ILifetimeManager lifetimeManager)
        {
            GetOrAddRoutedEvent(route).SubscribeForLifetime(handler, lifetimeManager);

            lifetimeManager.OnDisposed(() =>
            {
                routes.Remove(route);
            });
        }
Beispiel #4
0
        public async Task SetInterval(Action action, TimeSpan interval, ILifetimeManager lifetime)
        {
            var shouldRun = true;

            lifetime.OnDisposed(() => shouldRun = false);
            while (shouldRun)
            {
                await DelayAsync(interval);

                action();
            }
        }
Beispiel #5
0
        public static async Task <bool> TryControlVelocity(this SpacialElement el, Func <Velocity, Task> takeoverAction, ILifetimeManager lt)
        {
            Velocity tempV = new Velocity(el);

            lt.OnDisposed(tempV.Lifetime.Dispose);
            if (el is IHaveVelocity)
            {
                if ((el as IHaveVelocity).Velocity.MovementTakeover != null)
                {
                    return(false);
                }
                await(el as IHaveVelocity).Velocity.Takeover(() => takeoverAction(tempV));
            }
            else
            {
                await takeoverAction(tempV);
            }
            return(true);
        }
Beispiel #6
0
        public static async Task <bool> TryControlVelocity(this SpacialElement el, Func <Velocity, Task> takeoverAction, ILifetimeManager lt)
        {
            Velocity tempV = new Velocity(el);

            lt.OnDisposed(tempV.Lifetime.Dispose);
            if (el is IHaveVelocity)
            {
                if ((el as IHaveVelocity).Velocity.MovementTakeover != null)
                {
                    return(false);
                }
                tempV.HitDetectionDynamicExclusions = (el as IHaveVelocity).Velocity.HitDetectionDynamicExclusions;
                tempV.HitDetectionExclusions.AddRange((el as IHaveVelocity).Velocity.HitDetectionExclusions);
                tempV.HitDetectionExclusionTypes.AddRange((el as IHaveVelocity).Velocity.HitDetectionExclusionTypes);
                await(el as IHaveVelocity).Velocity.Takeover(() => takeoverAction(tempV));
            }
            else
            {
                await takeoverAction(tempV);
            }
            return(true);
        }
Beispiel #7
0
        /// <summary>
        /// Subscribes to be notified when the given property changes and also fires an initial notification.  The subscription expires when
        /// the given lifetime manager's lifetime ends.
        /// </summary>
        /// <param name="propertyName">The name of the property to subscribe to or ObservableObject.AnyProperty if you want to be notified of any property change.</param>
        /// <param name="handler">The action to call for notifications</param>
        /// <param name="lifetimeManager">the lifetime manager that determines when the subscription ends</param>

        public void SynchronizeForLifetime(string propertyName, Action handler, ILifetimeManager lifetimeManager)
        {
            var sub = SynchronizeUnmanaged(propertyName, handler);

            lifetimeManager.OnDisposed(sub);
        }
Beispiel #8
0
 public TimeThrottler(Action innerAction, ILifetimeManager lt)
 {
     this.innerAction = innerAction;
     Time.CurrentTime.EndOfCycle.SubscribeForLifetime(() => iterationsThisTick = 0, lt);
     lt.OnDisposed(this.Dispose);
 }
Beispiel #9
0
        /// <summary>
        /// Subscribes to this event such that the given handler will be called when the event fires. Notifications will stop
        /// when the lifetime associated with the given lifetime manager is disposed.
        /// </summary>
        /// <param name="handler">the action to run when the event fires</param>
        /// <param name="lifetimeManager">the lifetime manager that determines when to stop being notified</param>
        public void SubscribeForLifetime(Action handler, ILifetimeManager lifetimeManager)
        {
            var sub = SubscribeUnmanaged(handler);

            lifetimeManager.OnDisposed(sub.Dispose);
        }
Beispiel #10
0
 public void PushForLifetime(ConsoleKey key, ConsoleModifiers?modifier, Action <ConsoleKeyInfo> handler, ILifetimeManager manager)
 {
     manager.OnDisposed(PushUnmanaged(key, modifier, handler));
 }