CommandPattern 1.0.0
This package contains a collection of classes and interfaces to allow for quick and consistent implementation of the command pattern across projects.
Public Member Functions | Properties | Events | List of all members
SadSapphicGames.CommandPattern.CommandStream Class Reference

This is the object that stores commands to be invoked and executes them when told to by the client. It has no knowledge of the implementation of commands beyond their interfaces. More...

Public Member Functions

ReadOnlyCollection< ICommandGetCommandHistory ()
 Get the CommandStream's history of executed Commands, a linear time operation if HistoryCount has reached HistoryDepth
More...
 
ReadOnlyCollection< ICommandGetCommandQueue ()
 Get the queue of commands to be executed by the command stream. More...
 
ReadOnlyCollection< Task > GetRunningCommandTasks ()
 Get the Tasks being being run by AsyncCommands executed by this CommandStream More...
 
CancellationTokenSource GetRunningTaskCTS (Task task)
 Gets the CancellationTokenSource of a running AsyncCommand's task More...
 
CancellationTokenSource GetRunningTaskCTS (IAsyncCommand asyncCommand)
 
void CancelRunningCommandTask (Task task)
 Cancels a task if that task is currently running More...
 
void CancelRunningCommandTask (IAsyncCommand asyncCommand)
 Cancels the task of an IAsyncCommand if that task is currently running. More...
 
ReadOnlyDictionary< Task, Exception > GetFaultedCommandTasks ()
 Get a dictionary of any faulted tasks and the exceptions that they threw More...
 
 CommandStream (float _historyDepth=Single.PositiveInfinity)
 Creates a new CommandStream More...
 
void QueueCommand (ICommand command)
 This adds a Command to the command Queue More...
 
void QueueCommands (IEnumerable< ICommand > commands)
 Adds multiple Commands to the queue More...
 
List< ICommandDropQueue ()
 This will remove all commands from the CommandStream's queue and replace it with a new empty queue. More...
 
ReadOnlyCollection< ICommandDropHistory ()
 This will remove all commands from the CommandStream's history and replace it with a new empty list. More...
 
bool TryQueueUndoCommand (IUndoable undoable)
 Attempt to queue's the undo command of a Command object implementing IUndoable if that command exists in this CommandStream's history More...
 
void ForceQueueUndoCommand (IUndoable undoable)
 Force the stream to queue's the undo command of a Command object implementing IUndoable regardless of whether the command is recorded in this CommandStream's history More...
 
bool TryPeekNext (out ICommand nextCommand)
 Examine the next command in the commandQueue with out executing it More...
 
void RequeueNextCommand ()
 Removes the next command in the CommandStream's queue, if it has one, and adds it back to the end of the queue.
 
void SkipNextCommand ()
 Removes the next command from the queue without executing it
 
ExecuteCode TryExecuteNext (out ICommand topCommand)
 Attempts to execute the next command in the queue, returns an enum indicating if it was able to or not or if the queue was empty or the command is async and awaiting completion. More...
 
ExecuteCode TryExecuteImmediate (ICommand command)
 Bypass the command queue and immediately attempt to execute a command More...
 
ExecuteCode TryUndoImmediate (IUndoable undoable)
 Bypass the command queue and immediately attempt to execute an IUndoable's undo command if the IUndoable is in the CommandStream's history More...
 
ExecuteCode ForceTryUndoImmediate (IUndoable undoable)
 Bypass the command queue and immediately attempt to execute an IUndoable's undo command, regardless of wether the IUndoable is in the CommandStream's history More...
 
ExecuteCode TryExecuteNext ()
 Attempts to execute the next command in the queue, returns false if it is empty or the command is IFailable and would fail. More...
 
void ExecuteFullQueue ()
 Execute's Commands from the CommandStream queue until it is empty. Be warned this will not give any indication of commands failing.
 
void ExecuteFullQueue (out List< ICommand > failedCommands)
 Executes Commands from the CommandStream's queue until it is empty. Returns the a list of any Commands that failed as an out parameter More...
 

Properties

