Skip to content

Commit

Permalink
modifid readme and examples
Browse files Browse the repository at this point in the history
	modified:   README.md
	modified:   autoreload_example.js
	new file:   example_mymoduleloader.js
	modified:   index.js
  • Loading branch information
shimondoodkin committed Oct 29, 2010
1 parent 25abe28 commit 1e9265a
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 21 deletions.
55 changes: 44 additions & 11 deletions README.md
100755 → 100644
Original file line number Original file line Diff line number Diff line change
@@ -1,15 +1,45 @@
#node-hot-reload example: # node.js hot reload of js files. (it is not restart, more like erlang)




## hotreload.path

## hotreload.watch(script, reload_callback)
watch script file, onchange load the script file and call callback

script is string

reload_callback(newmodule) is a function,
it has an argument - the loaded module object.

## hotreload.watch(watchpath, script , reload_callback)
watch one file and load another.

watchpath can be a string or an array
script is string

## hotreload.watchrel(script, reload_callback)
watch script file relative to @hotreload.path@

## hotreload.watchrel(watchpath, script , reload_callback)


## example:
server.js: server.js:


var http = require('http'); var http = require('http');
var autoreload= require('node-hot-reload'); var hotreload= require('node-hot-reload');
var mymodule= require('./mymodule_example'); hotreload.path=__dirname;

//1st require the module normaly

var mymodule= require('./mymodule_example');


//2nd watch it and reload it dinamicaly


// explained example: // explained example:


autoreload.watchrel('mymodule_example.js', function (newmodule){ hotreload.watchrel('mymodule_example.js', function (newmodule){


/* you can put here staff to make your module /* you can put here staff to make your module
look like it was initialized well. */ look like it was initialized well. */
Expand All @@ -32,7 +62,7 @@ server.js:
// copy properties from new module to old module // copy properties from new module to old module
// as a solution to not having to update // as a solution to not having to update
// references to module object // references to module object
autoreload.copy(mymodule,newmodule); hotreload.copy(mymodule,newmodule);


// option 3 // option 3
// manually patch the old object with parts of the new object. // manually patch the old object with parts of the new object.
Expand All @@ -44,12 +74,15 @@ server.js:


}); });


// simple example: // simple example1:
// //hotreload.watchrel('mymodule_example.js',
//autoreload.watchrel('mymodule_example.js', // function (newmodule){ mymodule=newmodule; } );
function (newmodule){ mymodule=newmodule; } );

// simple example2:
// idea: add loader function to automate loading of modules: //hotreload.watchrel('mymodule_example.js',
// function (newmodule){ hotreload.copy(mymodule,newmodule); } );

// idea: add loader function to automate reloading of modules:
// //


mymodule.name="Shimon"; mymodule.name="Shimon";
Expand Down
25 changes: 17 additions & 8 deletions autoreload_example.js
Original file line number Original file line Diff line number Diff line change
@@ -1,11 +1,16 @@


var http = require('http'); var http = require('http');
var autoreload= require('./index'); var hotreload= require('./index');

//1st require the module normaly

var mymodule= require('./mymodule_example'); var mymodule= require('./mymodule_example');


//2nd watch it and reload it dinamicaly

// explained example: // explained example:


