-
Notifications
You must be signed in to change notification settings - Fork 7
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
|
||
// Triggers when the auth state change for instance when the user signs-in or signs-out. | ||
function authStateObserver(user) { | ||
if (user) { // User is signed in! | ||
// Everything starts working when the User logs in | ||
getActiveGameInstanceId(user); | ||
//getActiveGameInstanceId(user); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
const gameInstance = await queryActiveGameInstance(gameInstanceId); | ||
|
||
// Get the Game the GameInstance is using from DB | ||
const game = await queryGameDetails(gameInstance.gameId); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is a common workflow, is there a way to create a servlet that gets all the information you need? Three serial requests might take a little while : /
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure! But this would also mean three sequential reads in the servlet right? Is it still better to have these reads on the back end?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This gets fixed in another PR! So it's done!
@@ -74,15 +98,15 @@ function queryActiveGameInstanceDocument(gameInstanceId) { | |||
} | |||
|
|||
// Build the UI elements with all the info of the GameInstance | |||
function buildActiveGameInstanceUI(gameInstance, gameInstanceId) { | |||
function buildActiveGameInstanceUI(gameInstanceId, gameInstance, game) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would benefit from using named parameters so you don't have to worry about argument ordering. The common way people do named parameters in JS is this
function buildActiveGameInstanceUI({ gameInstanceId, gameInstance, game } = {}) {...}
buildActiveGameInstanceUI({ gameInstanceId: 123, gameInstance: gameInstance, game: game });
// Which can be shortened to this
buildActiveGameInstanceUI({ gameInstanceId: 123, gameInstance, game });
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So nice!! So in the last example I could call buildActiveGameInstanceUI
with just gameInstanceId, gameInstance, game
in any order (as long as they're named as the parameters of the function)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like this: buildActiveGameInstanceUI({ game, gameInstanceId, gameInstanceId });
You could throw a new error inside the functions that query Firebase if the doc isn't found and catch inside the calling function as long as you want to handle errors the same way in each situation. |
} else { | ||
throw 'User not registered in a GameInstance' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brettapeters So for example, there is a possibility that a user accesses the "Play game" page without having registered in any gameInstance previously, so in the loading process of the game, this read will fail and doc.data() will be null. In that case I want to stop everything and display in the UI something like "go register first and then come back". At which point of the calling functions should I place a catch
to abort loading the rest of the UI based on the nonexistent result of this query? In authStateObserver
which is the function that calls loadControlPanel
, where this query is called and the result used?
I just refactored the
controlGameInstance.js
file to includeasync
andawait
to improve readability and make functions have unique responsability. Now functions have very specific tasks instead of being chained like previously.I have one question though, what should I do to handle errors? Should I have the functions that query something from Firestore catch any error in case the document doesn't exist and then return it and catch it in the calling function?
Closes #96