Class SnapshotParticipant<T>
- Type Parameters:
T
- The objects that this participant uses to save its state snapshots.
- All Implemented Interfaces:
TransactionContext.CloseCallback
,TransactionContext.OuterCloseCallback
- Direct Known Subclasses:
SingleStackStorage
,SingleVariantStorage
T
in case it needs to revert to a previous state.
How to use from subclasses
- Call
updateSnapshots(net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext)
right before the state of your subclass is modified in a transaction. - Override
createSnapshot()
: it is called when necessary to create an object representing the state of your subclass. - Override
readSnapshot(T)
: it is called when necessary to revert to a previous state of your subclass. - You may optionally override
onFinalCommit()
: it is called at the of a transaction that modified the state. For example, it could contain a call tomarkDirty()
. - (Advanced!) You may optionally override
releaseSnapshot(T)
: it is called once a snapshot object will not be used, for example you may wish to pool expensive state objects.
More technical explanation
updateSnapshots(net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext)
should be called before any modification.
This will save the state of this participant using createSnapshot()
if no state was already saved for that transaction.
When the transaction is aborted and changes need to be rolled back, readSnapshot(T)
will be called
to signal that the current state should revert to that of the snapshot.
The snapshot object is then released, and can be cached for subsequent use, or discarded.
When an outer transaction is committed, readSnapshot(T)
will not be called so that the current state of this participant
is retained. releaseSnapshot(T)
will be called because the snapshot is not necessary anymore,
and onFinalCommit()
will be called after the transaction is closed.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Perform an action after the top-level transaction is closed.protected abstract T
Return a new nonnull object containing the current state of this participant.void
onClose
(TransactionContext transaction, TransactionContext.Result result) Perform an action when a transaction is closed.protected void
Called after an outer transaction succeeded, to perform irreversible actions such asmarkDirty()
or neighbor updates.protected abstract void
readSnapshot
(T snapshot) Roll back to a state previously created bycreateSnapshot()
.protected void
releaseSnapshot
(T snapshot) Signals that the snapshot will not be used anymore, and is safe to cache for next calls tocreateSnapshot()
, or discard entirely.void
updateSnapshots
(TransactionContext transaction) Update the stored snapshots so that the changes happening as part of the passed transaction can be correctly committed or rolled back.
-
Constructor Details
-
SnapshotParticipant
public SnapshotParticipant()
-
-
Method Details
-
createSnapshot
Return a new nonnull object containing the current state of this participant.null
may not be returned, or an exception will be thrown! -
readSnapshot
Roll back to a state previously created bycreateSnapshot()
. -
releaseSnapshot
Signals that the snapshot will not be used anymore, and is safe to cache for next calls tocreateSnapshot()
, or discard entirely. -
onFinalCommit
protected void onFinalCommit()Called after an outer transaction succeeded, to perform irreversible actions such asmarkDirty()
or neighbor updates. -
updateSnapshots
Update the stored snapshots so that the changes happening as part of the passed transaction can be correctly committed or rolled back. This function should be called every time the participant is about to change its internal state as part of a transaction. -
onClose
Description copied from interface:TransactionContext.CloseCallback
Perform an action when a transaction is closed.- Specified by:
onClose
in interfaceTransactionContext.CloseCallback
- Parameters:
transaction
- The closed transaction. OnlyTransactionContext.nestingDepth()
,TransactionContext.getOpenTransaction(int)
andTransactionContext.addOuterCloseCallback(net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext.OuterCloseCallback)
may be called on that transaction.TransactionContext.addCloseCallback(net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext.CloseCallback)
may additionally be called on parent transactions (accessed throughTransactionContext.getOpenTransaction(int)
for lower nesting depths).result
- The result of this transaction: whether it was committed or aborted.
-
afterOuterClose
Description copied from interface:TransactionContext.OuterCloseCallback
Perform an action after the top-level transaction is closed.- Specified by:
afterOuterClose
in interfaceTransactionContext.OuterCloseCallback
- Parameters:
result
- The result of the top-level transaction.
-