Implementing EventListeners on my own classes

48 views
Skip to first unread message

Jason Hall

unread,
May 17, 2012, 9:25:54 PM5/17/12
to General Dart Discussion
I'm interested in writing a class to upload files from the browser. It
will accept a File (or Blob), write it to some scratch space using the
File System API and start uploading chunks to the server via XHR. If
the browser crashes or the user browses away, uploading can continue
when they browse back.

I'd like to let users of the class subscribe to events when a chunk
has been uploaded (w/ which file and % complete), or when a complete
file is uploaded (w/ which file it was), and when all files are
finished uploading, i.e., when the queue is empty.

This seems like a job for event listener lists, as seems to be
idiomatic in the rest of Dart. e.g.,
button.on.click.add((Event e) => window.console.log('yay'));

or in my case something like:
uploader.on.progress.add((ProgressEvent e) =>
window.console.log('uploaded ${e.percent}% of ${e.filename}'));

I see the generated code for this is in
http://code.google.com/searchframe#TDGadvYaD94/trunk/dart/lib/html/dartium/html_dartium.dart
and seems to rely on private classes to wrap browser events. Is this
the approved way to do this in external-to-Dart code? Should I wrap
native JS browser events, or is there a better native Dart way to do
this?

Can I use a Completer/Future and just "complete" with different values
each time? Do Futures support being called more than once?

An example of this being done in pure Dart code would be fantastic, I
just couldn't find anything.

Thanks!
- Jason

John Messerly

unread,
May 17, 2012, 9:39:43 PM5/17/12
to Jason Hall, General Dart Discussion, Sean Eagan
On Thu, May 17, 2012 at 6:25 PM, Jason Hall <imja...@gmail.com> wrote:
I'm interested in writing a class to upload files from the browser. It
will accept a File (or Blob), write it to some scratch space using the
File System API and start uploading chunks to the server via XHR. If
the browser crashes or the user browses away, uploading can continue
when they browse back.

I'd like to let users of the class subscribe to events when a chunk
has been uploaded (w/ which file and % complete), or when a complete
file is uploaded (w/ which file it was), and when all files are
finished uploading, i.e., when the queue is empty.

This seems like a job for event listener lists, as seems to be
idiomatic in the rest of Dart. e.g.,
button.on.click.add((Event e) => window.console.log('yay'));

or in my case something like:
uploader.on.progress.add((ProgressEvent e) =>
window.console.log('uploaded ${e.percent}% of ${e.filename}'));



 
I see the generated code for this is in
http://code.google.com/searchframe#TDGadvYaD94/trunk/dart/lib/html/dartium/html_dartium.dart
and seems to rely on private classes to wrap browser events. Is this
the approved way to do this in external-to-Dart code? Should I wrap
native JS browser events, or is there a better native Dart way to do
this?

I certainly wouldn't base any in-Dart solution on the dart:html event classes. Those are pretty special due to the need to interact with the JS or Dartium DOM APIs. Writing the Event class in pure Dart seems more sensible if your class is a pure-Dart class.
 

Can I use a Completer/Future and just "complete" with different values
each time? Do Futures support being called more than once?


AFAIK, Futures don't support being called multiple times.
  
An example of this being done in pure Dart code would be fantastic, I
just couldn't find anything.

That was the closest I could find :)

Cheers!
- John

Reply all
Reply to author
Forward
0 new messages