@dolittle/sdk.events.handling
Version:
Dolittle is a decentralized, distributed, event-driven microservice platform built to harness the power of events.
43 lines • 4 kB
JavaScript
;
// Copyright (c) Dolittle. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDecoratedEventHandlerType = exports.isDecoratedEventHandlerType = exports.eventHandler = void 0;
const sdk_common_1 = require("@dolittle/sdk.common");
const sdk_events_1 = require("@dolittle/sdk.events");
const EventHandlerAlias_1 = require("../EventHandlerAlias");
const EventHandlerId_1 = require("../EventHandlerId");
const EventHandlerDecoratedType_1 = require("./EventHandlerDecoratedType");
const [decorator, getMetadata] = sdk_common_1.Decorators.createMetadataDecorator('event-handler', 'eventHandler', sdk_common_1.Decorators.DecoratorTarget.Class);
/**
* Decorator to mark a class as an EventHandler.
* @param {EventHandlerId | Guid | string} eventHandlerId - The id to associate with this EventHandler.
* @param {EventHandlerOptions} [options={}] - Options to give to the EventHandler.
* @returns {Decorators.Decorator} The decorator to apply.
*/
function eventHandler(eventHandlerId, options = {}) {
return decorator((target, type) => {
var _a;
return new EventHandlerDecoratedType_1.EventHandlerDecoratedType(EventHandlerId_1.EventHandlerId.from(eventHandlerId), options.inScope ? sdk_events_1.ScopeId.from(options.inScope) : sdk_events_1.ScopeId.default, options.partitioned === undefined || options.partitioned, EventHandlerAlias_1.EventHandlerAlias.from((_a = options.alias) !== null && _a !== void 0 ? _a : type.name), type);
});
}
exports.eventHandler = eventHandler;
/**
* Checks whether the specified class is decorated with an event handler type.
* @param {Constructor<any>} type - The class to check the decorated event handler type for.
* @returns {boolean} True if the decorator is applied, false if not.
*/
function isDecoratedEventHandlerType(type) {
return getMetadata(type, false, false) !== undefined;
}
exports.isDecoratedEventHandlerType = isDecoratedEventHandlerType;
/**
* Gets the {@link EventHandlerDecoratedType} of the specified class.
* @param {Constructor<any>} type - The class to get the decorated event handler type for.
* @returns {EventHandlerDecoratedType} The decorated event handler type.
*/
function getDecoratedEventHandlerType(type) {
return getMetadata(type, true, 'Classes used as event handlers must be decorated');
}
exports.getDecoratedEventHandlerType = getDecoratedEventHandlerType;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZXZlbnRIYW5kbGVyRGVjb3JhdG9yLmpzIiwic291cmNlUm9vdCI6Ii4uLyIsInNvdXJjZXMiOlsiQnVpbGRlcnMvZXZlbnRIYW5kbGVyRGVjb3JhdG9yLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSwrQ0FBK0M7QUFDL0MscUdBQXFHOzs7QUFLckcscURBQWtEO0FBQ2xELHFEQUErQztBQUUvQyw0REFBeUQ7QUFDekQsc0RBQW1EO0FBQ25ELDJFQUF3RTtBQUd4RSxNQUFNLENBQUMsU0FBUyxFQUFFLFdBQVcsQ0FBQyxHQUFHLHVCQUFVLENBQUMsdUJBQXVCLENBQTRCLGVBQWUsRUFBRSxjQUFjLEVBQUUsdUJBQVUsQ0FBQyxlQUFlLENBQUMsS0FBSyxDQUFDLENBQUM7QUFFbEs7Ozs7O0dBS0c7QUFDSCxTQUFnQixZQUFZLENBQUMsY0FBOEMsRUFBRSxVQUErQixFQUFFO0lBQzFHLE9BQU8sU0FBUyxDQUFDLENBQUMsTUFBTSxFQUFFLElBQUksRUFBRSxFQUFFOztRQUM5QixPQUFPLElBQUkscURBQXlCLENBQ2hDLCtCQUFjLENBQUMsSUFBSSxDQUFDLGNBQWMsQ0FBQyxFQUNuQyxPQUFPLENBQUMsT0FBTyxDQUFDLENBQUMsQ0FBQyxvQkFBTyxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUMsT0FBTyxDQUFDLENBQUMsQ0FBQyxDQUFDLG9CQUFPLENBQUMsT0FBTyxFQUNqRSxPQUFPLENBQUMsV0FBVyxLQUFLLFNBQVMsSUFBSSxPQUFPLENBQUMsV0FBVyxFQUN4RCxxQ0FBaUIsQ0FBQyxJQUFJLENBQUMsTUFBQSxPQUFPLENBQUMs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