@studyportals/sp-hs-misc
Version:
Miscellaneous code used in HouseStark's projects
201 lines (181 loc) • 7.54 kB
JavaScript
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; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {
function adopt(value) {
return value instanceof P ? value : new P(function (resolve) {
resolve(value);
});
}
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
}
function rejected(value) {
try {
step(generator["throw"](value));
} catch (e) {
reject(e);
}
}
function step(result) {
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
}
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
//# sourceMappingURL=eager-in-memory-cache.class.js.map
var EagerInMemoryCache = function () {
_createClass(EagerInMemoryCache, [{
key: "cache",
get: function get() {
return this._cache;
}
}, {
key: "promiseToBuildTheCache",
get: function get() {
return this._promiseToBuildTheCache;
}
}]);
function EagerInMemoryCache() {
_classCallCheck(this, EagerInMemoryCache);
this._cache = null;
this._promiseToBuildTheCache = null;
}
_createClass(EagerInMemoryCache, [{
key: "get",
value: function get(key) {
return __awaiter(this, void 0, void 0, /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
var cache;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return this.obtainCache();
case 2:
cache = _context.sent;
if (!cache.has(key)) {
_context.next = 5;
break;
}
return _context.abrupt("return", cache.get(key));
case 5:
throw new Error("A cache entry does not exist for the specified key: " + key);
case 6:
case "end":
return _context.stop();
}
}
}, _callee, this);
}));
}
}, {
key: "obtainCache",
value: function obtainCache() {
return __awaiter(this, void 0, void 0, /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
if (!this.isCacheBuilt()) {
_context2.next = 2;
break;
}
return _context2.abrupt("return", this.cache);
case 2:
return _context2.abrupt("return", this.acquireCache());
case 3:
case "end":
return _context2.stop();
}
}
}, _callee2, this);
}));
}
}, {
key: "isCacheBuilt",
value: function isCacheBuilt() {
return null != this.cache;
}
}, {
key: "acquireCache",
value: function acquireCache() {
if (this.isCacheBeingBuilt()) {
return this.promiseToBuildTheCache;
}
return this._promiseToBuildTheCache = this.buildCache();
}
}, {
key: "isCacheBeingBuilt",
value: function isCacheBeingBuilt() {
return null != this.promiseToBuildTheCache;
}
}, {
key: "buildCache",
value: function buildCache() {
return __awaiter(this, void 0, void 0, /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
var data, cache;
return regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
_context3.next = 2;
return this.retrieveData();
case 2:
data = _context3.sent;
cache = this.createCacheFromData(data);
return _context3.abrupt("return", this._cache = cache);
case 5:
case "end":
return _context3.stop();
}
}
}, _callee3, this);
}));
}
}, {
key: "retrieveData",
value: function retrieveData() {
throw new Error("Not implemented");
}
}, {
key: "createCacheFromData",
value: function createCacheFromData(data) {
var cache = new Map();
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = data[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var dataItem = _step.value;
var key = this.extractKey(dataItem);
cache.set(key, dataItem);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return cache;
}
}, {
key: "extractKey",
value: function extractKey(dataItem) {
throw new Error("Not implemented");
}
}]);
return EagerInMemoryCache;
}();