function create(name) {
assert.ok(name, "namespace must be given a name!");
var namespace = new Namespace(name);
namespace.id = process.addAsyncListener({
create : function () { return namespace.active; },
before : function (context, storage) { if (storage) namespace.enter(storage); },
after : function (context, storage) { if (storage) namespace.exit(storage); },
error : function (storage) { if (storage) namespace.exit(storage); }
});
process.namespaces[name] = namespace;
return namespace;
}...
setTimeout(function() {
// runs with the default context, because nested contexts have ended
console.log(writer.get('value')); // prints 0
}, 1000);
}
```
## cls.createNamespace(name)
* return: {Namespace}
Each application wanting to use continuation-local values should create its own
namespace. Reading from (or, more significantly, writing to) namespaces that
don't belong to you is a faux pas.
...function destroy(name) {
var namespace = get(name);
assert.ok(namespace, "can't delete nonexistent namespace!");
assert.ok(namespace.id, "don't assign to process.namespaces directly!");
process.removeAsyncListener(namespace.id);
process.namespaces[name] = null;
}...
## cls.getNamespace(name)
* return: {Namespace}
Look up an existing namespace.
## cls.destroyNamespace(name)
Dispose of an existing namespace. WARNING: be sure to dispose of any references
to destroyed namespaces in your old code, as contexts associated with them will
no longer be propagated.
## cls.reset()
...function get(name) {
return process.namespaces[name];
}...
* return: {Namespace}
Each application wanting to use continuation-local values should create its own
namespace. Reading from (or, more significantly, writing to) namespaces that
don't belong to you is a faux pas.
## cls.getNamespace(name)
* return: {Namespace}
Look up an existing namespace.
## cls.destroyNamespace(name)
...function reset() {
// must unregister async listeners
if (process.namespaces) {
Object.keys(process.namespaces).forEach(function (name) {
destroy(name);
});
}
process.namespaces = Object.create(null);
}...
* Clean up minor typo in docs.
### v2.1.0 (2013-09-03):
* Incorporate documentation from failed CLS PR.
* `namespace.bind()` now also always exits the domain, even on error.
* Namespaces can be destroyed.
* `cls.reset()` allows tests to nuke all existing namespaces (use with care
obviously).
### v2.0.0 (2013-09-01):
* Use `async-listener` polyfill instead of `cls-glue`.
* Incorporate tests from `cls-glue`.
...