UNPKG

kepler.gl

Version:

kepler.gl is a webgl based application to visualize large scale location data in the browser

120 lines (119 loc) 13.3 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.getRequestThrottle = getRequestThrottle; var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _utils = require("@kepler.gl/utils"); // SPDX-License-Identifier: MIT // Copyright contributors to the kepler.gl project var RequestThrottle = /*#__PURE__*/function () { function RequestThrottle() { (0, _classCallCheck2["default"])(this, RequestThrottle); (0, _defineProperty2["default"])(this, "serverQueues", void 0); (0, _defineProperty2["default"])(this, "maxConcurrentRequests", void 0); this.serverQueues = {}; this.maxConcurrentRequests = (0, _utils.getApplicationConfig)().rasterServerMaxPerServerRequests; } return (0, _createClass2["default"])(RequestThrottle, [{ key: "getServerQueue", value: function getServerQueue(serverKey) { if (!this.serverQueues[serverKey]) { this.serverQueues[serverKey] = { activeRequests: 0, queue: [] }; } return this.serverQueues[serverKey]; } }, { key: "getDebugInfo", value: function getDebugInfo() { var stats = Object.entries(this.serverQueues).map(function (_ref) { var _ref2 = (0, _slicedToArray2["default"])(_ref, 2), serverKey = _ref2[0], queue = _ref2[1]; return "Server: ".concat(serverKey, "\n Active Requests: ").concat(queue.activeRequests, "\n Queued Requests: ").concat(queue.queue.length); }); return stats.length > 0 ? "Request Throttle Stats:\n".concat(stats.join('\n')) : 'No active server queues'; } }, { key: "throttleRequest", value: function () { var _throttleRequest = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(serverKey, requestFunction) { var serverQueue, nextRequest; return _regenerator["default"].wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: serverQueue = this.getServerQueue(serverKey); if (!(serverQueue.activeRequests >= this.maxConcurrentRequests && Boolean(this.maxConcurrentRequests))) { _context2.next = 4; break; } _context2.next = 4; return new Promise(function (resolve) { serverQueue.queue.push( /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() { var result; return _regenerator["default"].wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: _context.prev = 0; _context.next = 3; return requestFunction(); case 3: result = _context.sent; resolve(); return _context.abrupt("return", result); case 8: _context.prev = 8; _context.t0 = _context["catch"](0); resolve(); return _context.abrupt("return", null); case 12: case "end": return _context.stop(); } }, _callee, null, [[0, 8]]); }))); }); case 4: serverQueue.activeRequests++; _context2.prev = 5; _context2.next = 8; return requestFunction(); case 8: return _context2.abrupt("return", _context2.sent); case 9: _context2.prev = 9; serverQueue.activeRequests--; // Process next request in queue if any nextRequest = serverQueue.queue.shift(); if (nextRequest) { nextRequest(); } return _context2.finish(9); case 14: case "end": return _context2.stop(); } }, _callee2, this, [[5,, 9, 14]]); })); function throttleRequest(_x, _x2) { return _throttleRequest.apply(this, arguments); } return throttleRequest; }() }]); }(); // Create a singleton instance var requestThrottle = null; function getRequestThrottle() { if (!requestThrottle) requestThrottle = new RequestThrottle(); return requestThrottle; } //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJfdXRpbHMiLCJyZXF1aXJlIiwiUmVxdWVzdFRocm90dGxlIiwiX2NsYXNzQ2FsbENoZWNrMiIsIl9kZWZpbmVQcm9wZXJ0eTIiLCJzZXJ2ZXJRdWV1ZXMiLCJtYXhDb25jdXJyZW50UmVxdWVzdHMiLCJnZXRBcHBsaWNhdGlvbkNvbmZpZyIsInJhc3RlclNlcnZlck1heFBlclNlcnZlclJlcX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