@robotlegsjs/core
Version:
An architecture-based IoC framework for JavaScript/TypeScript
368 lines • 15.3 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.Lifecycle = void 0;
var LifecycleError_1 = require("../api/LifecycleError");
var LifecycleEvent_1 = require("../api/LifecycleEvent");
var LifecycleState_1 = require("../api/LifecycleState");
var LifecycleTransition_1 = require("./LifecycleTransition");
// [Event(name="destroy", type="robotlegs.bender.framework.api.LifecycleEvent")]
// [Event(name="error", type="robotlegs.bender.framework.api.LifecycleEvent")]
// [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="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")]
/**
* Default object lifecycle
*
* @private
*/
var Lifecycle = /** @class */ (function () {
/*============================================================================*/
/* Constructor */
/*============================================================================*/
/**
* Creates a lifecycle for a given target object
*
* @param target The target object
*/
function Lifecycle(target) {
/*============================================================================*/
/* Public Properties */
/*============================================================================*/
this._state = LifecycleState_1.LifecycleState.UNINITIALIZED;
/*============================================================================*/
/* Private Properties */
/*============================================================================*/
this._reversedEventTypes = new Map();
this._reversePriority = 0;
this._target = target;
this._dispatcher = target; // || new EventDispatcher(this);
this._configureTransitions();
}
Object.defineProperty(Lifecycle.prototype, "state", {
/**
* @inheritDoc
*/
get: function () {
return this._state;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Lifecycle.prototype, "target", {
/**
* @inheritDoc
*/
get: function () {
return this._target;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Lifecycle.prototype, "uninitialized", {
/**
* @inheritDoc
*/
get: function () {
return this._state === LifecycleState_1.LifecycleState.UNINITIALIZED;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Lifecycle.prototype, "initialized", {
/**
* @inheritDoc
*/
get: function () {
return (this._state !== LifecycleState_1.LifecycleState.UNINITIALIZED &&
this._state !== LifecycleState_1.LifecycleState.INITIALIZING);
},
enumerable: false,
configurable: true
});
Object.defineProperty(Lifecycle.prototype, "active", {
/**
* @inheritDoc
*/
get: function () {
return this._state === LifecycleState_1.LifecycleState.ACTIVE;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Lifecycle.prototype, "suspended", {
/**
* @inheritDoc
*/
get: function () {
return this._state === LifecycleState_1.LifecycleState.SUSPENDED;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Lifecycle.prototype, "destroyed", {
/**
* @inheritDoc
*/
get: function () {
return this._state === LifecycleState_1.LifecycleState.DESTROYED;
},
enumerable: false,
configurable: true
});
/*============================================================================*/
/* Public Functions */
/*============================================================================*/
/**
* @inheritDoc
*/
Lifecycle.prototype.initialize = function (callback) {
this._initialize.enter(callback);
};
/**
* @inheritDoc
*/
Lifecycle.prototype.suspend = function (callback) {
this._suspend.enter(callback);
};
/**
* @inheritDoc
*/
Lifecycle.prototype.resume = function (callback) {
this._resume.enter(callback);
};
/**
* @inheritDoc
*/
Lifecycle.prototype.destroy = function (callback) {
this._destroy.enter(callback);
};
/**
* @inheritDoc
*/
Lifecycle.prototype.beforeInitializing = function (handler) {
if (!this.uninitialized) {
this._reportError(LifecycleError_1.LifecycleError.LATE_HANDLER_ERROR_MESSAGE);
}
this._initialize.addBeforeHandler(handler);
return this;
};
/**
* @inheritDoc
*/
Lifecycle.prototype.whenInitializing = function (handler) {
if (this.initialized) {
this._reportError(LifecycleError_1.LifecycleError.LATE_HANDLER_ERROR_MESSAGE);
}
this.addEventListener(LifecycleEvent_1.LifecycleEvent.INITIALIZE, this._createSyncLifecycleListener(handler, true));
return this;
};
/**
* @inheritDoc
*/
Lifecycle.prototype.afterInitializing = function (handler) {
if (this.initialized) {
this._reportError(LifecycleError_1.LifecycleError.LATE_HANDLER_ERROR_MESSAGE);
}
this.addEventListener(LifecycleEvent_1.LifecycleEvent.POST_INITIALIZE, this._createSyncLifecycleListener(handler, true));
return this;
};
/**
* @inheritDoc
*/
Lifecycle.prototype.beforeSuspending = function (handler) {
this._suspend.addBeforeHandler(handler);
return this;
};
/**
* @inheritDoc
*/
Lifecycle.prototype.whenSuspending = function (handler) {
this.addEventListener(LifecycleEvent_1.LifecycleEvent.SUSPEND, this._createSyncLifecycleListener(handler));
return this;
};
/**
* @inheritDoc
*/
Lifecycle.prototype.afterSuspending = function (handler) {
this.addEventListener(LifecycleEvent_1.LifecycleEvent.POST_SUSPEND, this._createSyncLifecycleListener(handler));
return this;
};
/**
* @inheritDoc
*/
Lifecycle.prototype.beforeResuming = function (handler) {
this._resume.addBeforeHandler(handler);
return this;
};
/**
* @inheritDoc
*/
Lifecycle.prototype.whenResuming = function (handler) {
this.addEventListener(LifecycleEvent_1.LifecycleEvent.RESUME, this._createSyncLifecycleListener(handler));
return this;
};
/**
* @inheritDoc
*/
Lifecycle.prototype.afterResuming = function (handler) {
this.addEventListener(LifecycleEvent_1.LifecycleEvent.POST_RESUME, this._createSyncLifecycleListener(handler));
return this;
};
/**
* @inheritDoc
*/
Lifecycle.prototype.beforeDestroying = function (handler) {
this._destroy.addBeforeHandler(handler);
return this;
};
/**
* @inheritDoc
*/
Lifecycle.prototype.whenDestroying = function (handler) {
this.addEventListener(LifecycleEvent_1.LifecycleEvent.DESTROY, this._createSyncLifecycleListener(handler, true));
return this;
};
/**
* @inheritDoc
*/
Lifecycle.prototype.afterDestroying = function (handler) {
this.addEventListener(LifecycleEvent_1.LifecycleEvent.POST_DESTROY, this._createSyncLifecycleListener(handler, true));
return this;
};
/**
* @inheritDoc
*/
Lifecycle.prototype.addEventListener = function (type, listener, useCapture, priority, useWeakReference) {
if (useCapture === void 0) { useCapture = false; }
if (priority === void 0) { priority = 0; }
if (useWeakReference === void 0) { useWeakReference = false; }
priority = this._flipPriority(type, priority);
// this._dispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
this._dispatcher.addEventListener(type, listener, undefined, useCapture, priority);
};
/**
* @inheritDoc
*/
Lifecycle.prototype.removeEventListener = function (type, listener, useCapture) {
if (useCapture === void 0) { useCapture = false; }
this._dispatcher.removeEventListener(type, listener);
};
/**
* @inheritDoc
*/
Lifecycle.prototype.dispatchEvent = function (event) {
return this._dispatcher.dispatchEvent(event);
};
/**
* @inheritDoc
*/
Lifecycle.prototype.hasEventListener = function (type) {
return this._dispatcher.hasEventListener(type);
};
/**
* @inheritDoc
*/
Lifecycle.prototype.willTrigger = function (type) {
return this._dispatcher.willTrigger(type);
};
/*============================================================================*/
/* Internal Functions */
/*============================================================================*/
Lifecycle.prototype.setCurrentState = function (state) {
if (this._state !== state) {
this._state = state;
this.dispatchEvent(new LifecycleEvent_1.LifecycleEvent(LifecycleEvent_1.LifecycleEvent.STATE_CHANGE));
}
};
Lifecycle.prototype.addReversedEventTypes = function () {
var _this = this;
var types = [];
for (var _i = 0; _i < arguments.length; _i++) {
types[_i] = arguments[_i];
}
types.forEach(function (type) {
_this._reversedEventTypes.set(type, true);
});
};
/*============================================================================*/
/* Private Functions */
/*============================================================================*/
Lifecycle.prototype._configureTransitions = function () {
this._initialize = new LifecycleTransition_1.LifecycleTransition(LifecycleEvent_1.LifecycleEvent.PRE_INITIALIZE, this)
.fromStates(LifecycleState_1.LifecycleState.UNINITIALIZED)
.toStates(LifecycleState_1.LifecycleState.INITIALIZING, LifecycleState_1.LifecycleState.ACTIVE)
.withEvents(LifecycleEvent_1.LifecycleEvent.PRE_INITIALIZE, LifecycleEvent_1.LifecycleEvent.INITIALIZE, LifecycleEvent_1.LifecycleEvent.POST_INITIALIZE);
this._suspend = new LifecycleTransition_1.LifecycleTransition(LifecycleEvent_1.LifecycleEvent.PRE_SUSPEND, this)
.fromStates(LifecycleState_1.LifecycleState.ACTIVE)
.toStates(LifecycleState_1.LifecycleState.SUSPENDING, LifecycleState_1.LifecycleState.SUSPENDED)
.withEvents(LifecycleEvent_1.LifecycleEvent.PRE_SUSPEND, LifecycleEvent_1.LifecycleEvent.SUSPEND, LifecycleEvent_1.LifecycleEvent.POST_SUSPEND)
.inReverse();
this._resume = new LifecycleTransition_1.LifecycleTransition(LifecycleEvent_1.LifecycleEvent.PRE_RESUME, this)
.fromStates(LifecycleState_1.LifecycleState.SUSPENDED)
.toStates(LifecycleState_1.LifecycleState.RESUMING, LifecycleState_1.LifecycleState.ACTIVE)
.withEvents(LifecycleEvent_1.LifecycleEvent.PRE_RESUME, LifecycleEvent_1.LifecycleEvent.RESUME, LifecycleEvent_1.LifecycleEvent.POST_RESUME);
this._destroy = new LifecycleTransition_1.LifecycleTransition(LifecycleEvent_1.LifecycleEvent.PRE_DESTROY, this)
.fromStates(LifecycleState_1.LifecycleState.SUSPENDED, LifecycleState_1.LifecycleState.ACTIVE)
.toStates(LifecycleState_1.LifecycleState.DESTROYING, LifecycleState_1.LifecycleState.DESTROYED)
.withEvents(LifecycleEvent_1.LifecycleEvent.PRE_DESTROY, LifecycleEvent_1.LifecycleEvent.DESTROY, LifecycleEvent_1.LifecycleEvent.POST_DESTROY)
.inReverse();
};
Lifecycle.prototype._flipPriority = function (type, priority) {
return priority === 0 && this._reversedEventTypes.get(type)
? this._reversePriority++
: priority;
};
Lifecycle.prototype._createSyncLifecycleListener = function (handler, once) {
if (once === void 0) { once = false; }
// When and After handlers can not be asynchronous
if (handler.length > 1) {
throw new LifecycleError_1.LifecycleError(LifecycleError_1.LifecycleError.SYNC_HANDLER_ARG_MISMATCH);
}
// A handler that accepts 1 argument is provided with the event type
if (handler.length === 1) {
return function (event) {
if (once) {
// (<IEventDispatcher>event.target).removeEventListener(event.type, arguments.callee);
event.target.removeEventListener(event.type, handler);
}
handler(event.type);
};
}
// Or, just call the handler
return function (event) {
if (once) {
// (<IEventDispatcher>event.target).removeEventListener(event.type, arguments.callee);
event.target.removeEventListener(event.type, handler);
}
handler();
};
};
Lifecycle.prototype._reportError = function (message) {
var error = new LifecycleError_1.LifecycleError(message);
if (this.hasEventListener(LifecycleEvent_1.LifecycleEvent.ERROR)) {
var event_1 = new LifecycleEvent_1.LifecycleEvent(LifecycleEvent_1.LifecycleEvent.ERROR, error);
this.dispatchEvent(event_1);
}
else {
throw error;
}
};
return Lifecycle;
}());
exports.Lifecycle = Lifecycle;
//# sourceMappingURL=Lifecycle.js.map