firebase-firestore-lite
firebase-firestore-lite copied to clipboard
attempts in transaction leads to confusion
Over here, it checks if attempts is greater than zero.
Ideally, it would be great if the variable is named 'maxRetryAttempts' so that it guarantee an execution at-least once.
I could be wrong as you might have kept it intentional so that you can call a transaction with Zero attempts as well but not sure about why someone wants it to be not executed at all.
Please correct me incase if i'm wrong.
Issue Label Bot is not confident enough to auto-label this issue. See dashboard for more details.
Well, the default is 5 times. So I assume that if someone changes that, he will know what he is doing, but maybe I should replace that statement with a do while
to make sure it runs at least once.
I think it makes sense to see it as additional tries.
I tried wrapping it this way if it makes sense.
async runTransaction(updateFunction: (tx: ITransaction) => Promise<void>, retryAttempts : number = 0) : Promise<void> {
do {
const transaction = new FirestoreLiteDatabaseTransaction(db);
await updateFunction(transaction);
// Only retry on transaction errors.
try {
await transaction.commit();
break; // Stop trying if it succeeded.
} catch (e) {
// Only throw if the error is not related to the transaction, or it is the last attempt.
if (
retryAttempts === 0 ||
(e.status !== 'NOT_FOUND' && e.status !== 'FAILED_PRECONDITION')
)
throw Error(e);
}
retryAttempts--;
} while (retryAttempts >= 0);
}
@manwithsteelnerves sorry for the delayed reply. I think it makes more the way you wrote it now. I will update it but feel free to do it yourself if you want to.