Skip to content
This repository has been archived by the owner on Jan 14, 2024. It is now read-only.

Refactor control game instance #107

Merged
merged 10 commits into from
Aug 12, 2020
Merged

Conversation

aroquev00
Copy link
Contributor

I just refactored the controlGameInstance.js file to include async and await 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

@aroquev00 aroquev00 self-assigned this Aug 6, 2020
@aroquev00 aroquev00 linked an issue Aug 6, 2020 that may be closed by this pull request
Copy link
Contributor

@thefredo1000 thefredo1000 left a 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove?

Copy link
Contributor Author

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);
Copy link
Contributor

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 : /

Copy link
Contributor Author

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?

Copy link
Contributor Author

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!

Base automatically changed from linksMainScreen to master August 10, 2020 14:36
@@ -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) {
Copy link
Contributor

@brettapeters brettapeters Aug 10, 2020

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 });

Copy link
Contributor Author

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)?

Copy link
Contributor Author

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 });

@brettapeters
Copy link
Contributor

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'
Copy link
Contributor Author

@aroquev00 aroquev00 Aug 10, 2020

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?

@seanjohnite seanjohnite merged commit eefe752 into master Aug 12, 2020
@seanjohnite seanjohnite deleted the refactorControlGameInstance branch August 12, 2020 00:45
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Change Teachers Admin Panel to use Promises
4 participants