Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

modifid readme and examples

	modified:   README.md
	modified:   autoreload_example.js
	new file:   example_mymoduleloader.js
	modified:   index.js
  • Loading branch information...
commit 1e9265aa065d47ca38e34edd4c134c4c2f414700 1 parent 25abe28
Shimon Doodkin authored October 30, 2010
55  README.md 100755 → 100644
Source Rendered
... ...
@@ -1,15 +1,45 @@
1  
-#node-hot-reload example:
  1
+# node.js hot reload of js files. (it is not restart, more like erlang) 
2 2
 
3 3
 
  4
+## hotreload.path
  5
+
  6
+## hotreload.watch(script, reload_callback)
  7
+watch script file,  onchange load the script file and call callback
  8
+
  9
+script is string
  10
+
  11
+reload_callback(newmodule) is a function,
  12
+it has an argument - the loaded module object.
  13
+
  14
+## hotreload.watch(watchpath, script , reload_callback)
  15
+watch one file and load another.
  16
+
  17
+watchpath can be a string or an array
  18
+script is string 
  19
+
  20
+## hotreload.watchrel(script, reload_callback)
  21
+watch script file relative to @hotreload.path@
  22
+
  23
+## hotreload.watchrel(watchpath, script , reload_callback)
  24
+
  25
+
  26
+## example:
4 27
 server.js:
5 28
 
6 29
     var http = require('http');
7  
-    var autoreload= require('node-hot-reload');
8  
-    var mymodule= require('./mymodule_example');
  30
+    var hotreload= require('node-hot-reload');
  31
+        hotreload.path=__dirname;
  32
+    
  33
+    //1st require the module normaly
  34
+    
  35
+    var mymodule= require('./mymodule_example'); 
  36
+    
  37
+    
  38
+    //2nd watch it and reload it dinamicaly
9 39
     
10 40
     // explained example:
11 41
 
