Thursday, March 1, 2012

Improvements to the dart:io libraries

Posted by Seth Ladd

Mads Ager, engineer on the Dart server and IO libraries, has just posted some breaking changes to the dart:io libraries.


One-shot methods now take their callback as an argument.

Old 'n busted:

var f = new File('myfile.txt'); 
f.exists(); 
f.existsHandler = (result) { 
  // do stuff. 
}; 

New hotness:

var f = new File('myfile.txt'); 
f.exists((result) { 
  // do stuff. 
}); 

For active objects such as streams that emit events, the event handlers have changed names from eventHandler to onEvent.

Old 'n busted:

stdin.dataHandler = () { 
  // do stuff. 
}; 

New hotness:

stdin.onData = () { 
  // do stuff. 
}; 

Why not follow the lead of the dart:html libraries and use the convention of object.on.event.add(handler)? Mads explains:
We did consider going with object.on.event.add(handler). However,
having a collection of handlers for dart:io events usually doesn't
make sense. Then you are down to object.on.event = stuff. At that
point I think it is better to just have object.onEvent = stuff.
As always, continue the discussion on the mailing list and file issues in our public issue tracker. Thanks!