Transactions
Provides functionality for reading data that might be involved in an ongoing,
uncommitted transaction initiated by Store:tx().
NOTE
This module only contains the implementation for Transactions.readTx. Writing transactions is handled in Store:tx
Purpose: When a Store:tx() operation begins, it might involve multiple steps
(e.g., reading current data, applying changes, writing back). If a server crashes
before the transaction is fully committed or rolled back, this module helps
determine the correct state to return.
It checks the status of the transaction ID (txId) stored separately in the DataStore.
If the transaction is still pending (represented as false) or has failed, it returns the last known
committed data. If the transaction has successfully committed (represented as nil), it applies the
transaction's patch (txPatch) to the previously committed data (committedData)
to return the newly committed state.
This ensures that readers either get the stable state before the transaction or the final state after a successful transaction, avoiding inconsistent intermediate states.
Types
ReadTxParams
interface ReadTxParams {txInfo: Types.TxInfo--
The transaction information associated with the data being read.
}Parameters required for the readTx function.
Functions
readTx
Transactions.readTx() → Promise<any>--
A Promise that resolves with the appropriate data (either committed or patched).
Reads data, considering the status of an associated transaction.
Checks the status of the transaction ID (txInfo.txId) in the DataStore.
-
If
txIdis nil (meaning no transaction was associated or it was cleaned up), returns thecommittedDatadirectly. - If
txIdexists:- Fetches the status from the DataStore using the
txIdas the key. - Status Convention:
-
nil: The transaction successfully committed. The correspondingtxPatchshould be applied tocommittedData. -
false: The transaction is still in progress or failed/rolled back. ThecommittedData(state before the transaction) should be returned.
-
- Applies the patch if the status is
niland a patch exists. - Returns
committedDataif the status isfalse.
- Fetches the status from the DataStore using the
Using nil to indicate a successful transaction allows for easier cleanup of
transaction markers in the DataStore. Instead of setting the marker to true
and only removing it after the txId is no longer needed, we can simply delete
the marker immediately after a successful transaction.
Errors
| Type | Description |
|---|---|
| string | Rejects if the transaction status indicates commitment (`nil`) but the `txPatch` is missing, or if DataStore operations fail (via `dataStoreRetry`). Propagates errors from `DataStore:GetAsync` via `dataStoreRetry`. |