autoreload.watchrel('mymodule_example.js', function (newmodule){ hotreload.watchrel('mymodule_example.js', function (newmodule){


/* you can put here staff to make your module /* you can put here staff to make your module
look like it was initialized well. */ look like it was initialized well. */
Expand All @@ -28,7 +33,7 @@ autoreload.watchrel('mymodule_example.js', function (newmodule){
// copy properties from new module to old module // copy properties from new module to old module
// as a solution to not having to update // as a solution to not having to update
// references to module object // references to module object
autoreload.copy(mymodule,newmodule); hotreload.copy(mymodule,newmodule);


// option 3 // option 3
// manually patch the old object with parts of the new object. // manually patch the old object with parts of the new object.
Expand All @@ -40,12 +45,16 @@ autoreload.watchrel('mymodule_example.js', function (newmodule){


}); });


// simple example: // simple example1:
//autoreload.watchrel('mymodule_example.js', //hotreload.watchrel('mymodule_example.js',
function (newmodule){ mymodule=newmodule; } ); // function (newmodule){ mymodule=newmodule; } );

// simple example2:
//hotreload.watchrel('mymodule_example.js',
// function (newmodule){ hotreload.copy(mymodule,newmodule); } );

// idea: add loader function to automate reloading of modules:


// idea: add loader function to automate loading of modules:
//


mymodule.name="Shimon"; mymodule.name="Shimon";


Expand Down
134 changes: 134 additions & 0 deletions example_mymoduleloader.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,134 @@
require.paths.unshift(__dirname); //make local paths searched by require

var app={};
app.hotreload=require('node-hot-reload');

app.loadmodules= function(arr) {

for(var objectname in modules) {
filename=modules[objectname];
app[objectname] = require(filename); // simple

//app[objectname] = require(filename).main.call(app,app);
// i use to use main function as kind of include mechanizm
// to inject dependenies -- change the this of the callled main to app
// and make the app var avalible in the module

app.watchmodule(objectname, filename + '.js');
}
};

app.watchmodule= function(objectname, filename) {
var watch_arr=[];
watch_arr.push(filename);
app.hotreload.watchrel(watch_arr,filename,function (newmodule){
hotreload.copy(app[objectname],newmodule);
});
};

// use
var modules_to_load=
{
mymodule:'mymodule_example',
//contacts:'subfolder/contacts'
}

app.loadmodules(modules_to_load);

app.mymodule.name="Shimon";

app.handlerequest=function (request, response)
{
try
{
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end(app.mymodule.time() + '\n');
}
catch(err)
{
response.end(err.stack);
console.log(err.stack);
}
}

app.run=function()
{
http.createServer(function (request, response) {
app.handlerequest(request, response)
}).listen(8124);
}

app.run();

/*
//ADVANCED USE EXAMPLE:
app.pages={};
// How to watch several files. for example a module and its templates.
// and have a custom standart reload function for modules of Page type.
app.watchpage = function(pagename,filename)
{
var watch_arr=[];
for(k in app.pages[pagename].load_templates)
{
if(app.pages[pagename].load_templates.hasOwnProperty (k))
{
watch_arr.push('templates/'+app.pages[pagename].load_templates[k]);
}
}
watch_arr.push(filename);
app.hotreload.watchrel(watch_arr,filename,
function (newmodule) {
var oldpage=app.pages[pagename];
var page=newmodule.main.apply(oldpage.pagethis?oldpage.pagethis:app,oldpage.pagearguments?oldpage.pagearguments:[app]);
// in the main function in the module i use to store the arguments passed to the main function.
app.doubletemplate.load_templates(page,function ()
{
app.pages[pagename]=page; // update main page reference
for(var i=0;i<app.url_routes.length;i++) // serch routes and update routes
{
if(app.url_routes[i].page)
if(app.url_routes[i].page.pagefilename==oldpage.pagefilename)
{
app.url_routes[i].page=page;
console.log( (new Date).toTimeString() + ' page ' + i + ' reloaded ' + filename );
}
}
});
// route update here
});
}
//contacts.js
this.main=function(app)
{
var page={
pagefilename:__filename,
load_templates:{
contacts:"contacts.html"
}
};
page.onload= function (page,template_name,callback) {
var shared={'page':page, 'app':app, 'req':{}, } ;
callback(shared);
};
page.main= function (shared,route_i) {
var req=shared.req,res=shared.res,page=shared.page;
var shared={'page':page, 'app':app, 'req':req, }
page.contacts( shared, function (echo) {
res.writeHead(200, { 'Content-Type': 'text/html'});
res.end(echo);
});
return true; // accept the route
}// main
return page;
} ;
*/
6 changes: 4 additions & 2 deletions index.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ var path = require('path');
var fs = require('fs'); var fs = require('fs');
var loadmoduletimer={}; var loadmoduletimer={};
var trackedfiles={}; exports.trackedfiles=trackedfiles; var trackedfiles={}; exports.trackedfiles=trackedfiles;
this.path=__dirname; this.path=__dirname+'/../'; // wild guess parent folder of this module is a modules directory
//findModulePath is here but not in use. Feel free to implement it, also you might want to get the latest copy of findModulePath from node.js source code this module is based on old node.js source code of Module.js.

// note: there is an upcomming version of node // note: there is an upcomming version of node
// with auto reload modules probably it will be integrated in the near future. // 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. // also every time you reload a module it does not free the memory of the previews module.
// it means that reloading modules sutes fine for development, // it means that reloading modules sutes fine for development,
// but do not relay on havy use of it for production. // but do not relay on havy use of it for production.


Expand Down

0 comments on commit 1e9265a

Please sign in to comment.