<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array">
    <added>
      <filename>public/ajax.js</filename>
    </added>
  </added>
  <modified type="array">
    <modified>
      <diff>@@ -1,5 +1,5 @@
 Copyright (c) 2009 Jim Benton
-microAjax Copyright (c) 2008 Stefan Lange-Hegermann
+ajax.js is based on microAjax Copyright (c) 2008 Stefan Lange-Hegermann
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the &quot;Software&quot;), to deal</diff>
      <filename>MIT_LICENSE</filename>
    </modified>
    <modified>
      <diff>@@ -39,19 +39,28 @@ exports.diligence.Server = function(setupCallback) {
   function start() {
     var server = new node.http.Server(function (req, res) {
 
-      // puts(req.uri.path);
-
-      if (req.uri.path == '/result') {
-        return result(req, res);
-      } else if (req.uri.path == '/tick') {
-        return code(req, res);
-      } else if (match = req.uri.path.match(/^\/static\/(.*)/)){
-        return sendStaticFile(match[1], res);
-      } else {
-        return sendStaticFile('runner.html', res);
-      }
+        puts(req.uri.path);
+
+        if (req.uri.path == '/result') {
+          req.setBodyEncoding('utf8');
+          var body = '';
+          req.onBody = function (chunk) {
+            body += chunk;
+          };
+          req.onBodyComplete = function() {
+            return result(body, req, res);
+          };
+        } else if (req.uri.path == '/tick') {
+          return tick(req, res);
+        } else if (match = req.uri.path.match(/^\/file/)){
+          return sendFile(req.uri.params.path, res);
+        } else if (match = req.uri.path.match(/^\/static\/(.*)/)){
+          return sendStaticFile(match[1], res);
+        } else {
+          return boot(req, res);
+        }
 
-    }).listen(config['port']);
+      }).listen(config['port']);
 
     if (server) {
       puts(&quot;diligence is running on port &quot; + config['port'].toString() + &quot;.&quot;);
@@ -116,74 +125,71 @@ exports.diligence.Server = function(setupCallback) {
     res.finish();
   }
 
-  function sendFile(data, contentType, res) {
+  function sendData(data, contentType, res) {
     res.sendHeader(200, [[&quot;Content-Type&quot;, contentType]]);
     res.sendBody(data);
     res.finish();
   }
 
   function sendStaticFile(filename, res) {
-    puts(&quot;serving static file '&quot; + publicPath(filename) + &quot;'&quot;);
-    var extension = filename.match(/[a-z0-9]*\.(js|html)/)[1];
+    sendFile(publicPath(filename), res);
+  }
+
+  function sendFile(path, res) {
+    puts(&quot;serving file '&quot; + path + &quot;'&quot;);
+    var extension = path.match(/.*(js|html)$/)[1];
     var contentType = extension == 'js' ? 'text/javascript' : 'text/html';
-    loadUtfFile(publicPath(filename), function(data) {
-      sendFile(data, contentType, res);
+    loadUtfFile(path, function(data) {
+      sendData(data, contentType, res);
     });
   }
 
   // actions
 
-  function result(req, res) {
-    config.process(req, JSON.parse(req.uri.params['payload']));
+  function result(body, req, res) {
+    config.process(req, JSON.parse(body));
     sendNothing(res);
   }
 
-  function code(req, res) {
+  function boot(req, res) {
+    
+    var browser = getBrowserState(req);
+    browser.lastSeenAt = new Date().getTime();
+    
+    var html = loadUtfFile(publicPath('runner.html'), function(data) {
+      var scripts = '';
+      var paths = expandPaths(config.testPaths);
+      for (var i=0,l=paths.length; i&lt;l; i++) {
+        scripts += '&lt;script type=&quot;text/javascript&quot; src=&quot;/files?path=' + encodeURIComponent(paths[i]) + '&quot;&gt;&lt;/script&gt;' + &quot;\n&quot;
+      }
+      var page = data.replace('{{ scripts }}', scripts);
+      sendData(page, 'text/html', res);
+    });
+  }
+
+  function tick(req, res) {
 
     var paths = expandPaths(config.testPaths);
-    var i = 0;
     var fileContent = '';
     var browser = getBrowserState(req);
+    var now = new Date().getTime();
     
     function checkModTime(index) {
-      node.fs.stat(paths[i], function(status, stats) {        
+      node.fs.stat(paths[index], function(status, stats) {
         if (typeof(browser.lastSeenAt) == 'undefined' || browser.lastSeenAt &lt; stats['mtime'].getTime()) {
-          browser.lastSeenAt = new Date().getTime();
-          debug(&quot;sending new code&quot;);
-          i = 0; // reset loop so code loading starts at the top
-          loadFileAt(index);
+          browser.lastSeenAt = now;
+          sendData(JSON.stringify({reload: true}), 'text/javascript', res);
         } else {
           var nextIndex = index + 1;
           if (nextIndex &lt; paths.length) {
             checkModTime(nextIndex);
           } else {
-            sendNothing(res);
+            sendData(JSON.stringify({reload: false}), 'text/javascript', res);
           }
         }
       });
     }
-    
-    function loadFileAt(index) {
-      loadUtfFile(paths[index], function(data) {
-        fileContent += data;
-        var nextIndex = index + 1;
-        if (nextIndex &lt; paths.length) {
-          loadFileAt(nextIndex);
-        } else {
-          respond();
-        }
-      });
-    }
-    
-    function respond() {
-      loadUtfFile(config.collectPath, function(collectCode) {
-        var data = {test: fileContent, collect: collectCode};
-        sendFile(JSON.stringify(data), 'text/javascript', res);
-      });
-    }
-    
     checkModTime(0);
-
   }
   
 };
\ No newline at end of file</diff>
      <filename>diligence.js</filename>
    </modified>
    <modified>
      <diff>@@ -5,10 +5,11 @@
 &lt;head&gt;
 	&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot;/&gt;
 	&lt;title&gt;runner&lt;/title&gt;
-	&lt;script src=&quot;/static/microajax.js&quot; type=&quot;text/javascript&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;
+	&lt;script src=&quot;/static/ajax.js&quot; type=&quot;text/javascript&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;
 	&lt;script src=&quot;/static/json2.js&quot; type=&quot;text/javascript&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;
 	&lt;script src=&quot;/static/runner.js&quot; type=&quot;text/javascript&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;
+	{{ scripts }}
 &lt;/head&gt;
-&lt;body onload=&quot;run();&quot;&gt;
+&lt;body&gt;
 &lt;/body&gt;
 &lt;/html&gt;
\ No newline at end of file</diff>
      <filename>public/runner.html</filename>
    </modified>
    <modified>
      <diff>@@ -1,29 +1,32 @@
-var interval;
-function go() {
-  interval = setInterval(run, 1000);
-};
+var diligence = (function() {
 
-function run() {
-  microAjax('/tick', function(json){
-    if (json != '') {
-      clearInterval(interval);
-      var response = JSON.parse(json);
-      try {
-        eval(response.test);
-      } catch(exception) {
-        var exception = {
-          message: exception.message
-        }
-      } finally {
-        var data = eval(response.collect);
-        if (exception) {
-          data.exception = exception;
+  var interval;
+
+  function wait() {
+    interval = setInterval(tick, 1000);
+  }
+
+  function tick() {  
+    new ajax('/tick', function(request){
+      var json = request.responseText;
+      if (json != '') {
+        response = JSON.parse(json);
+        if (response.reload) {
+          clearInterval(interval);
+          document.location.reload();
         }
-        var payload = encodeURIComponent(JSON.stringify(data));
-        microAjax('/result?payload=' + payload, function(data) {
-          go();
-        });
       }
-    }
-  });
-};
\ No newline at end of file
+    });
+  };
+
+  function respond(result) {
+    var payload = JSON.stringify(result);
+    new ajax('/result', function(request) {
+      wait();
+    }, payload);
+  }
+
+  return {
+    respond: respond
+  }
+})();
\ No newline at end of file</diff>
      <filename>public/runner.js</filename>
    </modified>
  </modified>
  <removed type="array">
    <removed>
      <filename>public/microajax.js</filename>
    </removed>
  </removed>
  <parents type="array">
    <parent>
      <id>99d27de2a2fe5d07a144b0ef5a63750a8b3929b3</id>
    </parent>
  </parents>
  <author>
    <name>Jim Benton</name>
    <email>jim@autonomousmachine.com</email>
  </author>
  <url>http://github.com/jim/diligence/commit/795da08ee308f24bfe4397924edd558576eee4cd</url>
  <id>795da08ee308f24bfe4397924edd558576eee4cd</id>
  <committed-date>2009-06-17T14:49:30-07:00</committed-date>
  <authored-date>2009-06-17T14:49:30-07:00</authored-date>
  <message>Changed to a simpler strategy, and rewrote most of the library as a result</message>
  <tree>189f6aa864ef216a99605e29c918a77082351d87</tree>
  <committer>
    <name>Jim Benton</name>
    <email>jim@autonomousmachine.com</email>
  </committer>
</commit>
