UNPKG

teambition-sdk-socket

Version:
131 lines 4.89 kB
'use strict'; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); require("rxjs/add/operator/switch"); require("rxjs/add/operator/publishReplay"); var Observable_1 = require("rxjs/Observable"); var BaseModel_1 = require("./BaseModel"); var index_1 = require("../utils/index"); var BaseCollection = (function (_super) { __extends(BaseCollection, _super); function BaseCollection(_schemaName, _condition, _dbIndex, _pageLength, _unionFlag) { if (_pageLength === void 0) { _pageLength = 30; } if (_unionFlag === void 0) { _unionFlag = '_id'; } var _this = _super.call(this) || this; _this._schemaName = _schemaName; _this._condition = _condition; _this._dbIndex = _dbIndex; _this._pageLength = _pageLength; _this._unionFlag = _unionFlag; _this._singals = new Map(); _this._data = new Map(); _this._pages = []; return _this; } BaseCollection.prototype.hasPage = function (page) { return this._pages.indexOf(page) !== -1; }; BaseCollection.prototype.addPage = function (page, data) { var _this = this; if (!data) { return Observable_1.Observable.of(null); } return Observable_1.Observable.create(function (observer) { var destSignal; if (_this.hasPage(page)) { if (page === 1) { destSignal = _this._singals.get(page); } else { destSignal = _this.get(page); } } else { _this._data.set(page, data); if (!_this._pages.length) { _this._pages.push(page); } else { index_1.forEach(_this._pages, function (_page, pos) { var nextPage = _this._pages[pos + 1]; if (!nextPage) { if (_page < page) { _this._pages.push(page); } else { _this._pages.unshift(page); } return false; } else if (page > _page && page < nextPage) { _this._pages.splice(pos, 0, page); return false; } return true; }); } var result = _this._getAll(); /** * page 默认从1开始 */ if (page === 1) { // 可发射多次的流 destSignal = _this._saveCollection(_this._dbIndex, result, _this._schemaName, _this._condition, _this._unionFlag) .publishReplay(1) .refCount(); } else { // 只会发射一次 destSignal = _this._updateCollection(_this._dbIndex, result) .publishReplay(1) .refCount(); } } _this._singals.set(page, destSignal); observer.next(destSignal); }) ._switch(); }; BaseCollection.prototype.get = function (page) { if (page) { if (this.hasPage(page)) { if (page !== 1) { var getSignal = this._singals.get(page); if (getSignal) { return getSignal; } else { return null; } } else { return this._get(this._dbIndex); } } } else { return this._get(this._dbIndex); } return null; }; BaseCollection.prototype._getAll = function () { var _this = this; var result = []; index_1.forEach(this._pages, function (page) { index_1.concat(result, _this._data.get(page)); }); return result; }; return BaseCollection; }(BaseModel_1.default)); exports.default = BaseCollection; //# sourceMappingURL=BaseCollection.js.map