Permalink
Browse files

modifid readme and examples

	modified:   README.md
	modified:   autoreload_example.js
	new file:   example_mymoduleloader.js
	modified:   index.js
  • Loading branch information...
1 parent 25abe28 commit 1e9265aa065d47ca38e34edd4c134c4c2f414700 @shimondoodkin committed Oct 29, 2010
Showing with 199 additions and 21 deletions.
  1. +44 −11 README.md
  2. +17 −8 autoreload_example.js
  3. +134 −0 example_mymoduleloader.js
  4. +4 −2 index.js
View
55 README.md 100755 → 100644
@@ -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:
var http = require('http');
- var autoreload= require('node-hot-reload');
- var mymodule= require('./mymodule_example');
+ var hotreload= require('node-hot-reload');
+ hotreload.path=__dirname;
+
+ //1st require the module normaly
+
+ var mymodule= require('./mymodule_example');
+
+
+ //2nd watch it and reload it dinamicaly
// 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
look like it was initialized well. */
@@ -32,7 +62,7 @@ server.js:
// copy properties from new module to old module
// as a solution to not having to update
// references to module object
- autoreload.copy(mymodule,newmodule);
+ hotreload.copy(mymodule,newmodule);
// option 3
// manually patch the old object with parts of the new object.
@@ -44,12 +74,15 @@ server.js:
});
- // simple example:
- //
- //autoreload.watchrel('mymodule_example.js',
- function (newmodule){ mymodule=newmodule; } );
-
- // idea: add loader function to automate loading of modules:
+ // simple example1:
+ //hotreload.watchrel('mymodule_example.js',
+ // 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:
//
mymodule.name="Shimon";
View
@@ -1,11 +1,16 @@
var http = require('http');
-var autoreload= require('./index');
+var hotreload= require('./index');
+
+//1st require the module normaly
+
var mymodule= require('./mymodule_example');
+//2nd watch it and reload it dinamicaly
+
// 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
look like it was initialized well. */
@@ -28,7 +33,7 @@ autoreload.watchrel('mymodule_example.js', function (newmodule){
// copy properties from new module to old module
// as a solution to not having to update
// references to module object
- autoreload.copy(mymodule,newmodule);
+ hotreload.copy(mymodule,newmodule);
// option 3
// manually patch the old object with parts of the new object.
@@ -40,12 +45,16 @@ autoreload.watchrel('mymodule_example.js', function (newmodule){
});
-// simple example:
-//autoreload.watchrel('mymodule_example.js',
- function (newmodule){ mymodule=newmodule; } );
+// simple example1:
+//hotreload.watchrel('mymodule_example.js',
+// 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";
View
@@ -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;
+} ;
+
+ */
View
@@ -43,11 +43,13 @@ var path = require('path');
var fs = require('fs');
var loadmoduletimer={};
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
// 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,
// but do not relay on havy use of it for production.

0 comments on commit 1e9265a

Please sign in to comment.