The ReadableStream to convert to an Observable
An Observable that emits Uint8Array chunks from the stream
SerialError with code SerialErrorCode.READ_FAILED if reading from the stream fails
// Convert a serial port's readable stream to an Observable
const readable$ = readableToObservable(port.readable);
readable$.subscribe({
next: (chunk) => {
console.log('Received chunk:', chunk);
},
complete: () => {
console.log('Stream completed');
},
error: (error) => {
console.error('Stream error:', error);
},
});
// Use with RxJS operators
readableToObservable(port.readable)
.pipe(
map((chunk) => new TextDecoder().decode(chunk)),
filter((text) => text.includes('OK'))
)
.subscribe((text) => console.log('Filtered text:', text));
Convert a ReadableStream to an RxJS Observable.
This utility function converts a Web Streams API ReadableStream into an RxJS Observable, allowing you to use RxJS operators with stream data. The Observable will emit Uint8Array chunks as they are read from the stream, complete when the stream ends, or error if the stream encounters an error.
The returned Observable handles stream cleanup automatically - if you unsubscribe before the stream completes, the reader lock will be released properly.