Go to repository

Class History<A>

A basic implementation for the 3 interfaces giving a history.

The interface for Changeable is used to register the changes over which the other interfaces operate.

The interface for Compactable has precedence over the others, thus a compact undo all changes and all transactions, and numAllChanges and allChanges inform of ALL changes, in all transactions.

The interface for Transactional has precedence over the Undoable, thus undos can only undo changes in the current transaction -- to access previous transactions, a rollback is needed.

Type Parameters

  • A

Implements

Constructors

It creates a structure that is able to track the changes made to a certain initial value, with the posibility to undo and redo the changes, and to access the current version of it. Also transactions can be started and rolled back, and all changes can be compacted.

Type Parameters

  • A

Parameters

  • e: A

    the initial value of the history.

Returns History<A>

API

Add a change to the history in the current transaction, as the last version of the value.

Parameters

Returns void

Returns the list of all changes. The first value is the initial one and the last one is the previous to the current one.

Returns A[]

Commit all changes, leaving only the last one as the current one, deleting all registered changes.

Returns void

Describes the list of all values since the initial one in the transaction, and the resulting after each of the changes made in the current transaction since it started. The first element is the one after the first change on the initial value, and the last one is the more current one.

Returns A[]

Describes the current value in the history.

Returns A

Describe the number of all changes made to the value since the initial one was given (either at creation or after a save).

Returns number

Describes the number of changes made in the current transaction since it started.

Returns number

Describe the number of redos that can be performed on the current value.

Returns number

Describe the number of undos that can be performed on the current value.

Returns number

Redo the next change, if there is some undone. If there are no changes to redo, do nothing. After a change in the current value, all possible occurrences of redo are lost.

Returns void

Roll back the current transaction undoing all the changes made in it. After a transaction is rolled back, all the changes made in it are lost. The current value after the rollback is the last value of the previous transaction, or the initial value, if there is no transaction started.

Returns void

Start a new transaction, using the last version of the value from the previous transaction as the starting one of the new one. Once a transaction is started, the other operations work relative to it -- the only way to access previous transactions is rolling back the current one.

Returns void

Undo the last change made, leaving the possibility to recover it later, if no other operation changing the value occurs in between. If there are no changes to undo, do nothing.

Returns void

Auxiliaries

Private

It initializes the different internal properties for the structure, setting up the given element as the current version, with no changes and no transactions. It is supposed to be used at creation, and after a save.

Parameters

Returns void

Internal properties

Private
_changes: A[]

The array with all the changes registered through all transactions. It is related to the _currentValueIndex property, that indicates which of its elements is the current value.

Private
_currentValueIndex: number

An index to the array of _changes indicating which one is the current value. It has the property of being between 0 and the number of changes minus 1 (see REPRESENTATION INVARIANTS).

Private
_transactionOffset: number[]

An array indicating which of the elements of _changes is the initial value of each registered transaction. All its values are positive and less than the number of changes, so they really point to _changes positions, and they are stored in sorted in ascending order, because of the stack nature of transactions (see REPRESENTATION INVARIANTS).