bool QueueEmpty [get]
 Gets commandQueue.Count == 0
 
float HistoryDepth [get]
 this is the maximum number of commands that will be recorded in the CommandHistory
 
int HistoryCount [get]
 Gets the number of Commands currently recorded in the CommandStream's history.
 
int QueueCount [get]
 Gets the number of Commands currently waiting in the CommandStream's queue.
 

Events

Action< Exception > OnTaskFaulted
 This event will be invoked if one of the tasks from an IAsyncCommand executed by this stream faults. Can be used to throw any exception's caused by tasks rather than storing them on the task object
 

Detailed Description

This is the object that stores commands to be invoked and executes them when told to by the client. It has no knowledge of the implementation of commands beyond their interfaces.

Constructor & Destructor Documentation

◆ CommandStream()

SadSapphicGames.CommandPattern.CommandStream.CommandStream ( float  _historyDepth = Single.PositiveInfinity)

Creates a new CommandStream

Parameters
_historyDepthThe depth to which previously executed commands will be recorded. Once this depth is reached the oldest commands will be forgotten first. <remark> To not record history, set to zero. To never forget executed commands, set to positive infinity. </remark>

Member Function Documentation

◆ CancelRunningCommandTask() [1/2]

void SadSapphicGames.CommandPattern.CommandStream.CancelRunningCommandTask ( IAsyncCommand  asyncCommand)

Cancels the task of an IAsyncCommand if that task is currently running.

Parameters
asyncCommandThe IAsyncCommand to cancel the task off

◆ CancelRunningCommandTask() [2/2]

void SadSapphicGames.CommandPattern.CommandStream.CancelRunningCommandTask ( Task  task)

Cancels a task if that task is currently running

Parameters
taskThe task to cancel

◆ DropHistory()

ReadOnlyCollection< ICommand > SadSapphicGames.CommandPattern.CommandStream.DropHistory ( )

This will remove all commands from the CommandStream's history and replace it with a new empty list.

<remark> This can be useful if you need the CommandStream to record all of its history but also need it to execute an extremely large number of commands without running out of memory. Even if every command is the same object a CommandStream will run out of memory at 2-3 hundred million commands in its queue or history. </remark>

Returns
The commands in the previous history, in case this information is needed

◆ DropQueue()

List< ICommand > SadSapphicGames.CommandPattern.CommandStream.DropQueue ( )

This will remove all commands from the CommandStream's queue and replace it with a new empty queue.

<remark> This can be useful to rearrange the commands in a queue. Simple preform the needed changes on the returned list and re-queue it </remark>

Returns
The commands in the previous queue, in case this information is needed.

◆ ExecuteFullQueue()

void SadSapphicGames.CommandPattern.CommandStream.ExecuteFullQueue ( out List< ICommand failedCommands)

Executes Commands from the CommandStream's queue until it is empty. Returns the a list of any Commands that failed as an out parameter

Parameters
failedCommandsA list of any Commands in the Queue that failed to execute

◆ ForceQueueUndoCommand()

void SadSapphicGames.CommandPattern.CommandStream.ForceQueueUndoCommand ( IUndoable  undoable)

Force the stream to queue's the undo command of a Command object implementing IUndoable regardless of whether the command is recorded in this CommandStream's history

<remark>This is equivalent to passing the result of IUndoable.GetUndoCommand() into CommandStream.QueueCommand(Command command) directly</remark>

Parameters
undoableThe IUndoable Command to queue the undo-Command of

◆ ForceTryUndoImmediate()

ExecuteCode SadSapphicGames.CommandPattern.CommandStream.ForceTryUndoImmediate ( IUndoable  undoable)

Bypass the command queue and immediately attempt to execute an IUndoable's undo command, regardless of wether the IUndoable is in the CommandStream's history

Parameters
undoablethe IUndoable to execute the undo command of
Returns
The ExecuteCode of the attempt to execute the undo command of the IUndoable

◆ GetCommandHistory()

ReadOnlyCollection< ICommand > SadSapphicGames.CommandPattern.CommandStream.GetCommandHistory ( )

