Skip to content
Sam Roberts edited this page Feb 17, 2014 · 3 revisions

Enumeration of Use Cases

Super-simple case

Very simple case. All "resources" and tasks are created and used within one task. Note that 'complete' might get called under certain scenarios. I'm not sure if it's a good idea to allow this for tasks-lite, but we could deal with it. The task-lite constraint would be that the user must call complete once or at least once.

new zone.Zone(function downloadTask() {
  var req = http.get('http://www.google.com', function(res) {
    var file = fs.createWriteStream('google.html');
    res.pipe(file);
  });
  }).setCallback(function(err) {
  if (err)
    console.log('Downloading failed! %s', err);
  else
    console.log('Download succeeded');
});

Slightly more complicated case: stream resource inherited from parent task.

http.createServer(function(req, res) {
  new Task(function sendResponseTask(complete) {
    fs.readFile('template', { encoding: 'utf8' }, function(err, data) {
      if (err) {
        // This is okay with tasks-full, but it is not okay in tasks-lite: we
        // need async listeners to catch the error.
        throw err;
        // But this would work in the tasks-lite case:
        return complete(err);
      }
      
      // Do some weird stuff with the template
      var html = template.replace(/\$DEITY/gi, 'Burt');
      
      res.writeHead(200, { 'Content-Encoding': 'text/html' });
      res.end(html);
    });
    
    res.on('error', complete);
  
  }).setCallback(function(err) {
    // Wrap in another task because writeHead may fail of course.
    err && new Task(function() {
      res.writeHead(500, 'Internal server error', {});
      res.end();
    }).setCallback(function(){});
  });

  // Should this be needed?
  res.on('error', function() {
    // Ignore the error, no need to crash,
    // our tasks infrastructure can handle the error.
  });

}).listen(80);

Complicated case: shared emitter.

unfinished
var io = require('socket.io').listen(8000);

http.createServer(function(req, res) {
  new Task(function sendResponseTask(complete) {
    io.sockets.on('message', function(message) {
    
    });
  }).setCallback(function(err) {
    if (err) {
      // Send 500 response.
    });
  });
});