Example #1
0
        public ITask ScheduleNextAsyncFrame(ILifecycleObject @object, Action action, string taskName)
        {
            SimpleTask task = new SimpleTask(this, @object, action, ExecutionTargetContext.NextAsyncFrame);

            TriggerEvent(task);
            return(task);
        }
Example #2
0
        public ITask ScheduleEveryPhysicUpdate(ILifecycleObject @object, Action action, string taskName)
        {
            SimpleTask task = new SimpleTask(this, @object, action, ExecutionTargetContext.EveryPhysicsUpdate);

            TriggerEvent(task);
            return(task);
        }
Example #3
0
        public IScheduledTask ScheduleUpdate(ILifecycleObject @object, Action action, string taskName, ExecutionTargetContext target)
        {
            if ([email protected])
            {
                return(null);
            }

            ScheduledTask task = new ScheduledTask(++taskIds, taskName, this, @object, action, target);

            TriggerEvent(task, async(sender, @event) =>
            {
                if (target != ExecutionTargetContext.Sync && @object.IsAlive)
                {
                    return;
                }

                if (@event != null && ((ICancellableEvent)@event).IsCancelled)
                {
                    return;
                }

                action();
                InternalTasks.Remove(task);
            });

            return(task);
        }
Example #4
0
 public EventAction(ILifecycleObject owner, EventCallback action, EventHandler handler, Type eventType)
 {
     Owner            = owner;
     Action           = action;
     Handler          = handler;
     TargetEventNames = EventManager.GetEventNames(eventType);
     TargetEventType  = eventType;
 }
Example #5
0
 public EventAction(ILifecycleObject owner, EventCallback action, EventHandler handler, Type eventType)
 {
     Owner            = new Util.WeakReference <ILifecycleObject>(owner);
     Action           = action;
     Handler          = handler;
     TargetEventNames = EventBus.GetEventNames(eventType);
     TargetEventType  = eventType;
 }
Example #6
0
        public void Unsubscribe <TEvent>(ILifecycleObject @object) where TEvent : IEvent
        {
            if ([email protected])
            {
                return;
            }

            eventListeners.RemoveAll(c => c.Owner == @object);
        }
Example #7
0
        public void Unsubscribe(ILifecycleObject @object)
        {
            if ([email protected])
            {
                return;
            }

            eventListeners.RemoveAll(c => !c.Owner.IsAlive || c.Owner.Target == @object);
        }
Example #8
0
        public void Unsubscribe(ILifecycleObject @object, Type eventType)
        {
            if ([email protected])
            {
                return;
            }

            eventListeners.RemoveAll(c => c.Owner == @object && CheckEvent(c, GetEventNames(eventType)));
        }
Example #9
0
 /// <inheritdoc />
 public ThreadSafeTask(int taskId, string name, ITaskScheduler scheduler, ILifecycleObject owner, Action action, ExecutionTargetContext executionTargetContext)
 {
     TaskId          = taskId;
     Name            = name;
     Scheduler       = scheduler;
     Owner           = owner;
     Action          = action;
     ExecutionTarget = executionTargetContext;
 }
Example #10
0
 internal SimpleTask(int taskId, string taskName, ITaskScheduler scheduler, ILifecycleObject owner, Action action,
                     ExecutionTargetContext executionTarget)
 {
     Name            = taskName;
     ownerRef        = new Core.Util.WeakReference <ILifecycleObject>(owner);
     Action          = action;
     ExecutionTarget = executionTarget;
     Scheduler       = scheduler;
     TaskId          = taskId;
 }
Example #11
0
        internal SimpleTask(ITaskScheduler scheduler, ILifecycleObject owner, Action action,
                            ExecutionTargetContext executionTarget)
        {
            Owner           = owner;
            Action          = action;
            ExecutionTarget = executionTarget;
            Scheduler       = scheduler;

            TaskId = ++taskIds;
        }
Example #12
0
 internal SimpleTask(int taskId, string taskName, ITaskScheduler scheduler, ILifecycleObject owner, Action action,
                     ExecutionTargetContext executionTarget)
 {
     Name            = taskName;
     Owner           = owner;
     Action          = action;
     ExecutionTarget = executionTarget;
     Scheduler       = scheduler;
     TaskId          = taskId;
 }
