@@ -11,48 +11,7 @@ function isBrokenPipeError(error) {
1111 return / b r o k e n p i p e | E P I P E | o s e r r o r 3 2 | c o d e : \s * 3 2 / i. test ( s ) ;
1212}
1313
14- /**
15- * Async generator that polls the serial port for incoming data
16- * Similar to streamAsyncIterable in WebSerial but uses polling instead of streams
17- */
18- async function * pollSerialData ( path , keepReadingFlag ) {
19- try {
20- while ( keepReadingFlag ( ) ) {
21- try {
22- // Non-blocking read with short timeout
23- const result = await invoke ( "plugin:serialplugin|read_binary" , {
24- path,
25- size : 256 ,
26- timeout : 10 ,
27- } ) ;
28-
29- if ( result && result . length > 0 ) {
30- yield new Uint8Array ( result ) ;
31- }
32-
33- // Small delay between polls to avoid overwhelming the system
34- await new Promise ( ( resolve ) => setTimeout ( resolve , 5 ) ) ;
35- } catch ( error ) {
36- const msg = error ?. message || ( error ?. toString ? error . toString ( ) : "" ) ;
37- // Timeout is expected when no data available
38- if ( msg && msg . toLowerCase ( ) . includes ( "no data received" ) ) {
39- // Continue polling
40- await new Promise ( ( resolve ) => setTimeout ( resolve , 5 ) ) ;
41- continue ;
42- }
43- if ( isBrokenPipeError ( msg ) ) {
44- console . error ( `${ logHead } Fatal poll error (broken pipe) on ${ path } :` , error ) ;
45- throw error ;
46- }
47- console . warn ( `${ logHead } Poll error:` , error ) ;
48- // Continue polling
49- await new Promise ( ( resolve ) => setTimeout ( resolve , 5 ) ) ;
50- }
51- }
52- } finally {
53- console . log ( `${ logHead } Polling stopped for ${ path } ` ) ;
54- }
55- }
14+ // Note: We avoid async generator + for-await-of to improve compatibility in some runtimes.
5615
5716/**
5817 * TauriSerial protocol implementation using tauri-plugin-serialplugin
@@ -141,10 +100,10 @@ class TauriSerial extends EventTarget {
141100 let productId = undefined ;
142101
143102 if ( info . vid ) {
144- vendorId = typeof info . vid === "number" ? info . vid : parseInt ( info . vid , 10 ) ;
103+ vendorId = typeof info . vid === "number" ? info . vid : Number . parseInt ( info . vid , 10 ) ;
145104 }
146105 if ( info . pid ) {
147- productId = typeof info . pid === "number" ? info . pid : parseInt ( info . pid , 10 ) ;
106+ productId = typeof info . pid === "number" ? info . pid : Number . parseInt ( info . pid , 10 ) ;
148107 }
149108
150109 return {
@@ -204,10 +163,10 @@ class TauriSerial extends EventTarget {
204163 let productId = undefined ;
205164
206165 if ( info . vid ) {
207- vendorId = typeof info . vid === "number" ? info . vid : parseInt ( info . vid , 10 ) ;
166+ vendorId = typeof info . vid === "number" ? info . vid : Number . parseInt ( info . vid , 10 ) ;
208167 }
209168 if ( info . pid ) {
210- productId = typeof info . pid === "number" ? info . pid : parseInt ( info . pid , 10 ) ;
169+ productId = typeof info . pid === "number" ? info . pid : Number . parseInt ( info . pid , 10 ) ;
211170 }
212171
213172 return {
@@ -309,12 +268,41 @@ class TauriSerial extends EventTarget {
309268
310269 async readLoop ( ) {
311270 try {
312- for await ( let value of pollSerialData ( this . connectionId , ( ) => this . reading ) ) {
313- this . dispatchEvent ( new CustomEvent ( "receive" , { detail : value } ) ) ;
271+ while ( this . reading ) {
272+ try {
273+ // Non-blocking read with short timeout
274+ const result = await invoke ( "plugin:serialplugin|read_binary" , {
275+ path : this . connectionId ,
276+ size : 256 ,
277+ timeout : 10 ,
278+ } ) ;
279+
280+ if ( result && result . length > 0 ) {
281+ this . dispatchEvent ( new CustomEvent ( "receive" , { detail : new Uint8Array ( result ) } ) ) ;
282+ }
283+
284+ // Small delay between polls to avoid overwhelming the system
285+ await new Promise ( ( resolve ) => setTimeout ( resolve , 5 ) ) ;
286+ } catch ( error ) {
287+ const msg = error ?. message || ( error ?. toString ? error . toString ( ) : "" ) ;
288+ // Timeout is expected when no data available
289+ if ( msg && msg . toLowerCase ( ) . includes ( "no data received" ) ) {
290+ await new Promise ( ( resolve ) => setTimeout ( resolve , 5 ) ) ;
291+ continue ;
292+ }
293+ if ( isBrokenPipeError ( msg ) ) {
294+ console . error ( `${ logHead } Fatal poll error (broken pipe) on ${ this . connectionId } :` , error ) ;
295+ throw error ;
296+ }
297+ console . warn ( `${ logHead } Poll error:` , error ) ;
298+ await new Promise ( ( resolve ) => setTimeout ( resolve , 5 ) ) ;
299+ }
314300 }
315301 } catch ( error ) {
316302 console . error ( `${ logHead } Error in read loop:` , error ) ;
317303 this . handleFatalSerialError ( error ) ;
304+ } finally {
305+ console . log ( `${ logHead } Polling stopped for ${ this . connectionId || "<no-port>" } ` ) ;
318306 }
319307 }
320308
0 commit comments