Permalink
Browse files

modified: README.md

	modified:   autoexit.js
	modified:   autoreload.js
	modified:   autoreload_example.js
	modified:   both_example.js
	new file:   mymodule_example.js
  • Loading branch information...
1 parent 975890d commit a1480ff4d628e33b001fb5e376e17c32de82c0b7 @shimondoodkin committed Aug 10, 2010
Showing with 54 additions and 21 deletions.
  1. +1 −1 README.md
  2. +7 −5 autoexit.js
  3. +16 −3 autoreload.js
  4. +3 −3 autoreload_example.js
  5. +19 −9 both_example.js
  6. +8 −0 mymodule_example.js
View
@@ -16,7 +16,7 @@ for example to `/var/www`. Copying of `autoexit.js` is optional and it can be in
// so it runs again after it exits.
var autoexit_watch=require('autoexit').watch;
//
- var on_autoexit=function (filename) { } // if it returns true it means to ignore exit this time;
+ var on_autoexit=function (filename) { } // if it returns false it means to ignore exit this time;
autoexit_watch(__dirname,".js", on_autoexit);
//autoexit_watch(__dirname+"/templates",".html", on_autoexit);
View
@@ -21,19 +21,21 @@ this.restarted=false;
var that=this;
function watch(parse_file_list_dirname,extention,callback) {
- var restart_server = function(file)
+ var restart_server = function(filename)
{
if(that.restarted) return;
that.restarted=true;
var ignore=false;
- var callbackresult=false;;
- if(callback)callbackresult=callback();
- ignore=(!callbackresult);
+ var callbackresult=true;;
+ if(callback)callbackresult=callback(filename);
+ ignore=(callbackresult===false);
if(!ignore)
{
- sys.puts((new Date).toTimeString()+' change discovered, restarting server. the file was: '+file);
+ sys.puts((new Date).toTimeString()+' change discovered, restarting server. the file was: '+filename);
process.exit();
}
+ else
+ that.restarted=false;
}
var parse_file_list1 = function(dir, files, extention)
View
@@ -1,19 +1,28 @@
+//
+// node.js auto reload module
+// by Shimon Doodkin.
+// license: 2 close BSD.
+//
+
var path = require('path');
var fs = require('fs');
var loadmoduletimer={};
-var
+var trackedfiles={}; exports.trackedfiles=trackedfiles;
+
// note: there is an upcomming version of node
// with auto reload modules probably it will be integrated in the near future.
//
// also every time you reload a module it does not free the memory of the reviews module.
// it means that reloading modules sutes fine for development,
// but do not relay on havy use of it for production.
-function loadlater( filename , callback )
+function loadlater( filename , callback )
{
+ console.log((new Date).toString()+' will load file: '+filename);
+
return setTimeout(function ()
{
- console.log('will load file: '+filename);
+ console.log((new Date).toString()+' loading file: '+filename);
fs.readFile(filename, function (err, content)
{
if (err) throw err; // need to add better error handling
@@ -40,10 +49,14 @@ function loadlater( filename , callback )
function watch(filename,callback)
{
+ trackedfiles[filename]=true;
fs.watchFile(filename, function ()
{
if(loadmoduletimer[filename])
+ {
+ console.log((new Date).toString()+'timeout cleaned - will load file: '+filename);
clearTimeout(loadmoduletimer[filename]);
+ }
loadmoduletimer[filename] =
loadlater( filename ,callback);
});
View
@@ -1,9 +1,9 @@
var http = require('http');
var autoreload= require('./autoreload');
-var mymodule= require('./mymodule');
+var mymodule= require('./mymodule_example');
-autoreload.watchrel('mymodule.js', function (newmodule){ mymodule=newmodule; });
-//autoreload.watch(mymodule.filename, function (newmodule){ mymodule=newmodule; }); // might not work if when started the module has errors or filename export is missing
+autoreload.watchrel('mymodule_example.js', function (newmodule){ mymodule=newmodule; });
+//autoreload.watch(mymodule.filename, function (newmodule){ mymodule=newmodule; }); // might not work if when started the module has errors or filename exports is missing
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
View
@@ -3,25 +3,35 @@
require.paths.unshift(__dirname); //make local paths accessible
var autoreload= require('./autoreload');
-var mymodule= require('./mymodule');
-autoreload.watchrel('mymodule.js', function (newmodule){
+var mymodule= require('./mymodule_example');
+autoreload.watchrel('mymodule_example.js', function (newmodule){
+ /* you can put here staff to make your module look like it was initialized well. */
+ newmodule.name=mymodule.name;
+ //mymodule.init(); // init the module before if possible, it will save error time.
mymodule=newmodule;
- /* also you can put hre staff to make you module looklike it was initialized well. */
+ //mymodule.moreinit(); // while this not finished you may get errors, because of not whell initilized your module.
});
//autoreload.watch(mymodule.filename, function (newmodule){ mymodule=newmodule; }); // might not work if when started the module has errors or filename export is missing
var sys = require('sys'),
http = require('http');
+
+
+ mymodule.name="Shimon Doodkin";
+
http.createServer(function (req, res) {
try
{
- res.writeHead(200, {'Content-Type': 'text/plain'});
- response.end(mymodule.time() + '\n');
- res.end();
-
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ res.end(mymodule.time() + '\n');
+ res.end();
+
+ //to do, main module reloadable instead of above you might use:
+ //mymodule.handlerequest(req,res);
+
}
catch(e)
{
@@ -38,8 +48,8 @@ sys.puts((new Date).toTimeString()+' Server running at http://127.0.0.1:8124/');
var autoexit_watch=require('autoexit').watch;
-var on_autoexit=function () { // if this function return true it means to ignore the exit;
-
+var on_autoexit=function (filename) { // if this function return false it means to ignore the exit;
+ if(autoreload.trackedfiles[filename]) return false;
}
autoexit_watch(__dirname,".js", on_autoexit);
//autoexit_watch(__dirname+"/templates",".html", on_autoexit);
View
@@ -0,0 +1,8 @@
+console.log('mymodule loaded');
+exports.name='test';
+exports.time=function ()
+{
+ // edit the number to see it working
+ return " test number 12 name: "+exports.name+" , today: "+(new Date).toString();
+}
+exports.filename=__filename;

0 comments on commit a1480ff

Please sign in to comment.