UNPKG

mysql-live-client

Version:
122 lines (121 loc) 4.5 kB
var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; var BaseCollection_1 = require("./BaseCollection"); var Helper_1 = require("./Helper"); var ObservableCollection = (function (_super) { __extends(ObservableCollection, _super); function ObservableCollection(name) { _super.call(this, name); this.listeners = []; this.simultaneouslyListeners = []; this.hasReceived = false; this.registerOnceCbIds = []; this.tasks = []; } ObservableCollection.getRandomEventId = function () { return ObservableCollection.EVENT_ID_START + Math.random(); }; ObservableCollection.prototype.addTask = function (task) { this.tasks.push(task); }; Object.defineProperty(ObservableCollection.prototype, "hasTasks", { get: function () { return this.tasks.length > 0; }, enumerable: true, configurable: true }); ObservableCollection.prototype.onChange = function (cb, cbId) { if (cbId !== undefined) { if (this.registerOnceCbIds.indexOf(cbId) >= 0) { return; } else { this.registerOnceCbIds.push(cbId); } } if (Helper_1.default.hasNextFnInside(cb)) { this.listeners.push(cb); } else { this.simultaneouslyListeners.push(cb); } }; ObservableCollection.prototype.on = function (eventName, cb, cbId) { this.onChange(function (evt, next, loop) { if (evt.name === eventName || eventName === ObservableCollection.ON_CHANGE) { cb(evt, next, loop); } else { next(); } }); }; ObservableCollection.prototype.removeOnChange = function () { this.listeners = []; }; ObservableCollection.prototype.fireChange = function (event) { var _this = this; if (!this.hasTasks || (event.name !== ObservableCollection.ON_INSERT && event.name !== ObservableCollection.ON_RECEIVE)) { this.fireChangeNow(event); } else { this.runTasks(event, function () { _this.fireChangeNow(event); }); } }; ObservableCollection.prototype.runTasks = function (event, finish, index) { var _this = this; if (index === void 0) { index = 0; } var caller = { collectionName: this.name, event: event }; var _next = function () { var nextTask = _this.tasks.length <= (index + 1) ? false : true; if (nextTask) { index = index + 1; _this.runTasks(event, finish, index); } else { index = 0; if (finish) finish(); } }; this.tasks[index].run(caller, _next); }; ObservableCollection.prototype.fireChangeNow = function (evt, listenerIndex) { var _this = this; if (listenerIndex === void 0) { listenerIndex = 0; } if (this.listeners.length > 0) { var index = listenerIndex; var _next = function () { index++; var nextListener = _this.listeners.length <= (index) ? undefined : _this.listeners[index]; if (nextListener) { nextListener(evt, _next, _this.loop); } else { index = listenerIndex; } }; this.listeners[listenerIndex].call(this, evt, _next, this.loop); } this.simultaneouslyListeners.forEach(function (cb) { cb(evt, _this.loop); }); }; ObservableCollection.prototype.destroy = function () { this.items = []; this.listeners = []; }; ObservableCollection.ON_RECEIVE = 'receive'; ObservableCollection.ON_INSERT = 'insert'; ObservableCollection.ON_UPDATE = 'update'; ObservableCollection.ON_REMOVE = 'remove'; ObservableCollection.ON_CHANGE = 'change'; ObservableCollection.EVENT_ID_START = "ONCHANGE_"; return ObservableCollection; })(BaseCollection_1.default); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = ObservableCollection;