lose-cls-context
Version:
Function to lose CLS context
70 lines (44 loc) • 2.54 kB
Markdown
//img.shields.io/npm/v/lose-cls-context.svg)](https://www.npmjs.com/package/lose-cls-context)
[](http://travis-ci.org/overlookmotel/lose-cls-context)
[](https://david-dm.org/overlookmotel/lose-cls-context)
[](https://david-dm.org/overlookmotel/lose-cls-context)
[](https://coveralls.io/r/overlookmotel/lose-cls-context)
Runs a callback asynchronously, but *outside* of current CLS context.
`callback` is called in next tick. As opposed to `setImmediate`, `process.nextTick()` etc, the [continuation-local-storage](https://www.npmjs.com/package/continuation-local-storage) context is not maintained.
Primarily, this module exists to facilitate testing of node.js libraries / applications that use [continuation-local-storage](https://www.npmjs.com/package/continuation-local-storage).
This module is a lightweight way to test what happens when CLS context is lost (which many libraries e.g. `redis` do) and to ensure that your own code handles this correctly, perhaps by re-binding with `namespace.bind()`.
### Example
```js
var cls = require('continuation-local-storage');
var namespace = cls.createNamespace('test');
var loseCls = require('lose-cls-context');
namespace.run(function() {
namespace.set('value', 123);
setImmediate(function() {
var value = namespace.get('value');
// CLS context is maintained - value === 123
});
loseCls(function() {
var value = namespace.get('value');
// CLS context has been lost - value === undefined
});
});
```
Use `npm test` to run the tests. Use `npm run cover` to check coverage.
See [changelog.md](https://github.com/overlookmotel/lose-cls-context/blob/master/changelog.md)
If you discover a bug, please raise an issue on Github. https://github.com/overlookmotel/lose-cls-context/issues
Pull requests are very welcome. Please:
* ensure all tests pass before submitting PR
* add an entry to changelog
* add tests for new features
* document new functionality/API additions in README
[![NPM version](https: