UNPKG

@robotlegsjs/createjs

Version:
130 lines 5.65 kB
"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.MediatorFactory = void 0; var core_1 = require("@robotlegsjs/core"); var MediatorManager_1 = require("./MediatorManager"); /** * @private */ var MediatorFactory = /** @class */ (function () { /*============================================================================*/ /* Constructor */ /*============================================================================*/ /** * @private */ function MediatorFactory(injector, manager) { /*============================================================================*/ /* Private Properties */ /*============================================================================*/ this._mediators = new Map(); this._injector = injector; this._manager = manager || new MediatorManager_1.MediatorManager(this); } /*============================================================================*/ /* Public Functions */ /*============================================================================*/ /** * @private */ MediatorFactory.prototype.getMediator = function (item, mapping) { return this._mediators.get(item) ? this._mediators.get(item).get(mapping) : null; }; /** * @private */ MediatorFactory.prototype.createMediators = function (item, type, mappings) { var _this = this; var createdMediators = []; var mediator; mappings.forEach(function (mapping) { mediator = _this.getMediator(item, mapping); if (!mediator) { _this._mapTypeForFilterBinding(mapping.matcher, type, item); mediator = _this._createMediator(item, mapping); _this._unmapTypeForFilterBinding(mapping.matcher, type, item); } if (mediator) { createdMediators.push(mediator); } }); return createdMediators; }; /** * @private */ MediatorFactory.prototype.removeMediators = function (item) { var _this = this; var mediators = this._mediators.get(item); if (!mediators) { return; } mediators.forEach(function (value, key) { return _this._manager.removeMediator(value, item, key); }); this._mediators.delete(item); }; /** * @private */ MediatorFactory.prototype.removeAllMediators = function () { var _this = this; this._mediators.forEach(function (value, key) { return _this.removeMediators(key); }); }; /*============================================================================*/ /* Private Functions */ /*============================================================================*/ MediatorFactory.prototype._createMediator = function (item, mapping) { var mediator = this.getMediator(item, mapping); if (mediator) { return mediator; } if (mapping.guards.length === 0 || core_1.guardsApprove(mapping.guards, this._injector)) { var mediatorClass = mapping.mediatorClass; mediator = core_1.instantiateUnmapped(this._injector, mediatorClass); if (mapping.hooks.length > 0) { this._injector.bind(mediatorClass).toConstantValue(mediator); core_1.applyHooks(mapping.hooks, this._injector); this._injector.unbind(mediatorClass); } this._addMediator(mediator, item, mapping); } return mediator; }; MediatorFactory.prototype._addMediator = function (mediator, item, mapping) { var mediatorMap = this._mediators.get(item) || new Map(); this._mediators.set(item, mediatorMap); mediatorMap.set(mapping, mediator); this._manager.addMediator(mediator, item, mapping); }; MediatorFactory.prototype._mapTypeForFilterBinding = function (filter, type, item) { var _this = this; var requiredTypes = this._requiredTypesFor(filter, type); requiredTypes.forEach(function (requiredType) { _this._injector.bind(requiredType).toConstantValue(item); }); }; MediatorFactory.prototype._unmapTypeForFilterBinding = function (filter, type, item) { var _this = this; var requiredTypes = this._requiredTypesFor(filter, type); requiredTypes.forEach(function (requiredType) { if (_this._injector.isBound(requiredType)) { _this._injector.unbind(requiredType); } }); }; MediatorFactory.prototype._requiredTypesFor = function (filter, type) { var requiredTypes = filter.allOfTypes.concat(filter.anyOfTypes); if (requiredTypes.indexOf(type) === -1) { requiredTypes.push(type); } return requiredTypes; }; return MediatorFactory; }()); exports.MediatorFactory = MediatorFactory; //# sourceMappingURL=MediatorFactory.js.map