@atomist/automation-client
Version:
Atomist API for software low-level client
88 lines • 2.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const asyncHooks = require("async_hooks");
const namespaces = {};
/**
* Create a new namespace
*/
function create() {
return namespace;
}
exports.create = create;
/**
* Set AutomationContext into the namespace of the current execution
* @param context
*/
function set(context) {
namespace.set("context", context);
}
exports.set = set;
/**
* Get AutomationContext from the namespace of the current execution
*/
function get() {
return namespace.get("context");
}
exports.get = get;
/**
* Internal mapping from async execution ids to AutomationContext instances
*/
class Namespace {
constructor(context = {}) {
this.context = context;
}
run(fn) {
const id = asyncHooks.executionAsyncId();
this.context[id] = {};
fn();
}
set(key, val) {
const id = asyncHooks.executionAsyncId();
if (this.context[id]) {
this.context[id][key] = val;
}
}
get(key) {
const id = asyncHooks.executionAsyncId();
if (this.context[id]) {
return this.context[id][key];
}
else {
return undefined;
}
}
}
/**
* Registers the internal async hooks on the namespace
* @param nsp
*/
function createHooks(nsp) {
function init(asyncId, type, triggerId, resource) {
if (nsp.context[triggerId]) {
nsp.context[asyncId] = nsp.context[triggerId];
}
}
function destroy(asyncId) {
delete nsp.context[asyncId];
}
const asyncHook = asyncHooks.createHook({ init, destroy });
asyncHook.enable();
}
/**
* Create a new Namespace instance of the given name
* @param name
*/
function createNamespace(name) {
if (namespaces[name]) {
throw new Error(`Namespace '${name}' already exists`);
}
const nsp = new Namespace();
namespaces[name] = nsp;
createHooks(nsp);
return nsp;
}
/**
* Create the default namespace used by command and event handler executions
*/
const namespace = createNamespace("automation-client");
//# sourceMappingURL=cls.js.map