ajsfw
Version:
Ajs Framework
97 lines (96 loc) • 3.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var exceptions = require("./exceptions");
var Model = (function () {
function Model(container) {
this.__initialized = false;
this.__container = container;
}
Model.prototype._initialized = function () { return this.__initialized; };
Model.prototype.initialize = function () {
if (!this.__initialized) {
return this.__initialize();
}
};
Model.prototype.release = function () {
this.__release();
};
Model.prototype._onInitialize = function () {
return Promise.resolve();
};
Model.prototype._onInitialized = function () {
return;
};
Model.prototype._onFinalize = function () {
return;
};
Model.prototype._checkInitialized = function (exception, callForward) {
this.__checkInitialized(exception, callForward);
};
Model.prototype._waitInitialized = function (timeout) {
var _this = this;
var timeStep = 250;
return new Promise(function (resolve, reject) {
var elapsed = 0;
var w;
function wait() {
if (this.__initialized) {
resolve();
}
else {
elapsed += timeStep;
if (elapsed > timeout) {
reject(new exceptions.InitializationTimeoutException());
}
setTimeout(function () { return w(); }, timeStep);
}
}
w = wait.bind(_this);
w();
});
};
Model.prototype.__initialize = function () {
var _this = this;
var promise = new Promise(function (resolve) {
_this._onInitialize()
.then(function () {
_this.__initialized = true;
_this._onInitialized();
resolve();
});
});
return promise;
};
Model.prototype.__destroy = function () {
this._onFinalize();
};
Model.prototype.__release = function () {
if (this.__container.releaseScopedInstanceReference(this)) {
this.__destroy();
}
};
Model.prototype.__checkInitialized = function (exception, callForward) {
var _this = this;
if (!this.__initialized) {
var timeout_1 = 80;
var w8timer_1 = setInterval(function () {
if (_this.__initialized) {
clearInterval(w8timer_1);
callForward();
}
else {
timeout_1--;
if (timeout_1 <= 0) {
clearInterval(w8timer_1);
throw exception;
}
}
}, 250);
}
else {
callForward();
}
};
return Model;
}());
exports.Model = Model;