Example #13
0
        public static ITask RunOnMainThread(this ITaskScheduler taskScheduler, ILifecycleObject owner, Action action, string taskName)
        {
            if (Thread.CurrentThread == taskScheduler.MainThread)
            {
                action();
                return(null);
            }

            return(taskScheduler.ScheduleUpdate(owner, action, taskName, ExecutionTargetContext.NextFrame));
        }
Example #14
0
 public EventAction(
     ILifecycleObject owner,
     EventCallback action,
     EventHandler handler,
     List <string> eventNames)
 {
     Owner            = new Util.WeakReference <ILifecycleObject>(owner);
     Action           = action;
     Handler          = handler;
     TargetEventNames = eventNames;
 }
Example #15
0
        public void Unsubscribe(ILifecycleObject @object, string eventName)
        {
            if ([email protected])
            {
                return;
            }

            eventListeners.RemoveAll(c => c.Owner == @object && CheckEvent(c, new List <string> {
                eventName
            }));
        }
Example #16
0
        public ITask ScheduleAt(ILifecycleObject @object, Action action, string taskName, DateTime date, bool runAsync = false)
        {
            SimpleTask task = new SimpleTask(++taskIds, taskName, this, @object, action,
                                             runAsync ? ExecutionTargetContext.Async : ExecutionTargetContext.Sync)
            {
                StartTime = date
            };

            TriggerEvent(task);
            return(task);
        }
Example #17
0
        public void Unsubscribe(ILifecycleObject @object, Type eventType, Type eventEmitterType)
        {
            if ([email protected])
            {
                return;
            }

            eventListeners.RemoveAll(c => !c.Owner.IsAlive || c.Owner.Target == @object &&
                                     CheckEvent(c, GetEventNames(eventType)) &&
                                     CheckEmitter(c, GetEmitterName(eventEmitterType), false));
        }
Example #18
0
 public EventAction(
     ILifecycleObject owner,
     EventCallback action,
     EventHandler handler,
     List <string> eventNames)
 {
     Owner            = owner;
     Action           = action;
     Handler          = handler;
     TargetEventNames = eventNames;
 }
Example #19
0
        public void Subscribe(ILifecycleObject @object, EventCallback callback, Type eventType, Type eventEmitterType)
        {
            if ([email protected])
            {
                return;
            }

            EventHandler handler = GetEventHandler(callback.Method, GetEmitterName(eventEmitterType));

            eventListeners.Add(new EventAction(@object, callback.Invoke, handler, eventType));
        }
Example #20
0
        public void Unsubscribe <TEvent, TEmitter>(ILifecycleObject @object)
            where TEvent : IEvent where TEmitter : IEventEmitter
        {
            if ([email protected])
            {
                return;
            }

            eventListeners.RemoveAll(c => c.Owner == @object &&
                                     CheckEvent(c, GetEventNames(typeof(TEvent))) &&
                                     CheckEmitter(c, GetEmitterName(typeof(TEmitter)), false));
        }
Example #21
0
 public EventAction(ILifecycleObject owner,
                    IEventListener listener,
                    MethodInfo method,
                    EventHandler handler, Type type)
 {
     Owner            = owner;
     Listener         = listener;
     Action           = (sender, @event) => method.Invoke(listener, new object[] { sender, @event });
     Handler          = handler;
     TargetEventNames = EventManager.GetEventNames(type);
     TargetEventType  = type;
 }
Example #22
0
 public EventAction(ILifecycleObject owner,
                    IEventListener listener,
                    MethodInfo method,
                    EventHandler handler, Type type)
 {
     Owner            = new Util.WeakReference <ILifecycleObject>(owner);
     Listener         = listener;
     Action           = (sender, @event) => method.InvokeWithTaskSupport(listener, new object[] { sender, @event });
     Handler          = handler;
     TargetEventNames = EventBus.GetEventNames(type);
     TargetEventType  = type;
 }
Example #23
0
        public void Subscribe <TEvent, TEmitter>(ILifecycleObject @object, EventCallback <TEvent> callback)
            where TEvent : IEvent where TEmitter : IEventEmitter
        {
            if ([email protected])
            {
                return;
            }

            EventHandler handler = GetEventHandler(callback.Method, GetEmitterName(typeof(TEmitter)));

            eventListeners.Add(new EventAction(@object,
                                               (sender, @event) => callback.Invoke(sender, (TEvent)@event), handler, typeof(TEvent)));
        }
