UNPKG

mframejs

Version:
138 lines 4.74 kB
const events = ['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift', 'sort']; events.forEach((eventX) => { const eventType = Array.prototype[eventX]; Array.prototype[eventX] = function () { const result = eventType.apply(this, arguments); if (this.__array_observer__class) { this.__array_observer__class.check(arguments, eventX); } return result; }; }); export class ClassArrayObserver { constructor(_class, key) { this._class = _class; this.key = key; this.internalEvents = []; this.timer = null; if (typeof this._class === 'object') { this.observe(); } } subscribe(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); } } } } } } } } unsubscribe(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); } } check(args, event) { clearTimeout(this.timer); this.timer = 0; this.internalEvents.push({ event: event, args: args }); this.timer = setTimeout(() => { if (this.c1) { const cc1 = this.c1; this.c1 = undefined; cc1.update(this.internalEvents); } if (this.c2) { const cc2 = this.c2; this.c2 = undefined; cc2.update(this.internalEvents); } if (this.c3) { const cc3 = this.c3; this.c3 = undefined; cc3.update(this.internalEvents); } if (this.c4) { const cc4 = this.c4; this.c4 = undefined; cc4.update(this.internalEvents); } if (this.c5) { const cc5 = this.c5; this.c5 = undefined; cc5.update(this.internalEvents); } if (this.callers && this.callers.length > 0) { const calls = this.callers.slice(); this.callers = []; calls.forEach((call) => { call.update(this.internalEvents); }); } this.internalEvents = null; this.internalEvents = []; if (this._class[this.key] && this._class[this.key].__observer) { for (const 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); } observe() { 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 }); } } } //# sourceMappingURL=classArrayObserver.js.map