12  
-    autoreload.watchrel('mymodule_example.js', function (newmodule){
  42
+    hotreload.watchrel('mymodule_example.js', function (newmodule){
13 43
 
14 44
        /* you can put here staff to make your module
15 45
           look like it was initialized well. */
@@ -32,7 +62,7 @@ server.js:
32 62
        // copy properties from new module to old module
33 63
        // as a solution to not having to update
34 64
        // references to module object
35  
-       autoreload.copy(mymodule,newmodule);
  65
+       hotreload.copy(mymodule,newmodule);
36 66
 
37 67
        // option 3
38 68
        //   manually patch the old object with parts of the new object.
@@ -44,12 +74,15 @@ server.js:
44 74
 
45 75
     });
46 76
     
47  
-    // simple example:
48  
-    //
49  
-    //autoreload.watchrel('mymodule_example.js',
50  
-      function (newmodule){ mymodule=newmodule; } );
51  
-
52  
-    // idea: add loader function to automate loading of modules:
  77
+    // simple example1:
  78
+    //hotreload.watchrel('mymodule_example.js',
  79
+    //  function (newmodule){ mymodule=newmodule; } );
  80
+      
  81
+    // simple example2:
  82
+    //hotreload.watchrel('mymodule_example.js',
  83
+    //  function (newmodule){ hotreload.copy(mymodule,newmodule); } );
  84
+    
  85
+    // idea: add loader function to automate reloading of modules:
53 86
     //  
54 87
     
55 88
     mymodule.name="Shimon";
25  autoreload_example.js
... ...
@@ -1,11 +1,16 @@
1 1
 
2 2
 var http = require('http');
3  
-var autoreload= require('./index');
  3
+var hotreload= require('./index'); 
  4
+
  5
+//1st require the module normaly
  6
+    
4 7
 var mymodule= require('./mymodule_example');
5 8
 
  9
+//2nd watch it and reload it dinamicaly
  10
+
6 11
 // explained example:
7 12
 
8  
-autoreload.watchrel('mymodule_example.js', function (newmodule){
  13
+hotreload.watchrel('mymodule_example.js', function (newmodule){
9 14
 
10 15
    /* you can put here staff to make your module
11 16
       look like it was initialized well. */
@@ -28,7 +33,7 @@ autoreload.watchrel('mymodule_example.js', function (newmodule){
28 33
    // copy properties from new module to old module
29 34
    // as a solution to not having to update
30 35
    // references to module object
31  
-   autoreload.copy(mymodule,newmodule);
  36
+   hotreload.copy(mymodule,newmodule);
32 37
 
33 38
    // option 3
34 39
    //   manually patch the old object with parts of the new object.
@@ -40,12 +45,16 @@ autoreload.watchrel('mymodule_example.js', function (newmodule){
40 45
 
41 46
 });
42 47
 
43  
-// simple example:
44  
-//autoreload.watchrel('mymodule_example.js',
45  
-  function (newmodule){ mymodule=newmodule; } );
  48
+// simple example1:
  49
+//hotreload.watchrel('mymodule_example.js',
  50
+//  function (newmodule){ mymodule=newmodule; } );
  51
+  
  52
+// simple example2:
  53
+//hotreload.watchrel('mymodule_example.js',
  54
+//  function (newmodule){ hotreload.copy(mymodule,newmodule); } );
  55
+
  56
+// idea: add loader function to automate reloading of modules:
46 57
 
47  
-// idea: add loader function to automate loading of modules:
48  
-//  
49 58
 
50 59
 mymodule.name="Shimon";
51 60
 
134  example_mymoduleloader.js
... ...
@@ -0,0 +1,134 @@
  1
+ require.paths.unshift(__dirname); //make local paths searched by require
  2
+ 
  3
+ var app={};
  4
+ app.hotreload=require('node-hot-reload');
  5
+ 
  6
+ app.loadmodules= function(arr) {
  7
+ 
  8
+  for(var objectname in modules)  {
  9
+   filename=modules[objectname];
  10
+   app[objectname] = require(filename); // simple
  11
+   
  12
+   //app[objectname] = require(filename).main.call(app,app);
  13
+   // i use to use main function as kind of include mechanizm
  14
+   // to inject dependenies -- change the this of the callled main to app
  15
+   // and make the app var avalible in the module
  16
+   
  17
+	 app.watchmodule(objectname, filename + '.js'); 
  18
+  }
  19
+ };
  20
+ 
  21
+ app.watchmodule= function(objectname, filename) {
  22
+  var watch_arr=[];
  23
+  watch_arr.push(filename);
  24
+  app.hotreload.watchrel(watch_arr,filename,function (newmodule){
  25
+   hotreload.copy(app[objectname],newmodule);
  26
+  });
  27
+ };
  28
+ 
  29
+ // use
  30
+ var modules_to_load=
  31
+ {
  32
+  mymodule:'mymodule_example',
  33
+  //contacts:'subfolder/contacts'
  34
+ }
  35
+ 
  36
+app.loadmodules(modules_to_load);
  37
+ 
  38
+app.mymodule.name="Shimon";
  39
+
  40
+app.handlerequest=function (request, response)
  41
+{
  42
+   try
  43
+   {
  44
+    response.writeHead(200, {'Content-Type': 'text/plain'});
  45
+    response.end(app.mymodule.time() + '\n');
  46
+   }
  47
+   catch(err)
  48
+   {
  49
+    response.end(err.stack);
  50
+    console.log(err.stack);
  51
+   }
  52
+}
  53
+
  54
+app.run=function()
  55
+{
  56
+ http.createServer(function (request, response) {
  57
+  app.handlerequest(request, response)
  58
+ }).listen(8124);
  59
+}
  60
+
  61
+app.run();
  62
+
  63
+/*
  64
+//ADVANCED USE EXAMPLE:
  65
+
  66
+app.pages={};
  67
+    
  68
+// How to watch several files. for example a module and its templates.
  69
+// and have a custom standart reload function for modules of Page type.  
  70
+
  71
+app.watchpage = function(pagename,filename)
  72
+{
  73
+ var watch_arr=[];
  74
+ for(k in app.pages[pagename].load_templates)
  75
+ {
  76
+  if(app.pages[pagename].load_templates.hasOwnProperty (k))
  77
+  {
  78
+   watch_arr.push('templates/'+app.pages[pagename].load_templates[k]);
  79
+  }
  80
+ }
  81
+ watch_arr.push(filename);
  82
+ app.hotreload.watchrel(watch_arr,filename, 
  83
+ function (newmodule) {
  84
+  var oldpage=app.pages[pagename];
  85
+  var page=newmodule.main.apply(oldpage.pagethis?oldpage.pagethis:app,oldpage.pagearguments?oldpage.pagearguments:[app]);
  86
+  // in the main function in the module i use to store the arguments passed to the main function.
  87
+  
  88
+  app.doubletemplate.load_templates(page,function ()
  89
+  {
  90
+   app.pages[pagename]=page; // update main page reference
  91
+   
  92
+   for(var i=0;i<app.url_routes.length;i++) // serch routes and update routes
  93
+   {
  94
+    if(app.url_routes[i].page)
  95
+    if(app.url_routes[i].page.pagefilename==oldpage.pagefilename)
  96
+    {
  97
+     app.url_routes[i].page=page;
  98
+     console.log( (new Date).toTimeString() + ' page ' + i + ' reloaded ' + filename );
  99
+    }
  100
+   }
  101
+  }); 
  102
+  // route update here
  103
+ }); 
  104
+}
  105
+
  106
+//contacts.js
  107
+this.main=function(app)
  108
+{
  109
+ var page={
  110
+      pagefilename:__filename,
  111
+      load_templates:{
  112
+       contacts:"contacts.html"
  113
+      }
  114
+     };
  115
+     
  116
+     page.onload= function (page,template_name,callback) {
  117
+      var shared={'page':page, 'app':app, 'req':{}, } ;
  118
+      callback(shared);
  119
+     };
  120
+     
  121
+     page.main= function (shared,route_i) {
  122
+      var req=shared.req,res=shared.res,page=shared.page;
  123
+      var shared={'page':page, 'app':app, 'req':req, }         
  124
+      page.contacts( shared, function (echo) {
  125
+       res.writeHead(200, { 'Content-Type': 'text/html'});
  126
+       res.end(echo);
  127
+      });      
  128
+      return true; // accept the route
  129
+     }// main
  130
+     
  131
+ return page; 
  132
+} ;
  133
+
  134
+  */
6  index.js
@@ -43,11 +43,13 @@ var path = require('path');
43 43
 var fs = require('fs');
44 44
 var loadmoduletimer={};
45 45
 var trackedfiles={}; exports.trackedfiles=trackedfiles;
46  
-this.path=__dirname;
  46
+this.path=__dirname+'/../'; // wild guess parent folder of this module is a modules directory
  47
+//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.
  48
+
47 49
 // note: there is an upcomming version of node 
48 50
 // with auto reload modules probably it will be integrated in the near future.
49 51
 //
50  
-// also every time you reload a module it does not free the memory of the reviews module.
  52
+// also every time you reload a module it does not free the memory of the previews module.
51 53
 // it means that reloading modules sutes fine for development,
52 54
 // but do not relay on havy use of it for production.
53 55
 

0 notes on commit 1e9265a

Please sign in to comment.
Something went wrong with that request. Please try again.