UNPKG

@shipengine/connect-loader

Version:

Internal library for loading ShipEngine Connect apps

50 lines 1.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fileCache = void 0; /** * Caches parsed file contents so the same file doesn't get read and parsed multiple times. * This also ensures that multiple references to the same file resolve to the same parsed obect instance. */ class FileCache { _cache = new Map(); _loadsInProgress = 0; /** * Adds the given value to the cache */ async add(filePath, promise) { if (this._cache.has(filePath)) { throw new ReferenceError(`Cache write error: ${filePath}`); } this._cache.set(filePath, promise); return promise; } /** * Gets the specified value from the cache, if it exists */ get(filePath) { return this._cache.get(filePath); } /** * Lets the cache know that a ShipEngine Connect app is currently being loaded. * Be sure to call `finishedLoading()` afterward so the cache can be cleared to free-up memory. */ startedLoading() { this._loadsInProgress++; } /** * Lets the cache know that a ShipEngine Connect app has finished loading. * The cache will be cleared to free-up memory. */ finishedLoading() { this._loadsInProgress--; if (this._loadsInProgress <= 0) { this._loadsInProgress = 0; this._cache.clear(); } } } /** * The singleton instance of the FileCache */ exports.fileCache = new FileCache(); //# sourceMappingURL=file-cache.js.map