mframejs
Version:
simple framework
173 lines • 5.89 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
var ClassPropertyObserver = (function () {
function ClassPropertyObserver(_class, key) {
this._class = _class;
this.key = key;
this.setterBind = this.setter.bind(this);
this.getterBind = this.getter.bind(this);
if (typeof this._class === 'object') {
this.value = this._class[this.key];
this.observe();
}
}
ClassPropertyObserver.prototype.unsubscribe = function (caller) {
if (this.c1 === caller) {
this.c1 = null;
}
if (this.c2 === caller) {
this.c2 = null;
}
if (this.c3 === caller) {
this.c3 = null;
}
if (this.c4 === caller) {
this.c4 = null;
}
if (this.c5 === caller) {
this.c5 = null;
}
if (this.callers && this.callers.indexOf(caller) !== -1) {
this.callers.splice(this.callers.indexOf(caller), 1);
}
};
ClassPropertyObserver.prototype.subscribe = function (caller) {
if (this.c1 !== caller && this.c2 !== caller && this.c3 !== caller && this.c4 !== caller && this.c5 !== caller) {
if (!this.c1) {
this.c1 = caller;
}
else {
if (!this.c2) {
this.c2 = caller;
}
else {
if (!this.c3) {
this.c3 = caller;
}
else {
if (!this.c4) {
this.c4 = caller;
}
else {
if (!this.c5) {
this.c5 = caller;
}
else {
if (!this.callers) {
this.callers = [];
}
if (this.callers.indexOf(caller) === -1) {
this.callers.push(caller);
}
}
}
}
}
}
}
};
ClassPropertyObserver.prototype.forceUpdate = function () {
if (this.c1) {
var cc1 = this.c1;
this.c1 = undefined;
cc1.update();
}
if (this.c2) {
var cc2 = this.c2;
this.c2 = undefined;
cc2.update();
}
if (this.c3) {
var cc3 = this.c3;
this.c3 = undefined;
cc3.update();
}
if (this.c4) {
var cc4 = this.c4;
this.c4 = undefined;
cc4.update();
}
if (this.c5) {
var cc5 = this.c5;
this.c5 = undefined;
cc5.update();
}
if (this.callers && this.callers.length > 0) {
var calls = this.callers.slice();
this.callers = [];
for (var i = 0; i < calls.length; i++) {
calls[i].update();
}
}
};
ClassPropertyObserver.prototype.observe = function () {
var descriptor = Reflect.getOwnPropertyDescriptor(this._class.__proto__, this.key);
var oldDescriptor = null;
if (descriptor) {
oldDescriptor = descriptor.get || descriptor.set;
if (oldDescriptor) {
console.warn('skipping dirty observer - no-dirty //todo remove this message');
}
}
if (oldDescriptor === null) {
if (!Reflect.defineProperty(this._class, this.key, {
configurable: true,
enumerable: true,
set: this.setterBind,
get: this.getterBind
})) {
console.warn('This key could not be observed:' + this.key);
}
}
};
ClassPropertyObserver.prototype.setter = function (newValue) {
if (newValue !== this.value) {
if (typeof newValue === 'object') {
if (newValue && newValue.__observer) {
newValue.__observer = null;
}
if (newValue && newValue.__observerArray) {
newValue.__observerArray = null;
}
}
this.value = newValue;
if (this.c1) {
var cc1 = this.c1;
this.c1 = undefined;
cc1.update();
}
if (this.c2) {
var cc2 = this.c2;
this.c2 = undefined;
cc2.update();
}
if (this.c3) {
var cc3 = this.c3;
this.c3 = undefined;
cc3.update();
}
if (this.c4) {
var cc4 = this.c4;
this.c4 = undefined;
cc4.update();
}
if (this.c5) {
var cc5 = this.c5;
this.c5 = undefined;
cc5.update();
}
if (this.callers && this.callers.length > 0) {
var calls = this.callers.slice();
this.callers = [];
for (var i = 0; i < calls.length; i++) {
calls[i].update();
}
}
}
};
ClassPropertyObserver.prototype.getter = function () {
return this.value;
};
return ClassPropertyObserver;
}());
exports.ClassPropertyObserver = ClassPropertyObserver;
//# sourceMappingURL=classPropertyObserver.js.map