The Observable to subscribe to and read data from
The WritableStream to write data to
A subscription object with an unsubscribe method for cleanup
SerialError with code SerialErrorCode.WRITE_FAILED if writing to the stream fails
const data$ = from([
new TextEncoder().encode('Hello'),
new TextEncoder().encode('World'),
]);
const subscription = subscribeToWritable(data$, port.writable);
// Later, to cancel and clean up:
subscription.unsubscribe();
// Use with RxJS operators
const processedData$ = of('Hello, Serial!').pipe(
map((text) => new TextEncoder().encode(text))
);
subscribeToWritable(processedData$, port.writable);
Subscribe to an Observable and write its values to a WritableStream.
This utility function subscribes to an RxJS Observable and writes all emitted values to the provided WritableStream. This is commonly used to write Observable data to a serial port's writable stream.
The function returns a subscription object that can be used to unsubscribe and clean up. When unsubscribed, the writer lock will be released properly.