Permalink
Browse files

update example

	modified:   README
	modified:   autoreload_example.js
	modified:   index.js
	modified:   mymodule_example.js
  • Loading branch information...
1 parent 5aed59e commit ec88364134cf164f0e9a53f0071fddc8264f359b @shimondoodkin committed Oct 29, 2010
Showing with 334 additions and 9 deletions.
  1. +89 −0 README
  2. +40 −8 autoreload_example.js
  3. +205 −1 index.js
  4. 0 mymodule_example.js
View
89 README 100644 → 100755
@@ -0,0 +1,89 @@
+#node-hot-reload example:
+
+
+server.js:
+
+ var http = require('http');
+ var autoreload= require('node-hot-reload');
+ var mymodule= require('./mymodule_example');
+
+ // explained 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; // take care to copy previous data
+
+ //mymodule.init(); // init the module before if possible,
+ // it will save error time.
+ // option 1
+ // replace reference - probably the best way
+ // but you must update all references you have
+ // replace reference - probably the best way
+ // but you must update all references you have
+
+ //mymodule=newmodule;
+ //myothermodulereference=newmodule;
+
+
+ // option 2
+ // copy properties from new module to old module
+ // as a solution to not having to update
+ // references to module object
+ autoreload.copy(mymodule,newmodule);
+
+ // option 3
+ // manually patch the old object with parts of the new object.
+
+ // initialization or more initialization after switching to the new module
+ // mymodule.init();
+ // better don't do it, you may get errors,
+ // because of not well-initialized module.
+
+ });
+
+ // simple example:
+ //
+ //autoreload.watchrel('mymodule_example.js',
+ function (newmodule){ mymodule=newmodule; } );
+
+ // idea: add loader function to automate loading of modules:
+ //
+
+ mymodule.name="Shimon";
+
+ http.createServer(function (request, response) {
+
+ try
+ {
+ response.writeHead(200, {'Content-Type': 'text/plain'});
+ response.end(mymodule.time() + '\n');
+ }
+ catch(err)
+ {
+ response.end(err.stack);
+ console.log(err.stack);
+ }
+
+ //to do, main module reloadable instead of above you might use:
+ //mymodule.handlerequest(req,res);
+
+ }).listen(8124);
+
+ console.log('Server running at http://127.0.0.1:8124/');
+
+
+mymodule_example.js:
+
+ console.log('mymodule loaded');
+ exports.name='test';
+ exports.date=(new Date).toString();
+ exports.time=function ()
+ {
+ // edit the number to see it working
+ return " test number 17 name: "+exports.name+" , loaded at: "+exports.date.toString();
+ }
+ exports.filename=__filename;
+
View
48 autoreload_example.js 100644 → 100755
@@ -1,19 +1,51 @@
+
var http = require('http');
-var autoreload= require('./index');
+var autoreload= require('node-hot-reload');
var mymodule= require('./mymodule_example');
+// explained 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.
+
+ /* you can put here staff to make your module
+ look like it was initialized well. */
+
+ newmodule.name=mymodule.name; // take care to copy previous data
+
+ //mymodule.init(); // init the module before if possible,
+ // it will save error time.
+ // option 1
+ // replace reference - probably the best way
+ // but you must update all references you have
+ // replace reference - probably the best way
+ // but you must update all references you have
+
+ //mymodule=newmodule;
+ //myothermodulereference=newmodule;
+
+
+ // option 2
+ // copy properties from new module to old module
+ // as a solution to not having to update
+ // references to module object
autoreload.copy(mymodule,newmodule);
- //mymodule=newmodule; // replace reference - probabaly the best but you must update all references you have
- //mymodule.moreinit(); // while this not finished you may get errors, because of not whell initilized your module.
+
+ // option 3
+ // manually patch the old object with parts of the new object.
+
+ // initialization or more initialization after switching to the new module
+ // mymodule.init();
+ // better don't do it, you may get errors,
+ // because of not well-initialized module.
+
});
-//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
+// simple example:
+//autoreload.watchrel('mymodule_example.js',
+ function (newmodule){ mymodule=newmodule; } );
+// idea: add loader function to automate loading of modules:
+//
mymodule.name="Shimon";
View
206 index.js 100644 → 100755
@@ -4,6 +4,41 @@
// license: 2 close BSD.
//
+/*
+short example:
+
+var hotreload= require('deps/node-hot-reload');hotreload.path=__dirname;
+hotreload.watchrel('mymodule_example.js', function (newmodule){
+hotreload.copy(mymodule,newmodule);
+});
+
+
+described example:
+var hotreload= require('deps/node-hot-reload');hotreload.path=__dirname;
+hotreload.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.
+
+ hotreload.copy(mymodule,newmodule);
+
+ //mymodule=newmodule; // replace a reference - probabaly the best but you must update all references you have
+
+
+ //mymodule.do_more_init_in_my_module();
+ // it is posible to do init after replacing references
+ // but probably it is a bad idea
+ // because while your module is not initilized yet you may get errors.
+ //
+
+});
+
+*/
+
+
var path = require('path');
var fs = require('fs');
var loadmoduletimer={};
@@ -16,6 +51,174 @@ this.path=__dirname;
// it means that reloading modules sutes fine for development,
// but do not relay on havy use of it for production.
+/* Sync unless callback given */
+function findModulePath (id, dirs, callback)
+{
+ process.assert(dirs.constructor == Array);
+
+ if (/^https?:\/\//.exec(id))
+ {
+ if (callback) {
+ callback(id);
+ } else {
+ throw new Error("Sync http require not allowed.");
+ }
+ return;
+ }
+
+ if (/\.(js|node)$/.exec(id)) {
+ id=id.substring(0,id.length-3);
+ //throw new Error("No longer accepting filename extension in module names");
+ }
+
+ if (dirs.length == 0) {
+ if (callback) {
+ callback();
+ } else {
+ return; // sync returns null
+ }
+ }
+
+ var dir = dirs[0];
+ var rest = dirs.slice(1, dirs.length);
+
+ if (id.charAt(0) == '/') {
+ dir = '';
+ rest = [];
+ }
+
+ var locations = [
+ path.join(dir, id + ".js"),
+ path.join(dir, id + ".node"),
+ path.join(dir, id, "index.js"),
+ path.join(dir, id, "index.node")
+ ];
+
+ var ext;
+ var extensions = Object.keys(extensionCache);
+ for (var i = 0, l = extensions.length; i < l; i++) {
+ var ext = extensions[i];
+ locations.push(path.join(dir, id + ext));
+ locations.push(path.join(dir, id, 'index' + ext));
+ }
+
+ function searchLocations () {
+ var location = locations.shift();
+ if (!location) {
+ return findModulePath(id, rest, callback);
+ }
+
+ // if async
+ if (callback) {
+ path.exists(location, function (found) {
+ if (found) {
+ callback(location);
+ } else {
+ return searchLocations();
+ }
+ });
+
+ // if sync
+ } else {
+ if (existsSync(location)) {
+ return location;
+ } else {
+ return searchLocations();
+ }
+ }
+ }
+ return searchLocations();
+}
+/*
+var paths=require.paths;
+
+function findmodulefile(request,callback)
+{
+ if (!callback) {
+ // sync
+ var filename = findModulePath(request, paths);
+ if (!filename) {
+ throw new Error("Cannot find module '" + request + "'");
+ } else {
+ return filename;
+ }
+
+ } else {
+ // async
+ findModulePath(request, paths, function (filename) {
+ if (!filename) {
+ var err = new Error("Cannot find module '" + request + "'");
+ callback(err);
+ } else {
+ callback(filename);
+ }
+ });
+ }
+}
+
+var hotrequire;
+hotrequire=function ( rquiredfilename )
+{
+ console.log((new Date).toString()+' hot loading file: '+filename);
+ var content=fs.readFileSync(fd,'utf8');
+ var parseerror=false,errorincallback=false;
+ try
+ {
+
+ var newmodule={};
+ var sandbox = {};
+ for (var k in global)
+ {
+ sandbox[k] = global[k];
+ }
+
+ var filename = findmodulefile( rquiredfilename );
+ var dirname = path.dirname(filename);
+
+ sandbox.require = require;
+ sandbox.hotrequire = hotrequire;
+ sandbox.exports = newmodule;
+ sandbox.__filename = filename;
+ sandbox.__dirname = dirname;
+ sandbox.module = newmodule;
+ sandbox.root = global;
+
+ // create wrapper function
+ var wrapper = "this.compiledWrapper = (function (exports, require, module, __filename, __dirname) { "
+ + content
+ + "\n});";
+ parseerror=true;
+ process.binding('evals').Script.runInNewContext(wrapper, sandbox, filename);
+ parseerror=false;
+ sandbox.compiledWrapper.apply(newmodule, [newmodule, require, newmodule, filename, dirname]);
+ errorincallback=true;
+ return newmodule;
+ errorincallback=false;
+ }
+ catch(err)
+ {
+ if (err)
+ {
+ if(parseerror)
+ {
+ console.log("Error Parsing "+filename+" \r\n (restarting the application may give you a more meaningful error message.)");
+ console.log(" -- start tryload message -- ");
+ tryload(filename,function(errortext){
+ console.log(errortext);
+ console.log(" -- end tryload message -- ");
+ });
+ }
+ else
+ {
+ if(errorincallback)console.log("Error in Callback hot-reloading: "+filename);
+ console.log(err.stack);
+ }
+ }
+ //if (err) throw err; // need to add better error handling
+ }
+} exports.hotrequire=hotrequire;
+
+*/
function loadlater( filename , callback )
{
//console.log((new Date).toString()+' will load file: '+filename);
@@ -40,6 +243,7 @@ function loadlater( filename , callback )
}
sandbox.require = require;
+ //sandbox.hotrequire = hotrequire;
sandbox.exports = newmodule;
sandbox.__filename = filename;
sandbox.__dirname = dirname;
@@ -101,7 +305,7 @@ function watch()
}
if(typeof watchfilename=='string') watchfilename=[watchfilename];
- console.log((new Date).toString()+' watch reaload file: '+filename);
+ //console.log((new Date).toString()+' watch reaload file: '+filename);
trackedfiles[filename]=true;
var functionload=function(curr,prev)
{
View
0 mymodule_example.js 100644 → 100755
No changes.

0 comments on commit ec88364

Please sign in to comment.