@robotlegsjs/phaser
Version:
RobotlegsJS integration with Phaser Scene Manager
128 lines • 5.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.AbstractMediatorFactory = void 0;
var core_1 = require("@robotlegsjs/core");
/**
* @protected
*/
var AbstractMediatorFactory = /** @class */ (function () {
/*============================================================================*/
/* Constructor */
/*============================================================================*/
/**
* @private
*/
function AbstractMediatorFactory(injector) {
/*============================================================================*/
/* Private Properties */
/*============================================================================*/
this._mediators = new Map();
this._injector = injector;
}
/*============================================================================*/
/* Public Functions */
/*============================================================================*/
/**
* @private
*/
AbstractMediatorFactory.prototype.getMediator = function (item, mapping) {
return this._mediators.get(item) ? this._mediators.get(item).get(mapping) : null;
};
/**
* @private
*/
AbstractMediatorFactory.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
*/
AbstractMediatorFactory.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
*/
AbstractMediatorFactory.prototype.removeAllMediators = function () {
var _this = this;
this._mediators.forEach(function (value, key) { return _this.removeMediators(key); });
};
/*============================================================================*/
/* Private Functions */
/*============================================================================*/
AbstractMediatorFactory.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;
};
AbstractMediatorFactory.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);
};
AbstractMediatorFactory.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);
});
};
AbstractMediatorFactory.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);
}
});
};
AbstractMediatorFactory.prototype.requiredTypesFor = function (filter, type) {
var requiredTypes = filter.allOfTypes.concat(filter.anyOfTypes);
if (requiredTypes.indexOf(type) === -1) {
requiredTypes.push(type);
}
return requiredTypes;
};
return AbstractMediatorFactory;
}());
exports.AbstractMediatorFactory = AbstractMediatorFactory;
//# sourceMappingURL=AbstractMediatorFactory.js.map