Esempio n. 1
0
        public void Publish <T>(T eventData, InvokeOption invokeOption)
        {
            var type = typeof(T);

            if (handlers.ContainsKey(type))
            {
                var subscribers = handlers[type].OrderBy(x => x.Priority).ToList();

                for (int i = subscribers.Count - 1; i >= 0; i--)
                {
                    var subscriber = subscribers[i];

                    if (subscriber.Delay > 0 && !subscriber.IsDelayCompleted)
                    {
                        UnregisterDelay(subscriber);
                        RegisterDelay(subscriber, eventData);
                        continue;
                    }

                    if (InvokeSubscriberHandler(subscriber, eventData, invokeOption) == false)
                    {
                        subscribers.RemoveAt(i);
                    }
                }
            }
        }
Esempio n. 2
0
        public void Subscribe <T>(Action <T> handler, InvokeOption invokeOption, InvokePriority priority, int delay)
        {
            if (!handlers.ContainsKey(typeof(T)))
            {
                handlers[typeof(T)] = new List <SubscriberInfo>();
            }

            handlers[typeof(T)].Add(new SubscriberInfo
            {
                Priority = priority,
                Handler  = new WeakAction(handler),
                Option   = invokeOption,
                Delay    = delay,
            });
        }
        private TValue MockInvoke <TValue>(InvokeOption invokeOption)
        {
            if (!MockConfigurationContext.IsConfigured && !MockConfigurationContext.IsConfiguring)
            {
                MockConfigurationContext.Configure();
            }

            object result = null;

            if (invokeOption is IGetPropertyOption getPropertyOption)
            {
                result = MockGetProperty(getPropertyOption);
            }
            else if (invokeOption is IInvokeFunctionOption invokeFunctionOption)
            {
                result = MockInvokeFunction(invokeFunctionOption);
            }

            if (result is null)
            {
                return(default);
Esempio n. 4
0
 public void Subscribe <T>(Action <T> handler, InvokeOption invokeOption, int delay)
 {
     Subscribe(handler, invokeOption, InvokePriority.Low, delay);
 }
Esempio n. 5
0
 public void Subscribe <T>(Action <T> handler, InvokeOption invokeOption, InvokePriority priority)
 {
     Subscribe(handler, invokeOption, priority, 0);
 }
Esempio n. 6
0
 public void Subscribe <T>(Action <T> handler, InvokeOption invokeOption)
 {
     Subscribe(handler, invokeOption, InvokePriority.Normal, 0);
 }
Esempio n. 7
0
        private bool InvokeSubscriberHandler <T>(SubscriberInfo subscriber, T eventData, InvokeOption invokeOption)
        {
            if (subscriber.Delay > 0)
            {
                UnregisterDelay(subscriber);
            }

            var handler = subscriber.Handler.CreateAction <T>();

            if (handler == null)
            {
                return(false);
            }

            switch (invokeOption)
            {
            case InvokeOption.UIThread:
                SynchronizationContext.Post(state => handler(eventData), eventData);
                break;

            case InvokeOption.ThreadPool:
                ThreadPool.QueueUserWorkItem(obj => handler(eventData));
                break;

            case InvokeOption.Blocking:
                handler(eventData);
                break;
            }
            return(true);
        }
 /// <inheritdoc />
 public ValueTask InvokeVoidAsync(string identifier, InvokeOption invokeOption)
 {
     MockInvoke <object>(invokeOption);
     return(ValueTask.CompletedTask);
 }
 /// <inheritdoc />
 public void InvokeVoid(string identifier, InvokeOption invokeOption)
 {
     MockInvoke <object>(invokeOption);
 }