Re: Is BlobBuilder needed?

On Mon, Oct 24, 2011 at 4:33 PM, Eric U <ericu@google.com> wrote:
> On Mon, Oct 24, 2011 at 3:52 PM, Jonas Sicking <jonas@sicking.cc> wrote:
>> Hi everyone,
>>
>> It was pointed out to me on twitter that BlobBuilder can be replaced
>> with simply making Blob constructable. I.e. the following code:
>>
>> var bb = new BlobBuilder();
>> bb.append(blob1);
>> bb.append(blob2);
>> bb.append("some string");
>> bb.append(myArrayBuffer);
>> var b = bb.getBlob();
>>
>> would become
>>
>> b = new Blob([blob1, blob2, "some string", myArrayBuffer]);
>>
>> or look at it another way:
>>
>> var x = new BlobBuilder();
>> becomes
>> var x = [];
>>
>> x.append(y);
>> becomes
>> x.push(y);
>>
>> var b = x.getBlob();
>> becomes
>> var b = new Blob(x);
>>
>> So at worst there is a one-to-one mapping in code required to simply
>> have |new Blob|. At best it requires much fewer lines if the page has
>> several parts available at once.
>>
>> And we'd save a whole class since Blobs already exist.
>
> It does look cleaner this way, and getting rid of a whole class would
> be very nice.
>
> The only things that this lacks that BlobBuilder has are the endings
> parameter for '\n' conversion in text and the content type.  The
> varargs constructor makes it awkward to pass in flags of any
> sort...any thoughts on how to do that cleanly?

The content-type can and should be added to the ctor.

Line-endings is a problem though. I'm not fully sure what the use case
is for them, and the spec does have an issue-marked asking if
line-ending conversion is needed at all. Worst case we could do them
as a separate function which does native-lineending-conversion for a
string.

I'm not fully sold on the var-args thing. It gets very awkward with
the content-type argument, and like you point out, it does make the
API less future proof.

It's also a bit scary to rely on a ES6 feature while ES6 is as
unstable as it still is.

/ Jonas

Received on Monday, 24 October 2011 23:40:36 UTC