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

Prevent unfulfilled promises on sendCommand method #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

VictorBalbo
Copy link
Contributor

Prevent keeping unfulfilled promises on memory because of SendCommand method.

src/Client.js Outdated Show resolved Hide resolved
@henriquetorquato henriquetorquato self-assigned this Mar 5, 2020
@@ -275,27 +275,26 @@ export default class Client {

// sendCommand :: Command -> Number -> Promise Command
sendCommand(command, timeout = this._application.commandTimeout) {
var commandPromise = Promise.race([
const commandPromise = Promise.race([
new Promise((resolve, reject) => {
this._commandResolves[command.id] = (c) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

To prevent a memory leak caused by an error in this function execution, you can design it in that way:

() => {
    try {
        // logic here
    } catch (err) {
        return reject(...);
    } finally {
        delete this._commandResolves[command.id];
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We actually need a race of 2 promises here, because the command must fail if there was no response before the timeout

Copy link
Contributor

Choose a reason for hiding this comment

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

I didn't question that. I'm talking about the arrow function you pass on the first promise.

Copy link
Contributor

Choose a reason for hiding this comment

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

The try/catch block is a good way to prevent unhandled errors. By the way, it might help us to handle exceptions better at BLiP Portal

setTimeout(() => {
if (!this._commandResolves[command.id])
return;
if (!this._commandResolves[command.id]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you need to check it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just added the bracket, but this if is used to guarantee the promise will only be reject for commands with timeout

Copy link
Contributor

Choose a reason for hiding this comment

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

That if statement seems to be impossible to be true.

Copy link
Contributor

@ceifa ceifa Mar 13, 2020

Choose a reason for hiding this comment

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

But anyway, I don't think that returning a string can be a good thing, think that consumers are expecting a specific type, in the case a lime envelope, returning a different type can cause problems. Miss you, typescript :(

@ceifa ceifa self-assigned this Mar 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants