Taijutsu.Event.Internal.BatchedHandlingSettings.BatchedHandlingSettings C# (CSharp) Method

BatchedHandlingSettings() public method

public BatchedHandlingSettings ( [ type, DelayUntil delayUntil, int priority ) : System
type [
delayUntil DelayUntil
priority int
return System
        public BatchedHandlingSettings([NotNull] Type type, DelayUntil delayUntil, int priority)
            : base(type, priority)
        {
            this.delayUntil = delayUntil;
            Unique = true;

            batchedAction = ev =>
            {
                if (ev == null)
                {
                    throw new ArgumentNullException("ev");
                }

                if (Type.IsInstanceOfType(ev))
                {
                    var context = InternalEnvironment.DataContextSupervisor.CurrentContext;

                    if (context != null)
                    {
                        var extension = InitializeExtension(context);

                        if (!extension.ContainsKey(this))
                        {
                            extension[this] = new List<object>();
                            EventHandler<FinishedEventArgs> action = null;

                            action = (sender, e) =>
                            {
                                try
                                {
                                    // ReSharper disable AccessToModifiedClosure
                                    switch (delayUntil)
                                    {
                                        case DelayUntil.PreCompleted:
                                            context.BeforeCompleted -= action;
                                            break;
                                        case DelayUntil.Completed:
                                            context.AfterCompleted -= action;
                                            break;
                                        case DelayUntil.Finished:
                                            context.Finished -= action;
                                            break;
                                    }

                                    var events = (List<object>)extension[this];

                                    extension.Remove(this);

                                    // ReSharper restore AccessToModifiedClosure
                                    if (events.Count > 0 && e.Completed)
                                    {
                                        Events.Publish(ResolveConstructor()(events));
                                    }
                                }
                                finally
                                {
                                    context = null;
                                    action = null;
                                    extension = null;
                                }
                            };

                            switch (delayUntil)
                            {
                                case DelayUntil.PreCompleted:
                                    context.BeforeCompleted += action;
                                    break;
                                case DelayUntil.Completed:
                                    context.AfterCompleted += action;
                                    break;
                                case DelayUntil.Finished:
                                    context.Finished += action;
                                    break;
                            }
                        }

                        ((List<object>)extension[this]).Add(ev);
                    }
                    else
                    {
                        Trace.TraceWarning("Taijutsu: Source of event is outside of unit of work, event is skipped.");
                    }
                }
            };
        }