Skip to content

Bugfix/run twice #10

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

Merged
merged 4 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
34 changes: 29 additions & 5 deletions micropython.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class MicroPythonBoard {
constructor() {
this.port = null
this.serial = null
this.reject_run = null
}

list_ports() {
Expand Down Expand Up @@ -113,7 +114,10 @@ class MicroPythonBoard {
}

async get_prompt() {
const out = await this.write_and_read_until(`\r\x02\x03`, '\r\n>>>')
await sleep(100)
await this.stop()
await sleep(100)
const out = await this.write_and_read_until(`\r\x03\x02`, '\r\n>>>')
return Promise.resolve(out)
}

Expand Down Expand Up @@ -147,10 +151,22 @@ class MicroPythonBoard {

async run(code, data_consumer) {
data_consumer = data_consumer || function() {}
await this.enter_raw_repl()
const output = await this.exec_raw(code || '#', data_consumer)
await this.exit_raw_repl()
return Promise.resolve(output)
return new Promise(async (resolve, reject) => {
if (this.reject_run) {
this.reject_run(new Error('re-run'))
this.reject_run = null

Choose a reason for hiding this comment

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

This line is not necessary here, but if you want to keep setting the rejected promise to null, maybe you can have a method for all three occurrences.

function ensureRejected(message) {
  if (this.reject_run) {
    this.reject_run(new Error(message));
    this.reject_run = null;
  }
}

Any reason you prefer rejecting with a string instead of an Error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, it should definitely be an error. I just don't find the helper function really helpful here. I'm also not sure if this is going to be the final solution ™️ so I'd prefer keeping things a bit more unrolled.

}
this.reject_run = reject
try {
await this.enter_raw_repl()
const output = await this.exec_raw(code || '#', data_consumer)
await this.exit_raw_repl()
return resolve(output)
} catch (e) {
reject(e)

Choose a reason for hiding this comment

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

Question: if there is an error here, and the promise is rejected, then the next run call will find this._reject_run to be not null and will reject it, but nothing will happen, as it has already been rejected. Is this expected behavior, or would it be better to set this.reject_run to null in the catch?

this.reject_run = null
}
})
}

async eval(k) {
Expand All @@ -159,12 +175,20 @@ class MicroPythonBoard {
}

async stop() {
if (this.reject_run) {
this.reject_run(new Error('pre stop'))
this.reject_run = null
}
// Dismiss any data with ctrl-C
await this.serial.write(Buffer.from(`\x03`))
return Promise.resolve()
}

async reset() {
if (this.reject_run) {
this.reject_run(new Error('pre reset'))
this.reject_run = null
}
// Dismiss any data with ctrl-C
await this.serial.write(Buffer.from(`\x03`))
// Soft reboot
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "micropython.js",
"version": "1.3.5",
"version": "1.4.1",
"description": "Interpretation of pyboard.py in javascript",
"main": "micropython.js",
"scripts": {
Expand Down