@@ -39,9 +39,8 @@ gc.collect()`
39
39
class SerialConnection extends EventEmitter {
40
40
constructor ( ) {
41
41
super ( )
42
+ this . executing = false
42
43
this . rawRepl = false
43
- this . loadingFile = false
44
- this . loadingFileList = false
45
44
}
46
45
/**
47
46
* List all available serial ports (with vendor id)
@@ -91,13 +90,15 @@ class SerialConnection extends EventEmitter {
91
90
* @param {String } code String of code to be executed. Line breaks must be `\n`
92
91
*/
93
92
execute ( code ) {
93
+ this . emit ( 'execution-started' )
94
94
// TODO: break code in lines and `_execRaw` line by line
95
95
this . stop ( )
96
96
this . _enterRawRepl ( )
97
97
this . _executeRaw ( code )
98
- . then ( ( ) => {
99
- this . _exitRawRepl ( )
100
- } )
98
+ . then ( ( ) => {
99
+ this . emit ( 'execution-finished' )
100
+ this . _exitRawRepl ( )
101
+ } )
101
102
}
102
103
/**
103
104
* Evaluate a command/expression.
@@ -125,7 +126,6 @@ class SerialConnection extends EventEmitter {
125
126
*/
126
127
listFiles ( ) {
127
128
this . data = ''
128
- this . loadingFileList = true
129
129
this . execute ( codeListFiles )
130
130
}
131
131
/**
@@ -134,7 +134,6 @@ class SerialConnection extends EventEmitter {
134
134
*/
135
135
loadFile ( path ) {
136
136
this . data = ''
137
- this . loadingFile = true
138
137
this . execute ( codeLoadFile ( path ) )
139
138
}
140
139
/**
@@ -152,23 +151,23 @@ class SerialConnection extends EventEmitter {
152
151
pCode += codeCollectGarbage + '\n'
153
152
// `content` is what comes from the editor. We want to write it
154
153
// 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 ) => {
158
156
if ( line ) {
159
- var nlMarker = line . indexOf ( '\n' ) ;
160
- var crMarker = line . indexOf ( '\r' ) ;
161
157
// TODO: Sanitize line replace """ with \"""
162
158
// To avoid the string escaping with weirdly we encode
163
159
// the line plus the `\n` that we just removed to base64
164
160
pCode += `f.write("""${ line } """)`
165
161
if ( lineCount != lines . length - 1 ) {
166
162
pCode += `\nf.write('\\n')\n`
167
163
}
168
- lineCount ++ ;
169
164
}
170
165
} )
171
166
pCode += `\nf.close()\n`
167
+
168
+ this . once ( 'execution-finished' , ( ) => {
169
+ this . emit ( 'file-saved' )
170
+ } )
172
171
this . execute ( pCode )
173
172
}
174
173
@@ -190,39 +189,6 @@ class SerialConnection extends EventEmitter {
190
189
_eventHandler ( buffer ) {
191
190
const data = buffer . toString ( )
192
191
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
- }
226
192
}
227
193
/**
228
194
* Put REPL in raw mode
@@ -241,32 +207,21 @@ class SerialConnection extends EventEmitter {
241
207
* @param {String } command Command to be written on connected port
242
208
*/
243
209
_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
260
212
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' )
268
223
resolve ( )
269
- } )
224
+ } , finished * 10 )
270
225
} )
271
226
}
272
227
}
0 commit comments