Get the CommandStream's history of executed Commands, a linear time operation if HistoryCount has reached HistoryDepth

Returns
The history of executed commands, null if history is not recorded.

◆ GetCommandQueue()

ReadOnlyCollection< ICommand > SadSapphicGames.CommandPattern.CommandStream.GetCommandQueue ( )

Get the queue of commands to be executed by the command stream.

Returns
The queue of commands the commandStream will execute.

◆ GetFaultedCommandTasks()

ReadOnlyDictionary< Task, Exception > SadSapphicGames.CommandPattern.CommandStream.GetFaultedCommandTasks ( )

Get a dictionary of any faulted tasks and the exceptions that they threw

Returns
A read only copy of the dictionary of faulted tasks and their exceptions

◆ GetRunningCommandTasks()

ReadOnlyCollection< Task > SadSapphicGames.CommandPattern.CommandStream.GetRunningCommandTasks ( )

Get the Tasks being being run by AsyncCommands executed by this CommandStream

Returns
The tasks that are currently being run

◆ GetRunningTaskCTS()

CancellationTokenSource SadSapphicGames.CommandPattern.CommandStream.GetRunningTaskCTS ( Task  task)

Gets the CancellationTokenSource of a running AsyncCommand's task

Parameters
taskThe task to get the CTS of
Returns
The CTS of task if it is running, null if it is not

◆ QueueCommand()

void SadSapphicGames.CommandPattern.CommandStream.QueueCommand ( ICommand  command)

This adds a Command to the command Queue

Parameters
commandThe Command to be Queued

◆ QueueCommands()

void SadSapphicGames.CommandPattern.CommandStream.QueueCommands ( IEnumerable< ICommand commands)

Adds multiple Commands to the queue

Parameters
commandsThe commands to be queued

◆ TryExecuteImmediate()

ExecuteCode SadSapphicGames.CommandPattern.CommandStream.TryExecuteImmediate ( ICommand  command)

Bypass the command queue and immediately attempt to execute a command

Parameters
commandThe command to immediately be executed
Returns
The ExecuteCode for the attempt to execute the command

◆ TryExecuteNext() [1/2]

ExecuteCode SadSapphicGames.CommandPattern.CommandStream.TryExecuteNext ( )

Attempts to execute the next command in the queue, returns false if it is empty or the command is IFailable and would fail.

Returns
False if the command queue is empty, or the next command would fail. True otherwise.

◆ TryExecuteNext() [2/2]

ExecuteCode SadSapphicGames.CommandPattern.CommandStream.TryExecuteNext ( out ICommand  topCommand)

Attempts to execute the next command in the queue, returns an enum indicating if it was able to or not or if the queue was empty or the command is async and awaiting completion.

Parameters
topCommandThe command that was next in the queue, null if the queue was empty
Returns
An ExecuteCode enum value indicating what happened when attempting to execute the next command

◆ TryPeekNext()

bool SadSapphicGames.CommandPattern.CommandStream.TryPeekNext ( out ICommand  nextCommand)

Examine the next command in the commandQueue with out executing it

Parameters
nextCommandThe next command in the queue, null if empty
Returns
Wether there was a command in queue or not

◆ TryQueueUndoCommand()

bool SadSapphicGames.CommandPattern.CommandStream.TryQueueUndoCommand ( IUndoable  undoable)

Attempt to queue's the undo command of a Command object implementing IUndoable if that command exists in this CommandStream's history

Parameters
undoableThe IUndoable Command to try and queue the undo-Command of
Returns
Wether the undo command was queued

◆ TryUndoImmediate()

ExecuteCode SadSapphicGames.CommandPattern.CommandStream.TryUndoImmediate ( IUndoable  undoable)

Bypass the command queue and immediately attempt to execute an IUndoable's undo command if the IUndoable is in the CommandStream's history

Parameters
undoablethe IUndoable to execute the undo command of
Returns
The ExecuteCode of the attempt to execute the undo command of the IUndoable, Execute.Failure if the IUndoable was not in the CommandStream's history

The documentation for this class was generated from the following file: