@robotlegsjs/core
Version:
An architecture-based IoC framework for JavaScript/TypeScript
80 lines • 3.9 kB
JavaScript
"use strict";
// ------------------------------------------------------------------------------
// Copyright (c) 2017-present, RobotlegsJS. All Rights Reserved.
//
// NOTICE: You are permitted to use, modify, and distribute this file
// in accordance with the terms of the license agreement accompanying it.
// ------------------------------------------------------------------------------
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventCommandTrigger = void 0;
var getQualifiedClassName_1 = require("../../../framework/impl/getQualifiedClassName");
var CommandPayload_1 = require("../../commandCenter/api/CommandPayload");
var CommandExecutor_1 = require("../../commandCenter/impl/CommandExecutor");
var CommandMapper_1 = require("../../commandCenter/impl/CommandMapper");
var CommandMappingList_1 = require("../../commandCenter/impl/CommandMappingList");
var isInstanceOfType_1 = require("../../matching/isInstanceOfType");
/**
* @private
*/
var EventCommandTrigger = /** @class */ (function () {
/*============================================================================*/
/* Constructor */
/*============================================================================*/
/**
* @private
*/
function EventCommandTrigger(injector, dispatcher, type, eventClass, processors, logger) {
this._dispatcher = dispatcher;
this._type = type;
this._eventClass = eventClass;
this._mappings = new CommandMappingList_1.CommandMappingList(this, processors ? processors : [], logger);
this._executor = new CommandExecutor_1.CommandExecutor(injector, this._mappings.removeMapping.bind(this._mappings));
}
/*============================================================================*/
/* Public Functions */
/*============================================================================*/
/**
* @private
*/
EventCommandTrigger.prototype.createMapper = function () {
return new CommandMapper_1.CommandMapper(this._mappings);
};
/**
* @inheritDoc
*/
EventCommandTrigger.prototype.activate = function () {
this._dispatcher.addEventListener(this._type, this._eventHandler, this);
};
/**
* @inheritDoc
*/
EventCommandTrigger.prototype.deactivate = function () {
this._dispatcher.removeEventListener(this._type, this._eventHandler, this);
};
EventCommandTrigger.prototype.toString = function () {
var eventDescription = "";
eventDescription = !this._eventClass ? "Event" : getQualifiedClassName_1.getQualifiedClassName(this._eventClass);
return eventDescription + " with selector '" + this._type + "'";
};
/*============================================================================*/
/* Private Functions */
/*============================================================================*/
EventCommandTrigger.prototype._eventHandler = function (event) {
var eventConstructor = event.constructor;
var payloadEventClass;
// not pretty, but optimized to avoid duplicate checks and shortest paths
if (!this._eventClass) {
payloadEventClass = eventConstructor;
}
else if (isInstanceOfType_1.isInstanceOfType(event, this._eventClass)) {
payloadEventClass = this._eventClass;
}
else {
return;
}
this._executor.executeCommands(this._mappings.getList(), new CommandPayload_1.CommandPayload([event], [payloadEventClass]));
};
return EventCommandTrigger;
}());
exports.EventCommandTrigger = EventCommandTrigger;
//# sourceMappingURL=EventCommandTrigger.js.map