mframejs
Version:
simple framework
142 lines • 5.15 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
var events = ['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift', 'sort'];
events.forEach(function (eventX) {
var eventType = Array.prototype[eventX];
Array.prototype[eventX] = function () {
var result = eventType.apply(this, arguments);
if (this.__array_observer__class) {
this.__array_observer__class.check(arguments, eventX);
}
return result;
};
});
var ClassArrayObserver = (function () {
function ClassArrayObserver(_class, key) {
this._class = _class;
this.key = key;
this.internalEvents = [];
this.timer = null;
if (typeof this._class === 'object') {
this.observe();
}
}
ClassArrayObserver.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);
}
}
}
}
}
}
}
};
ClassArrayObserver.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);
}
};
ClassArrayObserver.prototype.check = function (args, event) {
var _this = this;
clearTimeout(this.timer);
this.timer = 0;
this.internalEvents.push({
event: event,
args: args
});
this.timer = setTimeout(function () {
if (_this.c1) {
var cc1 = _this.c1;
_this.c1 = undefined;
cc1.update(_this.internalEvents);
}
if (_this.c2) {
var cc2 = _this.c2;
_this.c2 = undefined;
cc2.update(_this.internalEvents);
}
if (_this.c3) {
var cc3 = _this.c3;
_this.c3 = undefined;
cc3.update(_this.internalEvents);
}
if (_this.c4) {
var cc4 = _this.c4;
_this.c4 = undefined;
cc4.update(_this.internalEvents);
}
if (_this.c5) {
var cc5 = _this.c5;
_this.c5 = undefined;
cc5.update(_this.internalEvents);
}
if (_this.callers && _this.callers.length > 0) {
var calls = _this.callers.slice();
_this.callers = [];
calls.forEach(function (call) {
call.update(_this.internalEvents);
});
}
_this.internalEvents = null;
_this.internalEvents = [];
if (_this._class[_this.key] && _this._class[_this.key].__observer) {
for (var k in _this._class[_this.key].__observer) {
if (_this._class[_this.key].__observer[k] && _this._class[_this.key].__observer[k].forceUpdate) {
_this._class[_this.key].__observer[k].forceUpdate();
}
}
}
}, 0);
};
ClassArrayObserver.prototype.observe = function () {
if (this._class[this.key] && Array.isArray(this._class[this.key])) {
Object.defineProperty(this._class[this.key], '__array_observer__class', {
writable: true,
configurable: true,
value: this
});
}
};
return ClassArrayObserver;
}());
exports.ClassArrayObserver = ClassArrayObserver;
//# sourceMappingURL=classArrayObserver.js.map