UNPKG

@knora/api

Version:

JavaScript library that handles API requests to Knora

98 lines 4.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var rxjs_1 = require("rxjs"); var operators_1 = require("rxjs/operators"); /** * Generic cache class. * Fetches information of a specific type from Knora once and caches it. * Fetches also dependencies of a requested element (non-blocking). * * Works also with multiple async requests for the same key, also if not cached yet. */ var GenericCache = /** @class */ (function () { function GenericCache() { /** * Cache object: key -> value. */ this.cache = {}; } // TODO: check size of cache, delete oldest entries /** * Gets a specific item from the cache. * If not cached yet, the information will be fetched from Knora. * * @param key the id of the item to be returned. * @param isDependency true if the item to be returned * is a dependency of another item (recursive call to this method). * @return the requested item. */ GenericCache.prototype.getItem = function (key, isDependency) { // console.log("getItem", key, this.cache[key]); var _this = this; if (isDependency === void 0) { isDependency = false; } // If the key already exists, // return the associated AsyncSubject. if (this.cache[key] !== undefined) { return this.cache[key]; } // Item for `key` does not exist yet in cache. // Create an entry for a new AsyncSubject this.cache[key] = new rxjs_1.AsyncSubject(); // Requests information from Knora and updates the AsyncSubject // once the information is available // // take(1) ensures that the subscription is terminated // when the first value was emitted. this.requestItemFromKnora(key, isDependency).pipe(operators_1.take(1)).subscribe(function (items) { // console.log("fetching from Knora", key); if (items.length === 0) throw Error("No items returned from Knora for " + key); if (key !== _this.getKeyOfItem(items[0])) throw Error("First item of items returned from Knora is expected to be " + key); // Updates and completes the AsyncSubject for `key`. _this.cache[key].next(items[0]); _this.cache[key].complete(); // Write all available items to the cache (only for non existing keys) // Analyze dependencies of available items. items.forEach(function (item) { // Get key of item var itemKey = _this.getKeyOfItem(item); // Only write an additional item to the cache // if there is not entry for it yet // item for `key` has already been handled if (_this.cache[itemKey] === undefined) { _this.cache[itemKey] = new rxjs_1.AsyncSubject(); _this.cache[itemKey].next(item); _this.cache[itemKey].complete(); } // get items this item depends on _this.getDependenciesOfItem(item) .filter(function (depKey) { // ignore dependencies already taken care of return Object.keys(_this.cache).indexOf(depKey) === -1; }) .forEach(function (depKey) { // Request each dependency from the cache // Dependencies will be fetched asynchronously. _this.getItem(depKey, true); }); }); }); // return the AsyncSubject for `key` (will be updated once the information is available) return this.cache[key]; }; /** * Deletes an existing entry in the cache and requests information from Knora. * * @param key the id of the information to be returned. * @return the item. */ GenericCache.prototype.reloadItem = function (key) { if (this.cache[key] !== undefined) delete this.cache[key]; return this.getItem(key); }; return GenericCache; }()); exports.GenericCache = GenericCache; //# sourceMappingURL=GenericCache.js.map