public virtual IDisposable[] PublishOrderedCommands(CommandBase[] commands, IObserver <ICommandResponse <Unit> >[] observers = null)
        {
            var disposables      = new List <IDisposable>();
            var commandsTracking = commands.OrderBy(c => c.Order).ToDictionary(c => c as ICommandBase, c => false);
            var filter           = CreateOrderedCommandsFilter(commands, commandsTracking);

            filter.UnregisterToken = RegisterForCompletedCommands(filter.CompletedCommandsObserver);
            var pusbFilterDispose =
                RegisterForCompletedCommands(Observer.Create <CommandBase>(c => TraversePendingCommands()));

            filter.Finalizer = () =>
            {
                pusbFilterDispose.Dispose();
                filter.Finalizer = null;
                _filterManager.RemoveFilter(filter);
            };

            _filterManager.AddFilter(filter);
            for (int i = 0; i < commands.Length; i++)
            {
                var command = commands[i];
                IObserver <ICommandResponse <Unit> > observer = null;

                if (observers != null)
                {
                    observer = observers.ElementAtOrDefault(i);
                }
                disposables.Add(PublishCommand(command, observer));
            }
            return(disposables.ToArray());
        }
Exemple #2
0
        public CommandBase CreateCommand(Type type, params object[] args)
        {
            CommandBase instance = (CommandBase)Activator.CreateInstance(type, args);

            if (instance is BusyCommandBase)
            {
                instance.BeforeExecuteAction = c => _filterManager.AddFilter(_busyFilter);
                instance.CompleteAction      = c => _filterManager.RemoveFilter(_busyFilter);
                instance.RegisterForStateChange(Observer.Create <IObservedChange <CommandBase, CommandState> >(b =>
                {
                    if (b.Value == CommandState.Blocked || b.Value == CommandState.Canceled)
                    {
                        _filterManager.RemoveFilter(_busyFilter);
                    }
                }));
            }

            if (OnCreateCommand != null)
            {
                OnCreateCommand(instance);
            }

            return(instance);
        }