itslukej / async-namespace

async namespace made with async_hooks

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

async-namespace

import Namespace from 'async-namespace';

const ctx = new Namespace('name');
// or
const ctx = Namespace.for('name');

simple example:

const log = (...args) => console.log(ctx.get('userid'), args);

server.use(() => {
  // some auth logic..
  ctx.set('user', new User({...}));
  ctx.set('userid', 123456789123456789);

  log('auth finished'); // => 123456789123456789 ['auth finished']
});

server.get('/cpu/work', event => {
  log('very big cpu work!! :)'); // => 123456789123456789 ['very big cpu work!! :)']
});
const ctx = new Namespace(':)');

// safe methods
ctx.get(key); // Map.prototype.get
ctx.set(key, value); // Map.prototype.set (returns value)

ctx.keys(); // Map.prototype.keys
ctx.clear(); // Map.prototype.clear
ctx.values(); // Map.prototype.values
ctx.entries(); // Map.prototype.entries
ctx.delete(key); // Map.prototype.delete

// unsafe underlaying map access
ctx.map // => Map | undefined


ctx.destroy() // destroys the async hook and all maps

About

async namespace made with async_hooks

License:MIT License


Languages

Language:JavaScript 100.0%