Here we go, PR reworked. Still fixes #172 and #258, with no breaking changes.
The transaction in insertInitialDoc should be removed now, but I haven't touched it yet.
You'll see that I rewrote the function with if/elses. I know you're not a fan but it was getting too complex to deal with all the possible cases without those. We can argue the pros and cons at some point ;)
I also commented a lot, I like to be able to understand what a code does without actually reading the code, especially when there is a lot going on like here. You should be able to follow the logic super easily and see if I forgot something.
Beyond the fixes and restructuring, there's a minor improvement as I took into account the success of the initial document insert to fullfill the promise or not, which was previously ignored.
Here we go, PR reworked. Still fixes #172 and #258, with no breaking changes. The transaction in
insertInitialDoc
should be removed now, but I haven't touched it yet.You'll see that I rewrote the function with if/elses. I know you're not a fan but it was getting too complex to deal with all the possible cases without those. We can argue the pros and cons at some point ;) I also commented a lot, I like to be able to understand what a code does without actually reading the code, especially when there is a lot going on like here. You should be able to follow the logic super easily and see if I forgot something.
You'll see what I did with
fromCache
metadata instead ofhasPendingWrites
to ignore the irrelevant local calls, check out Firestore's documentation about it here: https://firebase.google.com/docs/firestore/query-data/listenBeyond the fixes and restructuring, there's a minor improvement as I took into account the success of the initial document insert to fullfill the promise or not, which was previously ignored.