tempus-api-graphql
Version:
A GraphQL wrapper for the tempus api (tempus2.xyz)
159 lines (124 loc) • 6.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _lruCache = _interopRequireDefault(require("lru-cache"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
var CachedByKeyResource = /*#__PURE__*/function () {
function CachedByKeyResource(loader) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
_classCallCheck(this, CachedByKeyResource);
var timeout = options.timeout || 60;
var max = options.max || 100;
this.loader = loader;
this.keyFunc = options.keyFunc || function (x) {
return JSON.stringify(x);
}; // insired by https://spin.atomicobject.com/2018/09/10/javascript-concurrency/
// My knowledge of JS's runtime is not good enough to fully vet this, but seems reasonable ish
// Also I don't really need concurrency guarantees for this project anyway
this.inFlight = {};
this.cache = new _lruCache["default"]({
max: max,
maxAge: timeout * 1000
});
}
_createClass(CachedByKeyResource, [{
key: "set",
value: function set(item, value) {
this.cache.set(this.keyFunc(item), value);
}
}, {
key: "load",
value: function () {
var _load = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(item) {
var _this = this;
var key, inFlight;
return regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
if (!(item === null)) {
_context3.next = 2;
break;
}
return _context3.abrupt("return", null);
case 2:
key = this.keyFunc(item);
inFlight = this.inFlight;
if (!inFlight[key]) {
inFlight[key] = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
var cached, r;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.prev = 0;
cached = _this.cache.get(key);
if (!cached) {
_context2.next = 6;
break;
}
_context2.next = 5;
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
return _context.abrupt("return", cached);
case 1:
case "end":
return _context.stop();
}
}
}, _callee);
}))();
case 5:
return _context2.abrupt("return", _context2.sent);
case 6:
_context2.next = 8;
return _this.loader(item);
case 8:
r = _context2.sent;
_this.cache.set(key, r);
return _context2.abrupt("return", r);
case 11:
_context2.prev = 11;
delete inFlight[key];
return _context2.finish(11);
case 14:
case "end":
return _context2.stop();
}
}
}, _callee2, null, [[0,, 11, 14]]);
}))();
}
return _context3.abrupt("return", inFlight[key]);
case 6:
case "end":
return _context3.stop();
}
}
}, _callee3, this);
}));
function load(_x) {
return _load.apply(this, arguments);
}
return load;
}()
}, {
key: "clearCache",
value: function clearCache() {
this.cache.reset();
}
}]);
return CachedByKeyResource;
}();
var _default = CachedByKeyResource;
exports["default"] = _default;