web-serial-rxjs API Documentation
    Preparing search index...

    Function subscribeToWritable

    • 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.

      Parameters

      • observable: Observable<Uint8Array<ArrayBufferLike>>

        The Observable to subscribe to and read data from

      • stream: WritableStream<Uint8Array<ArrayBufferLike>>

        The WritableStream to write data to

      Returns { unsubscribe: () => void }

      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);