semantic-network
Version:
A utility library for manipulating a list of links that form a semantic interface to a network of resources.
72 lines • 2.71 kB
JavaScript
import anylogger from 'anylogger';
const log = anylogger('UriMappingResolver');
/**
* A resolving cache of key/value mapping to/from a network of data URI to/from a document URI.
*
* We use this to resolve transitive references in a network of data when creating, updating and deleting.
* For example, we are cloning a resource that is linked to another resource. In the new resource, it is not
* linked to the original other resource but to the equivalent other. We need to be to map these going forward.
*
* @example
*
* Document: A exists, B references A
* NOD: Create A` (hold reference A` through A), Create B` (but now B` references A, replace A with A`)
*
*/
class UriMappingResolver {
constructor() {
/**
* A simple 'document' uri map to a 'network of data' URI map.
*/
this.resolutions = new Map();
}
/**
* Update a mapping to/from a network of data (NOD) URI to/from a document URI.
*/
update(documentUri, resolvedUri) {
log.debug('resolver: update \'%s\' --> \'%s\'', documentUri, resolvedUri);
this.resolutions.set(documentUri, resolvedUri);
}
/**
* Add a mapping to/from a network of data (NOD) URI to/from a document URI.
*/
add(documentUri, resolvedUri) {
log.debug('resolver: add \'%s\' --> \'%s\'', documentUri, resolvedUri);
this.resolutions.set(documentUri, resolvedUri);
}
/**
* Signal to the resolver that a mapping is no longer relevant.
* Remove based on the document URI a mapping to/from a network of data (NOD) URI to/from a document URI.
*/
remove(documentUri) {
for (const entry of this.resolutions.entries()) {
if (entry[1] === documentUri) {
log.debug('resolver: remove \'%s\'', entry[0]);
this.resolutions.delete(entry[0]);
}
}
}
/**
* Returns the network of data (NOD) URI based on a document URI or if not found itself
*/
resolve(documentUri) {
if (this.resolutions.has(documentUri)) {
const resolved = this.resolutions.get(documentUri);
if (resolved) {
log.debug('resolved \'%s\' --> \'%s\'', documentUri, resolved);
return resolved;
}
}
log.debug('resolved not found \'%s\'', documentUri);
return documentUri;
}
/**
* Helper to print out the resolutions map
* @returns stringified JSON version of the resolutions map
*/
out() {
return JSON.stringify(this.resolutions.entries());
}
}
export const uriMappingResolver = new UriMappingResolver();
//# sourceMappingURL=uriMappingResolver.js.map