"use strict"; var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => { __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); return value; }; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const errors = require("./errors.cjs"); const createDebugger = require("./lib/createDebugger.cjs"); const pathToFilePath = require("./lib/pathToFilePath.cjs"); const toReadonlyMap = require("./lib/toReadonlyMap.cjs"); const debug = createDebugger.createDebugger("akte:files"); const debugRender = createDebugger.createDebugger("akte:files:render"); const debugCache = createDebugger.createDebugger("akte:files:cache"); class AkteFiles { constructor(definition) { __publicField(this, "definition"); __publicField(this, "_dataMapCache", /* @__PURE__ */ new Map()); /** * Retrieves data from files' definition `data` method with given context. * * @param context - Context to get data with. * @returns Retrieved data. * @remark Returned data may come from cache. * @experimental Programmatic API might still change not following SemVer. */ __publicField(this, "getData", (context) => { const maybePromise = this._dataMapCache.get(context.path); if (maybePromise) { debugCache("using cached data %o", context.path); return maybePromise; } debugCache("retrieving data... %o", context.path); let promise; if (this.definition.data) { promise = this.definition.data(context); } else if (this.definition.bulkData) { const dataFromBulkData = async (path) => { const bulkData = await this.getBulkData({ globalData: context.globalData }); if (path in bulkData) { return bulkData[path]; } throw new errors.NotFoundError(path); }; promise = dataFromBulkData(context.path); } else { throw new Error(`Cannot render file for path \`${context.path}\`, no \`data\` or \`bulkData\` function available`); } if (promise instanceof Promise) { promise.then(() => { debugCache("retrieved data %o", context.path); }).catch(() => { }); } else { debugCache("retrieved data %o", context.path); } this._dataMapCache.set(context.path, promise); return promise; }); __publicField(this, "_bulkDataCache"); /** * Retrieves data from files' definition `bulkData` method with given context. * * @param context - Context to get bulk data with. * @returns Retrieved bulk data. * @remark Returned bulk data may come from cache. * @experimental Programmatic API might still change not following SemVer. */ __publicField(this, "getBulkData", (context) => { var _a, _b; if (!this._bulkDataCache) { debugCache("retrieving bulk data... %o", this.path); const bulkDataPromise = ((_b = (_a = this.definition).bulkData) == null ? void 0 : _b.call(_a, context)) || {}; if (bulkDataPromise instanceof Promise) { bulkDataPromise.then(() => { debugCache("retrieved bulk data %o", this.path); }); } else { debugCache("retrieved bulk data %o", this.path); } this._bulkDataCache = bulkDataPromise; } else { debugCache("using cached bulk data %o", this.path); } return this._bulkDataCache; }); this.definition = definition; debug("defined %o", this.path); } /** Path pattern of this Akte files. */ get path() { return this.definition.path; } /** @internal Prefer {@link AkteApp.render} or use at your own risks. */ async render(args) { const data = args.data || await this.getData(args); return this.definition.render({ path: args.path, globalData: args.globalData, data }); } /** @internal Prefer {@link AkteApp.renderAll} or use at your own risks. */ async renderAll(args) { if (!this.definition.bulkData) { debugRender("no files to render %o", this.path); return {}; } debugRender("rendering files... %o", this.path); const bulkData = await this.getBulkData(args); const render = async (path, data) => { const content = await this.definition.render({ path, globalData: args.globalData, data }); debugRender("rendered %o", path); return [pathToFilePath.pathToFilePath(path), content]; }; const promises = []; for (const path in bulkData) { const data = bulkData[path]; promises.push(render(path, data)); } const fileEntries = await Promise.all(Object.values(promises)); debugRender(`rendered %o ${fileEntries.length > 1 ? "files" : "file"} %o`, fileEntries.length, this.path); return Object.fromEntries(fileEntries); } /** @internal Prefer {@link AkteApp.clearCache} or use at your own risks. */ clearCache() { this._dataMapCache.clear(); this._bulkDataCache = void 0; } /** * Readonly cache of files' definition `data` method. * * @experimental Programmatic API might still change not following SemVer. */ get dataMapCache() { return toReadonlyMap.toReadonlyMap(this._dataMapCache); } /** * Readonly cache of files' definition `bulkData` method. * * @experimental Programmatic API might still change not following SemVer. */ get bulkDataCache() { return this._bulkDataCache; } } exports.AkteFiles = AkteFiles; //# sourceMappingURL=AkteFiles.cjs.map