Class StringWriteBuffer

Buffers a queue of strings.

When text is queued via add, it is chopped up into chunks and sent in serial to the dataHandler function. Data is processed at a set rate, by default 10ms.

const dataHandler = (data:string) => {
// Do something with queued data.
// eg. send to serial port
}

// Create a buffer with a chunk size of 100 characters
const b = new StringWriteBuffer(dataHandler, { chunkSize: 100 });
b.add('some text'); // Write to buffer
// dataHandler will be called until queued data is empty

It's also possible to get the buffer as a WritableStream:

const dataHandler = (data:string) => { ... }
const b = new StringWriteBuffer(dataHandler, 100);
const s = b.writable();

Other functions:

b.close(); // Close buffer
b.clear(); // Clear queued data, but don't close anything

Constructors

  • Constructor

    Parameters

    • dataHandler: ((data: string) => Promise<void>)

      Calback to 'send' data onwards

        • (data): Promise<void>
        • Parameters

          • data: string

          Returns Promise<void>

    • opts: StringWriteBufferOpts = {}

      Options

    Returns StringWriteBuffer

Properties

chunkSize: number
closed: boolean = false
paused: boolean = false
queue: QueueMutable<string> = ...
stream: undefined | WritableStream<string>
writer: Continuously

Accessors

  • get isClosed(): boolean
  • Returns true if close has been called.

    Returns boolean

Methods

  • Adds some queued data to send. Longer strings are automatically chunked up according to the buffer's settings.

    Throws an error if close has been called.

    Parameters

    • stringToQueue: string

    Returns void

  • Clear queued data.

    Throws an error if close has been called.

    Returns void

  • Close writer (async)

    Returns Promise<void>

  • Run in a continunously loop to process queued data

    Returns Promise<boolean>

    False if queue is empty and loop should stop. True if it shoud continue.

  • Gets the buffer as a writable stream.

    Do not close stream directly, use .close on this class instead.

    Throws an error if .close() has been called.

    Returns WritableStream<string>

    Underlying stream