Example #24
0
        public void Subscribe(ILifecycleObject @object, string emitterName, string eventName, EventCallback callback)
        {
            if ([email protected])
            {
                return;
            }

            EventHandler handler = GetEventHandler(callback.Method, emitterName);

            eventListeners.Add(new EventAction(@object, callback, handler, new List <string> {
                eventName
            }));
        }
Example #25
0
        public static IEnumerable <Type> FindTypes <T>(this ILifecycleObject @object,
                                                       bool includeAbstractAndInterfaces = true,
                                                       Func <Type, bool> predicate       = null)
        {
            IEnumerable <Type> filter = @object.FindAllTypes(includeAbstractAndInterfaces)
                                        .Where(t => typeof(T).IsAssignableFrom(t));

            if (predicate != null)
            {
                filter = filter.Where(predicate);
            }

            return(filter);
        }
Example #26
0
        public void Unsubscribe(ILifecycleObject @object, string emitterName, string eventName)
        {
            if ([email protected])
            {
                return;
            }

            eventListeners.RemoveAll(c => c.Owner == @object &&
                                     c.Handler.EmitterName.Equals(emitterName,
                                                                  StringComparison.OrdinalIgnoreCase) &&
                                     CheckEvent(c, new List <string> {
                eventName
            }));
        }
Example #27
0
        /// <inheritdoc />
        public ITask ScheduleUpdate(ILifecycleObject @object, Action action, string taskName, ExecutionTargetContext target)
        {
            ThreadSafeTask task;

            lock (taskIdLock)
            {
                task = new ThreadSafeTask(taskId++, taskName, this, @object, action, target);
            }

            lock (lockObj)
            {
                tasks.Add(task);
            }

            return(task);
        }
Example #28
0
        /// <inheritdoc />
        public ITask ScheduleAt(ILifecycleObject @object, Action action, string taskName, DateTime date, bool runAsync = false)
        {
            ThreadSafeTask         task;
            ExecutionTargetContext context = runAsync ? ExecutionTargetContext.Async : ExecutionTargetContext.NextFrame;

            lock (taskIdLock)
            {
                task = new ThreadSafeTask(taskId++, taskName, this, @object, action, context, TimeSpan.Zero, date, null);
            }

            lock (lockObj)
            {
                tasks.Add(task);
            }

            return(task);
        }
Example #29
0
        public ITask SchedulePeriodically(ILifecycleObject @object, Action action, string taskName, TimeSpan period, TimeSpan?delay = null,
                                          bool runAsync = false)
        {
            SimpleTask task = new SimpleTask(++taskIds, taskName, this, @object, action,
                                             runAsync ? ExecutionTargetContext.Async : ExecutionTargetContext.Sync)
            {
                Period = period
            };

            if (delay != null)
            {
                task.StartTime = DateTime.Now + delay;
            }

            TriggerEvent(task);
            return(task);
        }
Example #30
0
        public void AddEventListener(ILifecycleObject @object, IEventListener eventListener)
        {
            if ([email protected])
            {
                return;
            }

            // ReSharper disable once UseIsOperator.2
            if (!typeof(IEventListener).IsInstanceOfType(eventListener))
            {
                throw new ArgumentException(
                          "The eventListener to register has to implement IEventListener!",
                          nameof(eventListener));
            }

            if (eventListeners.Any(c => c.Listener?.GetType() == eventListener.GetType()))
            {
                return;
            }

            Type type = eventListener.GetType();

            foreach (Type @interface in type.GetInterfaces()
                     .Where(c => typeof(IEventListener).IsAssignableFrom(c) &&
                            c.GetGenericArguments().Length > 0))
            {
                foreach (MethodInfo method in @interface.GetMethods())
                {
                    EventHandler handler = (EventHandler)method.GetCustomAttributes(typeof(EventHandler), false)
                                           .FirstOrDefault()
                                           ?? new EventHandler
                    {
                        Priority = ServicePriority.Normal
                    };

                    Type eventType = @interface.GetGenericArguments()[0];

                    eventListeners.Add(new EventAction(@object, eventListener, method, handler, eventType));
                }
            }
        }