@robotlegsjs/core
Version:
An architecture-based IoC framework for JavaScript/TypeScript
431 lines • 14.6 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.Context = void 0;
var tslib_1 = require("tslib");
var inversify_1 = require("inversify");
var EventDispatcher_1 = require("../../events/impl/EventDispatcher");
var IContext_1 = require("../api/IContext");
var IInjector_1 = require("../api/IInjector");
var LifecycleEvent_1 = require("../api/LifecycleEvent");
var ConfigManager_1 = require("./ConfigManager");
var ExtensionInstaller_1 = require("./ExtensionInstaller");
var Lifecycle_1 = require("./Lifecycle");
var LogManager_1 = require("./LogManager");
var Pin_1 = require("./Pin");
var RobotlegsInjector_1 = require("./RobotlegsInjector");
var UID_1 = require("./UID");
// [Event(name="destroy", type="robotlegs.bender.framework.api.LifecycleEvent")]
// [Event(name="detain", type="robotlegs.bender.framework.api.PinEvent")]
// [Event(name="initialize", type="robotlegs.bender.framework.api.LifecycleEvent")]
// [Event(name="postDestroy", type="robotlegs.bender.framework.api.LifecycleEvent")]
// [Event(name="postInitialize", type="robotlegs.bender.framework.api.LifecycleEvent")]
// [Event(name="postResume", type="robotlegs.bender.framework.api.LifecycleEvent")]
// [Event(name="postSuspend", type="robotlegs.bender.framework.api.LifecycleEvent")]
// [Event(name="preDestroy", type="robotlegs.bender.framework.api.LifecycleEvent")]
// [Event(name="preInitialize", type="robotlegs.bender.framework.api.LifecycleEvent")]
// [Event(name="preResume", type="robotlegs.bender.framework.api.LifecycleEvent")]
// [Event(name="preSuspend", type="robotlegs.bender.framework.api.LifecycleEvent")]
// [Event(name="release", type="robotlegs.bender.framework.api.PinEvent")]
// [Event(name="resume", type="robotlegs.bender.framework.api.LifecycleEvent")]
// [Event(name="stateChange", type="robotlegs.bender.framework.api.LifecycleEvent")]
// [Event(name="suspend", type="robotlegs.bender.framework.api.LifecycleEvent")]
/**
* The core Robotlegs Context implementation
*/
var Context = /** @class */ (function (_super) {
tslib_1.__extends(Context, _super);
/*============================================================================*/
/* Constructor */
/*============================================================================*/
/**
* Creates a new Context
*/
function Context() {
var _this = _super.call(this) || this;
/*============================================================================*/
/* Private Properties */
/*============================================================================*/
_this._uid = UID_1.UID.create(Context_1);
_this._children = [];
_this._setup();
return _this;
}
Context_1 = Context;
Object.defineProperty(Context.prototype, "injector", {
/*============================================================================*/
/* Public Properties */
/*============================================================================*/
/**
* @inheritDoc
*/
get: function () {
return this._injector;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Context.prototype, "logLevel", {
/**
* @inheritDoc
*/
get: function () {
return this._logManager.logLevel;
},
/**
* @inheritDoc
*/
set: function (value) {
this._logManager.logLevel = value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Context.prototype, "state", {
/**
* @inheritDoc
*/
get: function () {
return this._lifecycle.state;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Context.prototype, "uninitialized", {
/**
* @inheritDoc
*/
get: function () {
return this._lifecycle.uninitialized;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Context.prototype, "initialized", {
/**
* @inheritDoc
*/
get: function () {
return this._lifecycle.initialized;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Context.prototype, "active", {
/**
* @inheritDoc
*/
get: function () {
return this._lifecycle.active;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Context.prototype, "suspended", {
/**
* @inheritDoc
*/
get: function () {
return this._lifecycle.suspended;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Context.prototype, "destroyed", {
/**
* @inheritDoc
*/
get: function () {
return this._lifecycle.destroyed;
},
enumerable: false,
configurable: true
});
/*============================================================================*/
/* Public Functions */
/*============================================================================*/
/**
* @inheritDoc
*/
Context.prototype.initialize = function (callback) {
this._lifecycle.initialize(callback);
};
/**
* @inheritDoc
*/
Context.prototype.suspend = function (callback) {
this._lifecycle.suspend(callback);
};
/**
* @inheritDoc
*/
Context.prototype.resume = function (callback) {
this._lifecycle.resume(callback);
};
/**
* @inheritDoc
*/
Context.prototype.destroy = function (callback) {
this._lifecycle.destroy(callback);
};
/**
* @inheritDoc
*/
Context.prototype.beforeInitializing = function (handler) {
this._lifecycle.beforeInitializing(handler);
return this;
};
/**
* @inheritDoc
*/
Context.prototype.whenInitializing = function (handler) {
this._lifecycle.whenInitializing(handler);
return this;
};
/**
* @inheritDoc
*/
Context.prototype.afterInitializing = function (handler) {
this._lifecycle.afterInitializing(handler);
return this;
};
/**
* @inheritDoc
*/
Context.prototype.beforeSuspending = function (handler) {
this._lifecycle.beforeSuspending(handler);
return this;
};
/**
* @inheritDoc
*/
Context.prototype.whenSuspending = function (handler) {
this._lifecycle.whenSuspending(handler);
return this;
};
/**
* @inheritDoc
*/
Context.prototype.afterSuspending = function (handler) {
this._lifecycle.afterSuspending(handler);
return this;
};
/**
* @inheritDoc
*/
Context.prototype.beforeResuming = function (handler) {
this._lifecycle.beforeResuming(handler);
return this;
};
/**
* @inheritDoc
*/
Context.prototype.whenResuming = function (handler) {
this._lifecycle.whenResuming(handler);
return this;
};
/**
* @inheritDoc
*/
Context.prototype.afterResuming = function (handler) {
this._lifecycle.afterResuming(handler);
return this;
};
/**
* @inheritDoc
*/
Context.prototype.beforeDestroying = function (handler) {
this._lifecycle.beforeDestroying(handler);
return this;
};
/**
* @inheritDoc
*/
Context.prototype.whenDestroying = function (handler) {
this._lifecycle.whenDestroying(handler);
return this;
};
/**
* @inheritDoc
*/
Context.prototype.afterDestroying = function (handler) {
this._lifecycle.afterDestroying(handler);
return this;
};
/**
* @inheritDoc
*/
Context.prototype.install = function () {
var _this = this;
var extensions = [];
for (var _i = 0; _i < arguments.length; _i++) {
extensions[_i] = arguments[_i];
}
extensions.forEach(function (extension) {
_this._extensionInstaller.install(extension);
});
return this;
};
/**
* @inheritDoc
*/
Context.prototype.configure = function () {
var _this = this;
var configs = [];
for (var _i = 0; _i < arguments.length; _i++) {
configs[_i] = arguments[_i];
}
configs.forEach(function (config) {
_this._configManager.addConfig(config);
});
return this;
};
/**
* @inheritDoc
*/
Context.prototype.addChild = function (child) {
if (this._children.indexOf(child) === -1) {
this._logger.debug("Adding child context {0}", [child]);
if (!child.uninitialized) {
this._logger.warn("Child context {0} must be uninitialized", [child]);
}
if (child.injector.parent) {
this._logger.warn("Child context {0} must not have a parent Injector", [child]);
}
this._children.push(child);
child.injector.parent = this.injector;
child.addEventListener(LifecycleEvent_1.LifecycleEvent.POST_DESTROY, this._onChildDestroy, this);
}
return this;
};
/**
* @inheritDoc
*/
Context.prototype.removeChild = function (child) {
var childIndex = this._children.indexOf(child);
if (childIndex > -1) {
this._logger.debug("Removing child context {0}", [child]);
this._children.splice(childIndex, 1);
child.injector.parent = null;
child.removeEventListener(LifecycleEvent_1.LifecycleEvent.POST_DESTROY, this._onChildDestroy, this);
}
else {
this._logger.warn("Child context {0} must be a child of {1}", [child, this]);
}
return this;
};
/**
* @inheritDoc
*/
Context.prototype.addConfigHandler = function (matcher, handler) {
this._configManager.addConfigHandler(matcher, handler);
return this;
};
/**
* @inheritDoc
*/
Context.prototype.getLogger = function (source) {
return this._logManager.getLogger(source);
};
/**
* @inheritDoc
*/
Context.prototype.addLogTarget = function (target) {
this._logManager.addLogTarget(target);
return this;
};
/**
* @inheritDoc
*/
Context.prototype.detain = function () {
var _this = this;
var instances = [];
for (var _i = 0; _i < arguments.length; _i++) {
instances[_i] = arguments[_i];
}
instances.forEach(function (instance) {
_this._pin.detain(instance);
});
return this;
};
/**
* @inheritDoc
*/
Context.prototype.release = function () {
var _this = this;
var instances = [];
for (var _i = 0; _i < arguments.length; _i++) {
instances[_i] = arguments[_i];
}
instances.forEach(function (instance) {
_this._pin.release(instance);
});
return this;
};
/**
* @inheritDoc
*/
Context.prototype.toString = function () {
return this._uid;
};
/*============================================================================*/
/* Private Functions */
/*============================================================================*/
/**
* Configures mandatory context dependencies
*/
Context.prototype._setup = function () {
this._logManager = new LogManager_1.LogManager();
this._injector = new RobotlegsInjector_1.RobotlegsInjector();
this._injector.bind(IInjector_1.IInjector).toConstantValue(this._injector);
this._injector.bind(IContext_1.IContext).toConstantValue(this);
this._logger = this._logManager.getLogger(this);
this._pin = new Pin_1.Pin(this);
this._lifecycle = new Lifecycle_1.Lifecycle(this);
this._configManager = new ConfigManager_1.ConfigManager(this);
this._extensionInstaller = new ExtensionInstaller_1.ExtensionInstaller(this);
this.beforeInitializing(this._beforeInitializingCallback.bind(this));
this.afterInitializing(this._afterInitializingCallback.bind(this));
this.beforeDestroying(this._beforeDestroyingCallback.bind(this));
this.afterDestroying(this._afterDestroyingCallback.bind(this));
};
Context.prototype._beforeInitializingCallback = function () {
this._logger.debug("Initializing...");
};
Context.prototype._afterInitializingCallback = function () {
this._logger.debug("Initialize complete");
};
Context.prototype._beforeDestroyingCallback = function () {
this._logger.debug("Destroying...");
};
Context.prototype._afterDestroyingCallback = function () {
this._extensionInstaller.destroy();
this._configManager.destroy();
this._pin.releaseAll();
this._injector.unbindAll();
this._removeChildren();
this._logger.debug("Destroy complete");
this._logManager.removeAllTargets();
};
Context.prototype._onChildDestroy = function (event) {
this.removeChild(event.target);
};
Context.prototype._removeChildren = function () {
while (this._children.length > 0) {
this.removeChild(this._children[this._children.length - 1]);
}
};
var Context_1;
Context = Context_1 = tslib_1.__decorate([
inversify_1.injectable(),
tslib_1.__metadata("design:paramtypes", [])
], Context);
return Context;
}(EventDispatcher_1.EventDispatcher));
exports.Context = Context;
//# sourceMappingURL=Context.js.map