Skip to content

Instantly share code, notes, and snippets.

@taddeiweb
Created January 27, 2014 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save taddeiweb/793f79c81e624bcae057 to your computer and use it in GitHub Desktop.
Save taddeiweb/793f79c81e624bcae057 to your computer and use it in GitHub Desktop.
disappearing context
var http = require('http');
if (!process.addAsyncListener) require('async-listener');
// Setup the process's async listener
process.addAsyncListener(setup, {
before: onBefore
});
var realContext;
env = {
get context(){
return realContext;
},
set context(value){
throw new Error('please use env.createContext to create a new context')
}
};
var cnt = 0;
env.createContext = function(){
// INIT CONTEXT
realContext = {
'_ctxid': ++cnt
};
};
// Initialize the null context
// This function is called every time the stack is closing
function setup(){
return realContext;
}
// This function is called when the new stack starts
function onBefore(_ , context){
realContext = context;
}
var server = http.createServer(function(req,res){
req.on('data' , function(){
console.log(env.context);
});
req.on('end', function(){
console.log(env.context);
res.end('ok');
});
env.createContext();
});
server.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment