firebase-firestore-lite icon indicating copy to clipboard operation
firebase-firestore-lite copied to clipboard

attempts in transaction leads to confusion

Open manwithsteelnerves opened this issue 4 years ago • 4 comments

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.

manwithsteelnerves avatar Jul 09 '20 09:07 manwithsteelnerves

Issue Label Bot is not confident enough to auto-label this issue. See dashboard for more details.

issue-label-bot[bot] avatar Jul 09 '20 09:07 issue-label-bot[bot]

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.

samuelgozi avatar Jul 09 '20 10:07 samuelgozi

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 avatar Jul 09 '20 10:07 manwithsteelnerves

@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.

samuelgozi avatar Jul 13 '20 09:07 samuelgozi