UNPKG

podchat-browser

Version:

Javascript SDK to use POD's Chat Service - Browser Only

304 lines (255 loc) 10.7 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.HistoryItem = HistoryItem; var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); /** * * @param {number} threadId * @param {Object} params * @param {Array} list * @returns {Object} * @constructor */ function HistoryItem(threadId, params, list) { var config = { threadId: threadId, list: list, hasMore: params.count <= list.length, hasNext: true }; return { threadId: threadId, get: function get() { return config.list; }, findMessageIdByFromTime: function findMessageIdByFromTime(time) { var message = config.list.filter(function (item) { return item.timeMiliSeconds === time; }); if (message.length === 0) { return 0; } return message[0].id || 0; }, findMessagePreviousIdByToTime: function findMessagePreviousIdByToTime(time) { var message = config.list.filter(function (item) { return item.timeMiliSeconds === time; }); if (message.length === 0) { return 0; } return message[0].previousId || 0; }, hasNextMessage: function hasNextMessage() {}, hasNoPrevious: function hasNoPrevious(cacheData) { if (cacheData.length > 0) { var _cacheData; return ((_cacheData = cacheData[cacheData.length - 1]) === null || _cacheData === void 0 ? void 0 : _cacheData.previousId) == undefined; } return true; }, isCacheValid: function isCacheValid(cachedData, count, previousId) { return cachedData.length === count || this.hasNoPrevious(cachedData); }, matchesPreviousId: function matchesPreviousId(cachedData, previousId) { var _cachedData$; return ((_cachedData$ = cachedData[0]) === null || _cachedData$ === void 0 ? void 0 : _cachedData$.previousId) === previousId; }, checkSequence: function checkSequence(cachedData) { for (var i = 1; i < cachedData.length; i++) { if (cachedData[i].id !== cachedData[i - 1].previousId) { return false; } } return true; }, getRangeCacheData: function getRangeCacheData(params) { var resCachedData = []; var cacheData = (0, _toConsumableArray2["default"])(config.list); var size = params.size, messageId = params.messageId; if (size && messageId) { var message = config.list.find(function (item) { return item.id === messageId; }); if (message.timeMiliSeconds) { var fromTimeIndex = cacheData.findIndex(function (x) { return x.timeMiliSeconds === message.timeMiliSeconds; }); if (fromTimeIndex >= size) { resCachedData = cacheData.slice(fromTimeIndex - size, fromTimeIndex + size + 1); if (!this.checkSequence(resCachedData)) { return []; } } } } return resCachedData; }, getCacheData: function getCacheData(params) { var lastMessageTime = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; var count = params.count, fromTime = params.fromTime, toTime = params.toTime, order = params.order; var cacheData = (0, _toConsumableArray2["default"])(config.list); var resCachedData = []; if (fromTime) { var _cacheData$fromTimeIn, _resCachedData; var fromTimeIndex = cacheData.findIndex(function (x) { return x.timeMiliSeconds === fromTime; }); var start = fromTimeIndex >= count ? fromTimeIndex - count : 0; resCachedData = cacheData.slice(start, fromTimeIndex); if (((_cacheData$fromTimeIn = cacheData[fromTimeIndex]) === null || _cacheData$fromTimeIn === void 0 ? void 0 : _cacheData$fromTimeIn.id) !== ((_resCachedData = resCachedData[resCachedData.length - 1]) === null || _resCachedData === void 0 ? void 0 : _resCachedData.previousId)) { if (!(lastMessageTime === fromTime && count === resCachedData.length)) { return []; } } if (!this.checkSequence(resCachedData)) { return []; } return resCachedData; } else if (toTime) { var toTimeIndex = cacheData.findIndex(function (x) { return x.timeMiliSeconds === toTime; }); if (toTimeIndex > -1) { var _cacheData2; resCachedData = cacheData.slice(toTimeIndex, toTimeIndex + count); if (((_cacheData2 = cacheData[toTimeIndex - 1]) === null || _cacheData2 === void 0 ? void 0 : _cacheData2.previousId) !== resCachedData[0].id) { if (!(lastMessageTime === toTime && count === resCachedData.length)) { return []; } } if (!this.checkSequence(resCachedData)) { return []; } } } else { resCachedData = config.list.slice(0, count); if (!this.checkSequence(resCachedData)) { return []; } } if (order && order === 'desc') { return Array.from(resCachedData).reverse(); } return resCachedData; }, cacheHasEnoughData: function cacheHasEnoughData(params, lastMessageItem) { // handle range history cache if (params.size && params.messageId) { return this.getRangeCacheData(params).length === params.size * 2 + 1; } var lastMessageId = lastMessageItem.id, timeMiliSeconds = lastMessageItem.timeMiliSeconds; var cacheData = this.getCacheData(params, timeMiliSeconds); var oneOrLessThanCountItemCondition = cacheData.length > 0 && cacheData.length < params.count; if (oneOrLessThanCountItemCondition) { var _cacheData$index2, _cacheData3, _cacheData$2; var index = params.order === 'desc' ? cacheData.length - 1 : 0; // prevent request when scroll top. (toTime) if (params !== null && params !== void 0 && params.toTime) { var _cacheData$index, _cacheData$; return [(_cacheData$index = cacheData[index]) === null || _cacheData$index === void 0 ? void 0 : _cacheData$index.previousId, (_cacheData$ = cacheData[0]) === null || _cacheData$ === void 0 ? void 0 : _cacheData$.previousId].includes(undefined); } // handle request params.fromTime if (params !== null && params !== void 0 && params.fromTime) { return cacheData[index].id === lastMessageId; } // if threads history items less than count value and request with count and offset parameter (not fromTime or To Time) if (((_cacheData$index2 = cacheData[index]) === null || _cacheData$index2 === void 0 ? void 0 : _cacheData$index2.id) === lastMessageId && (((_cacheData3 = cacheData[cacheData.length - 1]) === null || _cacheData3 === void 0 ? void 0 : _cacheData3.previousId) == undefined || ((_cacheData$2 = cacheData[0]) === null || _cacheData$2 === void 0 ? void 0 : _cacheData$2.previousId) == undefined)) { return true; } // if receive event with number 2 code from server when client is offline and data is less than count and fromTime and endTime not exit => data is invalid return false; } // check has more data if fromTime parameter exist on request and cacheData is empty if (cacheData.length === 0 && (params === null || params === void 0 ? void 0 : params.fromTime) === timeMiliSeconds && timeMiliSeconds !== undefined) { return true; } return cacheData.length == params.count; }, pushAtEnd: function pushAtEnd(message) { config.list.push(message); }, mergeArray: function mergeArray(messages) { config.list = config.list.concat(messages).filter(function (item, index, self) { return index === self.findIndex(function (t) { return t.id === item.id; }); }); }, sortDesc: function sortDesc() { config.list.sort(function (a, b) { return b.time - a.time; }); }, putAtBegin: function putAtBegin(message) { config.list.unshift(message); }, binaryInsert: function binaryInsert(message) { var left = 0; var right = config.list.length; // جستجوی دودویی برای پیدا کردن مکان مناسب while (left < right) { var mid = Math.floor((left + right) / 2); if (message.time > config.list[mid].time) { right = mid; } else { left = mid + 1; } } // درج شیء در مکان پیدا شده config.list.splice(left, 0, message); }, updateOrPushEndMessage: function updateOrPushEndMessage(message) { var foundIndex = config.list.findIndex(function (item) { return item.id == message.id; }); if (foundIndex > -1) config.list[foundIndex] = message;else { var indexScrollDown = config.list.findIndex(function (item) { return item.id === message.previousId; }); var indexScrollUp = config.list.findIndex(function (item) { return item.previousId === message.id; }); if (indexScrollDown > -1) { config.list.splice(indexScrollDown, 0, message); } else { // config.list.push(null) config.list.push(message); } // config.list.push(message) } }, updateMessage: function updateMessage(message) { var foundIndex = config.list.findIndex(function (item) { return item.id == message.id; }); if (foundIndex > -1) config.list[foundIndex] = message; }, hasMore: function hasMore() { return config.hasMore; }, messageExists: function messageExists(messageId) { return config.list.findIndex(function (item) { return item.id == messageId; }) > -1; }, remove: function remove(messageId) { config.list = config.list.filter(function (item) { return !item || item && item.id != messageId; }); }, removeReplyInfoVO: function removeReplyInfoVO(messageId) { for (var i in config.list) { if (config.list[i].id == messageId) { config.list[i].replyInfo && (config.list[i].replyInfo.deleted = true); } } }, updateSeenItem: function updateSeenItem(messageId) { var foundIndex = config.list.findIndex(function (item) { return item.id == messageId; }); if (foundIndex > -1) config.list[foundIndex]['seen'] = true; } }; }