-
Notifications
You must be signed in to change notification settings - Fork 13
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) => { | ||
if (!c.status) | ||
return; | ||
|
||
if (c.status === Lime.CommandStatus.SUCCESS) { | ||
if (!c.status) { | ||
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]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you need to check it? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That if statement seems to be impossible to be true. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'; | ||
|
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.
To prevent a memory leak caused by an error in this function execution, you can design it in that way:
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.
We actually need a race of 2 promises here, because the command must fail if there was no response before the timeout
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.
I didn't question that. I'm talking about the arrow function you pass on the first promise.
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.
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