Re: Alternative File API

On 8/17/09 12:33 AM, Michael Nordman wrote:
> Strictly speaking, I think the seperate 'Reader' class makes for a more
> correct API. The two corners above would not conflict since each would
> presumably be working with a distinct FileReader object. And the
> seperate 'Reader' with event handlers seems more javscript'y.
I agree with this. Reader+events 'feels' pretty much what I'd like to see.

>
> If we go with distinct 'Readers', maybe have the 'Data' object
> responsible for manufacturing them as there could be differences in
> where the 'Data' really resides and what it takes to retrieve it.
>
> var reader = data.createReader();

I'd do
var reader = new DataReader(someDataObject, DataReader.BINARY);
reader.onload =
   function(evt) {
     // grab the data using evt.target.getData();
   };
reader.read();


>
> File - represents a file in the file system
> BinaryData - represents a bag of binary bits (roughly analogous to a
> Gears.Blob)
> BinaryDataReader - an interface to read those bits
>
> File isa BinaryData
> XHR.ResponseBody isa BinaryData
> SQLBlob isa BinaryData

So whatever implements Data would be readable using DataReader.
(Note, I wouldn't call it BinaryData, but just Data, or something like 
that.)

So to read a file
var reader = new DataReader(input.files[0], DataReader.TEXT, "UTF-8");
reader.onload = handleLoad;
reader.read();

To read just some part of a file

var reader = new DataReader(input.files[0].slice(0, 1024), 
DataReader.TEXT, "UTF-8");
reader.onload = handleHeaderLoad;
reader.read();


Interface would be close to what Jonas proposed as an
alternative API
[Constructor, Implements=EventTarget]
interface DataReader {
   const unsigned short BINARY = 0;
   const unsigned short TEXT = 1;
   const unsigned short DATAURL = 2;

   DataReader(Data, getAsType [, encoding]);

   void read();
   void abort();

   const unsigned short INITIAL = 0;
   const unsigned short LOADING = 1;
   const unsigned short DONE = 2;
   readonly attribute unsigned short readyState;

   readonly attribute unsigned short dataType;
   readonly attribute DOMString encoding;

   readonly attribute unsigned long length;
   readonly attribute DOMString getData([unsigned long offset [,
                                         unsigned long length]]);

   attribute Function onloadstart;
   attribute Function onprogress;
   attribute Function onload;
   attribute Function onabort;
   attribute Function onerror;
   attribute Function onloadend;
};


-Olli

Received on Monday, 17 August 2009 09:46:29 UTC