@robotlegsjs/core
Version:
An architecture-based IoC framework for JavaScript/TypeScript
103 lines • 3.63 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.EventRelay = void 0;
/**
* Relays events from a source to a destination
*/
var EventRelay = /** @class */ (function () {
/*============================================================================*/
/* Constructor */
/*============================================================================*/
/**
* Relays events from the source to the destination
*
* @param source Event Dispatcher
* @param destination Event Dispatcher
* @param types The list of event types to relay
*/
function EventRelay(source, destination, types) {
this._source = source;
this._destination = destination;
this._types = types || [];
}
/*============================================================================*/
/* Public Functions */
/*============================================================================*/
/**
* Start relaying events
*
* @return Self
*/
EventRelay.prototype.start = function () {
if (!this._active) {
this._active = true;
this._addListeners();
}
return this;
};
/**
* Stop relaying events
*
* @return Self
*/
EventRelay.prototype.stop = function () {
if (this._active) {
this._active = false;
this._removeListeners();
}
return this;
};
/**
* Add a new event type to relay
*
* @param eventType
*/
EventRelay.prototype.addType = function (eventType) {
this._types.push(eventType);
if (this._active) {
this._addListener(eventType);
}
};
/**
* Remove a relay event type
*
* @param eventType
*/
EventRelay.prototype.removeType = function (eventType) {
var index = this._types.indexOf(eventType);
if (index > -1) {
this._types.splice(index, 1);
this._removeListener(eventType);
}
};
/*============================================================================*/
/* Private Functions */
/*============================================================================*/
EventRelay.prototype._addListener = function (type) {
this._source.addEventListener(type, this._destination.dispatchEvent, this._destination);
};
EventRelay.prototype._removeListener = function (type) {
this._source.removeEventListener(type, this._destination.dispatchEvent, this._destination);
};
EventRelay.prototype._addListeners = function () {
var _this = this;
this._types.forEach(function (type) {
_this._addListener(type);
});
};
EventRelay.prototype._removeListeners = function () {
var _this = this;
this._types.forEach(function (type) {
_this._removeListener(type);
});
};
return EventRelay;
}());
exports.EventRelay = EventRelay;
//# sourceMappingURL=EventRelay.js.map