UNPKG

2.19 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const GraphQL = require("./internal/graph/graphQL");
4const metadataReading_1 = require("./internal/metadata/metadataReading");
5const string_1 = require("./internal/util/string");
6const scan_1 = require("./scan");
7const constructionUtils_1 = require("./util/constructionUtils");
8/**
9 * Create a HandleEvent instance with the appropriate metadata wrapping
10 * the given function
11 * @param {OnEvent<T, P>} h
12 * @param {Maker<P>} factory
13 * @param {string} subscription
14 * @param {string} name
15 * @param {string} description
16 * @param {string | string[]} tags
17 * @returns {HandleEvent<T, P> & EventHandlerMetadata}
18 */
19function eventHandlerFrom(h, factory, subscription, name = h.name || `Event${string_1.generateHash(h.toString())}`, description = name, tags = []) {
20 const handler = new FunctionWrappingEventHandler(name, description, h, factory, subscription, tags);
21 scan_1.registerEvent(handler);
22 return handler;
23}
24exports.eventHandlerFrom = eventHandlerFrom;
25class FunctionWrappingEventHandler {
26 constructor(name, description, h, parametersFactory,
27 // tslint:disable-next-line:variable-name
28 _subscription,
29 // tslint:disable-next-line:variable-name
30 _tags = []) {
31 this.name = name;
32 this.description = description;
33 this.h = h;
34 this.parametersFactory = parametersFactory;
35 this._subscription = _subscription;
36 this._tags = _tags;
37 const newParamInstance = this.freshParametersInstance();
38 const md = metadataReading_1.metadataFromInstance(newParamInstance);
39 this.values = md.values;
40 this.secrets = md.secrets;
41 this.tags = string_1.toStringArray(_tags).map(t => ({ name: t, description: t }));
42 this.subscription = GraphQL.inlineQuery(GraphQL.replaceOperationName(this._subscription, this.name));
43 this.subscriptionName = GraphQL.operationName(this.subscription);
44 }
45 freshParametersInstance() {
46 return constructionUtils_1.toFactory(this.parametersFactory)();
47 }
48 handle(e, ctx, params) {
49 return this.h(e, ctx, params);
50 }
51}
52//# sourceMappingURL=onEvent.js.map
\No newline at end of file