Gestor de deshacer

Deshacer es una característica necesaria en las aplicaciones modernas. Gaphor no es una excepción. Tener una función de deshacer en su lugar significa que puede cambiar el modelo y fácilmente volver a un estado anterior.

Resumen de las transacciones

The recording and playback of changes in Gaphor is handled by the the Undo Manager. The Undo Manager works transactionally. A transaction must succeed or fail as a complete unit. If the transaction fails in the middle, it is rolled back. In Gaphor this is achieved by the transaction module, which provides a context manager Transaction.

When transactions take place, they emit events when the top-most transaction begins and is finished. The event notifications are for the begin of the transaction, and the commit of the transaction if it is successful or the rollback of the transaction if it fails.

The Undo Manager only allows changes to be made in a transaction. If a change is made outside a transaction, an exception is raised.

Inicio de una transacción

  1. A Transaction object is created.

  2. TransactionBegin event is emitted.

  3. The UndoManager instantiates a new ActionStack which is the transaction object, and adds the undo action to the stack.

Se admiten transacciones anidadas para permitir que se añada una transacción dentro de otra que ya esté en curso.

Transacción exitosa

  1. A TransactionCommit event is emitted

  2. The UndoManager closes and stores the transaction.

Transacción fallida

  1. A TransactionRollback event is emitted.

  2. The UndoManager plays back all the recorded actions, but does not store it.

Transaction API

Nota

You only require transactions if the undo manager is active.

class gaphor.transaction.Transaction(event_manager, context: str | None = None)[fuente]

The transaction.

On start and end of a transaction an event is emitted. Transactions can be nested. Events are only emitted when the outermost transaction begins and finishes.

Note that transactions are a global construct.

>>> import gaphor.core.eventmanager
>>> event_manager = gaphor.core.eventmanager.EventManager()

Transactions can be nested. If the outermost transaction is committed or rolled back, an event is emitted.

It’s most convenient to use Transaction as a context manager:

>>> with Transaction(event_manager) as ctx:
...     ... # do actions
...     # in case the transaction should be rolled back:
...     ctx.rollback()

Events can be handled programmatically, although this is discouraged:

>>> tx = Transaction(event_manager)
>>> tx.commit()
commit()[fuente]

Commit the transaction.

The transaction is closed. A TransactionCommit event is emitted. If the transaction needs to be rolled back, a TransactionRollback event is emitted instead.

classmethod in_transaction() bool[fuente]

Are you running inside a transaction?

classmethod mark_rollback()[fuente]

Mark the transaction for rollback.

This operation itself will not close the transaction, instead it will allow you to elegantly revert changes.

rollback()[fuente]

Roll-back the transaction.

First, the transaction is closed. A TransactionRollback event is emitted.

class gaphor.event.TransactionBegin(context: str | None)[fuente]

This event denotes the beginning of a transaction.

Nested (sub-) transactions should not emit this signal.

class gaphor.event.TransactionCommit(context: str | None)[fuente]

This event is emitted when a transaction (toplevel) is successfully committed.

class gaphor.event.TransactionRollback(context: str | None)[fuente]

This event is emitted to tell the operation has failed.

If a set of operations fail (e.i. due to an exception) the transaction should be marked for rollback.

Referencias