Skip to content

Commit cc45de3

Browse files
committed
Execution and file-save events
1 parent 0d7232c commit cc45de3

File tree

3 files changed

+30
-39
lines changed

3 files changed

+30
-39
lines changed

micropython.js

Lines changed: 12 additions & 13 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

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)