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
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

if (!c.status)
return;

if (c.status === Lime.CommandStatus.SUCCESS) {
if (!c.status){
VictorBalbo marked this conversation as resolved.
Show resolved Hide resolved
reject(new ClientError('Command received without a status'));
} else if (c.status === Lime.CommandStatus.SUCCESS) {
resolve(c);
}
else {
} else {
const cmd = JSON.stringify(c);
reject(new ClientError(cmd));
}

delete this._commandResolves[command.id];
};
}),
new Promise((_, reject) => {
new Promise((resolve, reject) => {
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 :(

resolve('Command already resolved');
}

delete this._commandResolves[command.id];
command.status = 'failure';
Expand Down