Idiomatic representation of { buffer, bytesRead }

Jason Orendorff jason.orendorff at gmail.com
Wed Mar 4 11:06:57 UTC 2015


On Mon, Mar 2, 2015 at 3:45 PM, Domenic Denicola <d at domenic.me> wrote:
> While working on lower-level byte streams we're encountering a number of situations that need to return something along the lines of `{ buffer, bytesRead }`. (In this setting "buffer" = ArrayBuffer.) In the most general form the signature ends up being something like
>
>     { sourceBuffer, offset, bytesDesired } -> { newBuffer, bytesRead }

I very much like 2 and 3 because they provide the result type that the
user wants anyway. Slightly prefer DataView.

But you can support both, like this:

    pull(DataView) -> Promise<DataView>
    pull(TypedArrayView) -> Promise<TypedArrayView of the same type>

A view argument conveniently provides just the three pieces of
information you need, plus a type.

The lower-level primitive could take an optional fourth argument:

    pull(sourceBuffer, offset, bytesDesired,
resultConstructor=DataView) -> Promise<resultConstructor>

This could even be generic in resultConstructor, though it's a little
awkward because you have to divide by
resultConstructor.BYTES_PER_ELEMENT before invoking the constructor.

-j


More information about the es-discuss mailing list