UNPKG

semantic-network

Version:

A utility library for manipulating a list of links that form a semantic interface to a network of resources.

101 lines 4.12 kB
"use strict"; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.uriMappingResolver = void 0; var anylogger_1 = __importDefault(require("anylogger")); var log = (0, anylogger_1.default)('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`) * */ var UriMappingResolver = /** @class */ (function () { function UriMappingResolver() { /** * 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. */ UriMappingResolver.prototype.update = function (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. */ UriMappingResolver.prototype.add = function (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. */ UriMappingResolver.prototype.remove = function (documentUri) { var e_1, _a; try { for (var _b = __values(this.resolutions.entries()), _c = _b.next(); !_c.done; _c = _b.next()) { var entry = _c.value; if (entry[1] === documentUri) { log.debug('resolver: remove \'%s\'', entry[0]); this.resolutions.delete(entry[0]); } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_1) throw e_1.error; } } }; /** * Returns the network of data (NOD) URI based on a document URI or if not found itself */ UriMappingResolver.prototype.resolve = function (documentUri) { if (this.resolutions.has(documentUri)) { var 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 */ UriMappingResolver.prototype.out = function () { return JSON.stringify(this.resolutions.entries()); }; return UriMappingResolver; }()); exports.uriMappingResolver = new UriMappingResolver(); //# sourceMappingURL=uriMappingResolver.js.map