Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Command ¶
type Command interface { // Do performs the action. // An error is returned if the action could not be performed. Do(modder world.Modder) error // Undo reverses its action to restore the environment to the previous state. // An error is returned if the action could not be successfully undone. The // environment may not be in the state as before in an error occurred. Undo(modder world.Modder) error }
Command describes an action that can be performed, undone, and redone.
type Commander ¶
type Commander interface {
Queue(command Command)
}
Commander tries to execute the given command.
type List ¶
type List []Command
List is a sequence of commands, which are executed as one step.
type Registry ¶ added in v1.6.0
type Registry interface { Commander Register(modifier ...TransactionModifier) error }
Registry allows to build commands as transactions with modifier functions.
type Stack ¶
type Stack struct {
// contains filtered or unexported fields
}
Stack describes a list of commands. The stack allows to sequentially undo and redo stacked commands. It essentially stores two lists: a list of commands to undo, and another of commands to redo. Modifying stack functions will panic if they are called while already in use.
func (*Stack) CanRedo ¶
CanRedo returns true if there is at least one more command that can be redone.
func (*Stack) CanUndo ¶
CanUndo returns true if there is at least one more command that can be undone.
func (*Stack) Perform ¶
Perform executes the given command and puts it on the stack if the command was successful. This function also clears the list of commands to be redone.
func (*Stack) Redo ¶
Redo attempts to perform the next command on the redo list. If there is no further command to redo, nothing happens. An error is returned if the command failed. In this case, the stack is unchanged and a further attempt to redo will try the same command again.
type Transaction ¶ added in v1.6.0
Transaction is a named list of forward and reverse tasks. Transaction provides execution of the tasks as a Command.
type TransactionBuilder ¶ added in v1.6.0
type TransactionBuilder struct { Commander Commander // contains filtered or unexported fields }
TransactionBuilder provides a way to register a command at a commander with possibly nested actions.
func (*TransactionBuilder) Queue ¶ added in v1.6.0
func (builder *TransactionBuilder) Queue(command Command)
Queue implements the Commander interface to add given command to a currently built transaction. If there is no outer transaction being built, then the command is immediately forwarded to the commander. Avoid building a loop between TransactionBuilder instances, as this will result in a stack overflow.
func (*TransactionBuilder) Register ¶ added in v1.6.0
func (builder *TransactionBuilder) Register(modifier ...TransactionModifier) error
Register creates a new transaction based on given modifier.
type TransactionError ¶ added in v1.6.0
type TransactionError struct { // Name is that of the transaction, if it was set. Name string // Index identifies the list index of tasks. Index int // Nested is the error returned from the task. In case of nested commands, this may be another TransactionError. Nested error }
TransactionError is returned from a transaction execution if a task failed.
func (TransactionError) Error ¶ added in v1.6.0
func (err TransactionError) Error() string
Error returns the path of the transaction task, combined with the nested error text.
func (TransactionError) Unwrap ¶ added in v1.6.0
func (err TransactionError) Unwrap() error
Unwrap returns the nested error, to support diving into nested errors.
type TransactionModifier ¶ added in v1.6.0
type TransactionModifier func(*Transaction) error
TransactionModifier is a function to change a transaction while it is being built.
func Forward ¶ added in v1.6.0
func Forward(task Task) TransactionModifier
Forward creates a modifier for a task that is called when doing a command. If a nested command is added to the transaction after the forward function, then the nested task is performed after the given task. If a nested command is added to the transaction before the forward function, then the nested task is performed before the given task.
func Named ¶ added in v1.6.0
func Named(name string) TransactionModifier
Named allows to name the current level of transaction.
func Nested ¶ added in v1.6.0
func Nested(nested func() error) TransactionModifier
Nested adds a further level to the currently registered transaction. Depending on the order of the modifier in the register, the corresponding tasks will be called in sequence. See other modifier.
func Reverse ¶ added in v1.6.0
func Reverse(task Task) TransactionModifier
Reverse creates a modifier for a task that is called when undoing a command. If a nested command is added to the transaction after the reverse function, then the nested task is performed before the given task. If a nested command is added to the transaction before the reverse function, then the nested task is performed after the given task.