@atomist/automation-client
Version:
Atomist API for software low-level client
52 lines • 2.19 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const GraphQL = require("./internal/graph/graphQL");
const metadataReading_1 = require("./internal/metadata/metadataReading");
const string_1 = require("./internal/util/string");
const scan_1 = require("./scan");
const constructionUtils_1 = require("./util/constructionUtils");
/**
* Create a HandleEvent instance with the appropriate metadata wrapping
* the given function
* @param {OnEvent<T, P>} h
* @param {Maker<P>} factory
* @param {string} subscription
* @param {string} name
* @param {string} description
* @param {string | string[]} tags
* @returns {HandleEvent<T, P> & EventHandlerMetadata}
*/
function eventHandlerFrom(h, factory, subscription, name = h.name || `Event${string_1.generateHash(h.toString())}`, description = name, tags = []) {
const handler = new FunctionWrappingEventHandler(name, description, h, factory, subscription, tags);
scan_1.registerEvent(handler);
return handler;
}
exports.eventHandlerFrom = eventHandlerFrom;
class FunctionWrappingEventHandler {
constructor(name, description, h, parametersFactory,
// tslint:disable-next-line:variable-name
_subscription,
// tslint:disable-next-line:variable-name
_tags = []) {
this.name = name;
this.description = description;
this.h = h;
this.parametersFactory = parametersFactory;
this._subscription = _subscription;
this._tags = _tags;
const newParamInstance = this.freshParametersInstance();
const md = metadataReading_1.metadataFromInstance(newParamInstance);
this.values = md.values;
this.secrets = md.secrets;
this.tags = string_1.toStringArray(_tags).map(t => ({ name: t, description: t }));
this.subscription = GraphQL.inlineQuery(GraphQL.replaceOperationName(this._subscription, this.name));
this.subscriptionName = GraphQL.operationName(this.subscription);
}
freshParametersInstance() {
return constructionUtils_1.toFactory(this.parametersFactory)();
}
handle(e, ctx, params) {
return this.h(e, ctx, params);
}
}
//# sourceMappingURL=onEvent.js.map