ng-packagr
Version:
Compile and package Angular libraries in Angular Package Format (APF)
73 lines • 2.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MemoryLoadResultCache = void 0;
exports.createCachedLoad = createCachedLoad;
const path_1 = require("../utils/path");
function createCachedLoad(cache, callback) {
if (cache === undefined) {
return callback;
}
return async (args) => {
const loadCacheKey = `${args.namespace}:${args.path}`;
let result = cache.get(loadCacheKey);
if (result === undefined) {
result = await callback(args);
// Do not cache null or undefined
if (result) {
// Ensure requested path is included if it was a resolved file
if (args.namespace === 'file') {
result.watchFiles ??= [];
result.watchFiles.push(args.path);
}
await cache.put(loadCacheKey, result);
}
}
return result;
};
}
class MemoryLoadResultCache {
#loadResults = new Map();
#fileDependencies = new Map();
get(path) {
return this.#loadResults.get(path);
}
async put(path, result) {
this.#loadResults.set(path, result);
if (result.watchFiles) {
for (const watchFile of result.watchFiles) {
// Normalize the watch file path to ensure OS consistent paths
const normalizedWatchFile = (0, path_1.ensureUnixPath)(watchFile);
let affected = this.#fileDependencies.get(normalizedWatchFile);
if (affected === undefined) {
affected = new Set();
this.#fileDependencies.set(normalizedWatchFile, affected);
}
affected.add(path);
}
}
}
invalidate(path) {
const affectedPaths = this.#fileDependencies.get((0, path_1.ensureUnixPath)(path));
let found = false;
if (affectedPaths) {
for (const affected of affectedPaths) {
if (this.#loadResults.delete(affected)) {
found = true;
}
}
this.#fileDependencies.delete(path);
}
return found;
}
get watchFiles() {
// this.#loadResults.keys() is not included here because the keys
// are namespaced request paths and not disk-based file paths.
return [...this.#fileDependencies.keys()];
}
clear() {
this.#loadResults.clear();
this.#fileDependencies.clear();
}
}
exports.MemoryLoadResultCache = MemoryLoadResultCache;
//# sourceMappingURL=load-result-cache.js.map