Coder Social home page Coder Social logo

drex's Introduction

drex

drex - "dynamic require() extension". Dynamic version of Node's require() - loads fresh copy of the module every time the module file is changed.

Insanely simple, and/but astonishingly useful grab of bits, which has been brought to this world out of severe necessity.

Here is why:

  • all re-loaders I was able to find reload the whole node process, which means that the context of the process is gone;
  • but, sometimes, you don't want that! You want your process to continue running intact, at least for those clients who already deep in it, and would choke if process forgets about them;
  • sometime you have a little (or BIG) piece of code which you constantly change and, for G-d sake, do not want to sacrifice the whole your Node process for, but
  • you don't want to loose the benefits of CommonJS/require supported modularity of your code.

Here comes drex, and it comes like this:

var drex = require('drex');

... node code node code node code ...

// here goes my frea[ky/quently updated] piece of code, which lives in a js file called mucode.js:
drex.require('./mucode.js', function(mucode)
{
  // at this point my mucode.js has been require()d, just like this: 
  // var mucode = require('./mucode.js');
  // the code of the required module is the LATEST UPDATE TO mucode.js
  mucode.muNewFunc();  
});

#Here is an example (and the reason I had to comeup with drex) from the real life when drex is irreplaceable:# frequently updated/added socket.io event handlers:

io.sockets.on('connection', function (socket) {
  // I need to do many things here, and these things change all the time!
  // If I use something like "forever", or "supervisor" to re-start my Node process every time 
  // when things here should change, all active sessions will be killed!
  // Oh, no, no, no!
  // All I want to do here, most of the time, is to put new event handler, which existing sessions 
  // do not even know about!
  
  drex.require('./my_module_with_event_handlers_which_I_always_change.js', function(mymod) {
      // here I can start calling methods from my module like there is no tomorrow!
      // and I'm guaranteed that every time I update my module, sessions which will come here after the update
      // will get the new code, but sessions which were opened before the update will still be working with the
      // code which existed in my module when these sessions were created. That's fair!
  });

#API# drex.require(<module reference>, <callBack>);

where:

  • <module reference> resolvable Node module reference, such as './module.js'

  • <callBack> function which is being called when the module has been require()d.

    callBack signature: callBack(module, filepath)

    where:

    • module: reference to the require()d module, and
    • filepath: full path to the module's file

#Installation using NPM# npm install drex #Even more simple installation# Just copy drex.js to the place where Node can find it, i.e. for require('./drex.js') - to the root of your Node project.

#tl;dr# drex is watching a module for updates and cleanly re-requires the module after the update. New code is being require()d as if the new code is a totally different module, so require.cache is not a problem.

#License# GPL

Enjoy

drex's People

Contributors

yuryb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

drex's Issues

Getting drex from npm

After installing drex from npm and doing require('drex'); errors with Error: Cannot find module 'drex', however, doing require('drex/drex'); seems to work =/

Can't get drex module to work

running app with: NODE_ENV=development node app.js
I get this:

module.js:340
throw err;
^
Error: Cannot find module 'drex'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object. (/Volumes/NAUSICAA/projects/arashin/lib/utils.js:13:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)

I did a npm install before in the app root directory, and the drex module seems to be installed in the node_modules dir.
I'm running Node 0.10.4 on a mac.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.