Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feedback #1

Merged
merged 1 commit into from
Feb 5, 2019
Merged

Feedback #1

merged 1 commit into from
Feb 5, 2019

Conversation

gillesdemey
Copy link

I've done some small refactoring, tested locally by linking scratch-vm and scratch-gui :)

@@ -253,16 +253,15 @@ class Scratch3MQTTBlocks {
}

isConnected(){
if ( ! this._client ) return false;
return this._client.connected;
return this._client
Copy link
Author

Choose a reason for hiding this comment

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

ternary operators can reduce functions to a single return statement

Copy link
Owner

Choose a reason for hiding this comment

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

Not sure if I understand this correctly. How does this return then the "connected attribute of the _client object ?"

Copy link
Author

Choose a reason for hiding this comment

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

If this._client is defined we execute the ? this._client.connected statement, otherwise we return : false.

I've split up the statements into multiple lines, but it could have been one single line :)

return this._client ? this._client.connected : false

this._client.end();
}

if ( this.isConnected() ) {
Copy link
Author

Choose a reason for hiding this comment

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

refactored this to use the public interface instead :)

this._client.on('error',function(){

this._client.on('error', () => {
Copy link
Author

Choose a reason for hiding this comment

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

We'll need to use an arrow function here, those will automatically bind the this context inside the function to the one outside of it.

If we use a regular function, the this context inside of our closure is a new context and doesn't have the end() function defined.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

Copy link
Owner

Choose a reason for hiding this comment

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

Hmm, interesting... I guess I need to start learning javascript properly :)

@@ -303,60 +302,48 @@ class Scratch3MQTTBlocks {

disconnectFromBroker(args) {
console.log("[mqtt] disconnecting from MQTT broker... ");
if ( this._client != null ) { this._client.end(); }
if ( this._client ) { this._client.end(); }
Copy link
Author

Choose a reason for hiding this comment

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

Using truthy and falsey comparison instead of comparing with null or undefined values can eliminate potential bugs; because unfortunately the javascript equality matrix is pretty complicated https://dorey.github.io/JavaScript-Equality-Table/unified/ 😢

}

subscribe( args ) {
if ( !this.isConnected() ) {
Copy link
Author

Choose a reason for hiding this comment

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

Using early returns prevents nested if-statements and makes it a bit more readable when we're doing some validation before continuing.

@@ -36,7 +37,9 @@ const base = {
})
]
},
plugins: []
plugins: [
new NormalModuleReplacementPlugin(/^mqtt$/, "mqtt/dist/mqtt.js")
Copy link
Author

Choose a reason for hiding this comment

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

For some reason I couldn't get webpack to compile the mqtt dependency, so I had to manually refer it to the compiled source.

see mqttjs/MQTT.js#720

@binomaiheu
Copy link
Owner

binomaiheu commented Feb 4, 2019 via email

@binomaiheu binomaiheu merged commit 2c72635 into binomaiheu:develop Feb 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants