Skip to content

Commit 684af42

Browse files
authored
Merge pull request #4 from arduino/bugfix/save-large-files
Bugfix/save large files
2 parents b6021f9 + cc45de3 commit 684af42

File tree

3 files changed

+43
-96
lines changed

3 files changed

+43
-96
lines changed

micropython.js

Lines changed: 25 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ gc.collect()`
3939
class SerialConnection extends EventEmitter {
4040
constructor() {
4141
super()
42+
this.executing = false
4243
this.rawRepl = false
43-
this.loadingFile = false
44-
this.loadingFileList = false
4544
}
4645
/**
4746
* List all available serial ports (with vendor id)
@@ -91,13 +90,15 @@ class SerialConnection extends EventEmitter {
9190
* @param {String} code String of code to be executed. Line breaks must be `\n`
9291
*/
9392
execute(code) {
93+
this.emit('execution-started')
9494
// TODO: break code in lines and `_execRaw` line by line
9595
this.stop()
9696
this._enterRawRepl()
9797
this._executeRaw(code)
98-
.then(() => {
99-
this._exitRawRepl()
100-
})
98+
.then(() => {
99+
this.emit('execution-finished')
100+
this._exitRawRepl()
101+
})
101102
}
102103
/**
103104
* Evaluate a command/expression.
@@ -125,7 +126,6 @@ class SerialConnection extends EventEmitter {
125126
*/
126127
listFiles() {
127128
this.data = ''
128-
this.loadingFileList = true
129129
this.execute(codeListFiles)
130130
}
131131
/**
@@ -134,7 +134,6 @@ class SerialConnection extends EventEmitter {
134134
*/
135135
loadFile(path) {
136136
this.data = ''
137-
this.loadingFile = true
138137
this.execute(codeLoadFile(path))
139138
}
140139
/**
@@ -152,23 +151,23 @@ class SerialConnection extends EventEmitter {
152151
pCode += codeCollectGarbage + '\n'
153152
// `content` is what comes from the editor. We want to write it
154153
// line one by one on a file so we split by `\n`
155-
var lineCount = 0;
156-
var lines = content.split('\r\n')
157-
lines.forEach((line) => {
154+
let lines = content.split('\r\n')
155+
lines.forEach((line, lineCount) => {
158156
if (line) {
159-
var nlMarker = line.indexOf('\n');
160-
var crMarker = line.indexOf('\r');
161157
// TODO: Sanitize line replace """ with \"""
162158
// To avoid the string escaping with weirdly we encode
163159
// the line plus the `\n` that we just removed to base64
164160
pCode += `f.write("""${line}""")`
165161
if(lineCount != lines.length - 1){
166162
pCode += `\nf.write('\\n')\n`
167163
}
168-
lineCount++;
169164
}
170165
})
171166
pCode += `\nf.close()\n`
167+
168+
this.once('execution-finished', () => {
169+
this.emit('file-saved')
170+
})
172171
this.execute(pCode)
173172
}
174173

@@ -190,39 +189,6 @@ class SerialConnection extends EventEmitter {
190189
_eventHandler(buffer) {
191190
const data = buffer.toString()
192191
this.emit('output', data)
193-
194-
// Getting data that should be sent to frontend
195-
// Loading file content, listing files, etc
196-
// if (data.indexOf('<REC>') !== -1) {
197-
// this.recordingData = true
198-
// }
199-
// if (this.recordingData) {
200-
// this.data += data
201-
// }
202-
// if (data.indexOf('<EOF>') !== -1) {
203-
// const iofREC = this.data.indexOf('<REC>')
204-
// const rec = this.data.indexOf('<REC>\r\n')+7
205-
// const eof = this.data.indexOf('<EOF>')
206-
// if (this.loadingFile) {
207-
// this.emit('file-loaded', this.data.slice(rec, eof))
208-
// this.loadingFile = false
209-
// }
210-
// if (this.loadingFileList) {
211-
// this.emit('file-list-loaded', this.data.slice(rec, eof))
212-
// this.loadingFileList = false
213-
// }
214-
// this.recordingData = false
215-
// }
216-
217-
if (this.rawRepl && data.indexOf('\n>>> ') != -1) {
218-
this.emit('execution-finished')
219-
this.rawRepl = false
220-
}
221-
222-
if (!this.rawRepl && data.indexOf('raw REPL;') != -1) {
223-
this.emit('execution-started')
224-
this.rawRepl = true
225-
}
226192
}
227193
/**
228194
* Put REPL in raw mode
@@ -241,32 +207,21 @@ class SerialConnection extends EventEmitter {
241207
* @param {String} command Command to be written on connected port
242208
*/
243209
_executeRaw(command) {
244-
const writePromise = (buffer) => {
245-
return new Promise((resolve, reject) => {
246-
setTimeout(() => {
247-
this.port.write(buffer, (err) => {
248-
if (err) return reject()
249-
resolve()
250-
})
251-
}, 1)
252-
})
253-
}
254-
const l = 1024
255-
let slices = []
256-
for(let i = 0; i < command.length; i+=l) {
257-
let slice = command.slice(i, i+l)
258-
slices.push(slice)
259-
}
210+
let p = 0
211+
const l = 256
260212
return new Promise((resolve, reject) => {
261-
slices.reduce((cur, next) => {
262-
return cur.then(() => {
263-
return writePromise(next)
264-
})
265-
}, Promise.resolve())
266-
.then()
267-
.then(() => {
213+
for(let i = 0; i < command.length; i+=l) {
214+
let slice = command.slice(i, i+l)
215+
setTimeout(() => {
216+
this.port.write(slice)
217+
}, p*10)
218+
p += 1
219+
}
220+
let finished = (command.length / l) + 1
221+
setTimeout(() => {
222+
this.port.write('\x04')
268223
resolve()
269-
})
224+
}, finished * 10)
270225
})
271226
}
272227
}

preload.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@ serialBus.on('connect', (p) => {
3535
})
3636
connection.on('execution-started', () => {
3737
console.log('serialBus', 'execution-started')
38-
serialBus.emit('running')
38+
serialBus.emit('execution-started')
3939
})
4040
connection.on('execution-finished', () => {
4141
console.log('serialBus', 'execution-finished')
42-
serialBus.emit('stopped')
42+
serialBus.emit('execution-finished')
43+
})
44+
connection.on('file-saved', () => {
45+
console.log('serialBus', 'file-saved')
46+
serialBus.emit('file-saved')
4347
})
4448
connection.open(p)
4549
})

ui/blank/store.js

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
function store(state, emitter) {
22
state.connected = false
3+
state.executing = false
4+
35
state.isPortDialogOpen = false
46
state.ports = []
57
state.panel = 'terminal' // terminal | files
@@ -92,27 +94,23 @@ function store(state, emitter) {
9294

9395
emitter.on('list-board-folder', () => {
9496
console.log('list-board-folder')
95-
9697
let outputBuffer = ''
9798
function parseData(o) {
9899
outputBuffer += o
99100
rawMessage = extractREPLMessage(outputBuffer)
100101
if (rawMessage) {
102+
// console.log('raw message', rawMessage, outputBuffer)
101103
// Prepare to parse JSON
102-
// console.log('raw message', rawMessage)
103104
rawMessage = rawMessage.replace(/'/g, `"`)
104105
try {
105106
let jsonMessage = JSON.parse(rawMessage)
106107
state.boardFiles = jsonMessage
107108
emitter.emit('render')
108-
} catch(e) {
109-
110-
}
109+
} catch(e) {}
111110
window.serialBus.off('data', parseData)
112111
}
113112
}
114113
window.serialBus.on('data', parseData)
115-
116114
window.serialBus.emit('list-files')
117115
})
118116
emitter.on('select-board-file', (file) => {
@@ -131,9 +129,7 @@ function store(state, emitter) {
131129
}
132130
}
133131
window.serialBus.on('data', parseData)
134-
135132
window.serialBus.emit('load-file', file)
136-
137133
emitter.emit('render')
138134
})
139135

@@ -164,19 +160,15 @@ function store(state, emitter) {
164160
}
165161

166162
if (state.selectedDevice === 'disk') {
167-
window.diskBus.emit(
168-
'save-file',
169-
{
170-
folder: state.diskFolder,
171-
filename: state.selectedFile,
172-
content: editor.getValue()
173-
}
174-
)
163+
window.diskBus.emit( 'save-file', {
164+
folder: state.diskFolder,
165+
filename: state.selectedFile,
166+
content: editor.getValue()
167+
})
175168
}
176169

177170
if (state.selectedDevice === 'board') {
178171
window.serialBus.emit('save-file', state.selectedFile, editor.getValue())
179-
setTimeout(() => emitter.emit('list-board-folder'), 100)
180172
}
181173
})
182174
emitter.on('remove-file', () => {
@@ -252,10 +244,8 @@ function store(state, emitter) {
252244
console.log('send-file-to-board')
253245
let editor = state.cache(AceEditor, 'editor').editor
254246
window.serialBus.emit('save-file', state.selectedFile, editor.getValue())
255-
emitter.emit('update-files')
256247
})
257248

258-
259249
window.serialBus.on('connected', (port) => {
260250
console.log('serialBus', 'connected', port)
261251
state.connected = true
@@ -264,11 +254,6 @@ function store(state, emitter) {
264254
emitter.emit('list-board-folder')
265255
emitter.emit('render')
266256
})
267-
window.serialBus.on('serialBus', (port) => {
268-
console.log('serialBus', 'disconnected', port)
269-
state.connected = false
270-
emitter.emit('render')
271-
})
272257
window.serialBus.on('ports', (ports) => {
273258
console.log('serialBus', 'ports', ports)
274259
state.ports = ports
@@ -279,6 +264,9 @@ function store(state, emitter) {
279264
state.cache(XTerm, 'terminal').term.write(buffer)
280265
state.cache(XTerm, 'terminal').term.scrollToBottom()
281266
})
267+
window.serialBus.on('file-saved', () => {
268+
setTimeout(() => emitter.emit('update-files'), 100)
269+
})
282270

283271
window.diskBus.on('folder-opened', ({ folder, files }) => {
284272
console.log('diskBus', 'folder-opened', folder, files)

0 commit comments

Comments
 (0)