UNPKG

data-provider-temporary

Version:

Library that helps with server-to-client synchronization of data

145 lines (121 loc) 4.37 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _lodash = require('lodash'); var _lodash2 = _interopRequireDefault(_lodash); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var DataProvider = function () { function DataProvider(_ref) { var id = _ref.id, ref = _ref.ref, rawOnData = _ref.rawOnData, onData = _ref.onData, initialData = _ref.initialData; _classCallCheck(this, DataProvider); this.id = id; this.ref = ref; this.rawOnData = rawOnData; this.onData = onData; this.userConfigs = {}; this.loaded = false; if (initialData !== undefined) { this.loaded = true; this.onData(initialData); } } _createClass(DataProvider, [{ key: 'updateUser', value: function updateUser(userId, _ref2) { var _ref2$polling = _ref2.polling, polling = _ref2$polling === undefined ? Infinity : _ref2$polling, _ref2$needed = _ref2.needed, needed = _ref2$needed === undefined ? true : _ref2$needed, rawGetData = _ref2.rawGetData, getData = _ref2.getData; var needFetch = false; if (rawGetData != null && !_lodash2.default.isEqual(rawGetData, this.rawGetData)) { needFetch = true; this.rawGetData = rawGetData; this.getData = getData; this.loaded = false; } var oldPolling = this.polling(); this.userConfigs[userId] = { polling: polling, needed: needed }; if (this.polling() < oldPolling) { needFetch = true; } if (!this.loaded && !this.timer) { needFetch = true; } if (needFetch) { this.fetch(); } } }, { key: 'removeUser', value: function removeUser(userId) { delete this.userConfigs[userId]; } }, { key: 'polling', value: function polling() { return _lodash2.default.min([].concat(_toConsumableArray(_lodash2.default.values(this.userConfigs).map(function (_ref3) { var polling = _ref3.polling; return polling; })), [Infinity])); } }, { key: 'needed', value: function needed() { return _lodash2.default.reduce(_lodash2.default.values(this.userConfigs).map(function (_ref4) { var needed = _ref4.needed; return needed; }), function (v1, v2) { return v1 || v2; }, false); } }, { key: 'canceled', value: function canceled() { return _lodash2.default.isEmpty(this.userConfigs); } }, { key: 'scheduleNextFetch', value: function scheduleNextFetch() { var _this = this; if (this.polling() === Infinity) { return; } this.timer = setTimeout(function () { _this.fetch(); }, this.polling()); } }, { key: 'fetch', value: function fetch() { var _this2 = this; if (this.canceled()) { return null; } if (this.timer) { clearTimeout(this.timer); } return this.getData().then(function (data) { if (!_this2.canceled()) { _this2.loaded = true; _this2.onData(data); } }).catch(function (e) { console.error(e); // eslint-disable-line no-console }).then(function () { return _this2.scheduleNextFetch(); }); } }]); return DataProvider; }(); exports.default = DataProvider;