@dasch-swiss/dsp-js
Version:
JavaScript library that handles API requests to Knora
66 lines (65 loc) • 2.36 kB
TypeScript
import { Observable } from "rxjs";
import { ApiResponseError } from "../models/api-response-error";
/**
* 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.
*
* @category Internal
*/
export declare abstract class GenericCache<T> {
/**
* Cache object: key -> value.
*/
private cache;
/**
* Gets a specific item from the cache.
* If not cached yet, the information will be retrieved from DSP-API.
*
* @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).
*/
protected getItem(key: string, isDependency?: boolean): Observable<T>;
/**
* Deletes an existing entry in the cache and requests information from DSP-API.
*
* @param key the id of the information to be returned.
* @return the item.
*/
protected reloadItem(key: string): Observable<T>;
/**
* Fetches information from DSP-API.
*
* @param key the id of the information to be returned.
* @param isDependency true if the requested key is a dependency of another item.
* @return the items received from DSP-API.
*/
protected abstract requestItemFromKnora(key: string, isDependency: boolean): Observable<T[] | ApiResponseError>;
/**
* Given an item, determines its key.
*
* @param item The item whose key has to be determined.
*/
protected abstract getKeyOfItem(item: T): string;
/**
* Given an item, determines its dependencies on other items.
*
* @param item the item whose dependencies have to be determined.
* @return keys of the items the current item relies on.
*/
protected abstract getDependenciesOfItem(item: T): string[];
/**
* Handle additional items that were resolved with a request.
*
* @param items dependencies that have been retrieved.
*/
private saveAdditionalItems;
/**
* Requests dependencies of the items retrieved from DSP-API.
*
* @param items items returned from DSP-API to a request.
*/
private requestDependencies;
}