UNPKG

4.5 kBJavaScriptView Raw
1var __defProp = Object.defineProperty;
2var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3var __publicField = (obj, key, value) => {
4 __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5 return value;
6};
7import { NotFoundError } from "./errors.js";
8import { createDebugger } from "./lib/createDebugger.js";
9import { pathToFilePath } from "./lib/pathToFilePath.js";
10const debug = createDebugger("akte:files");
11const debugRender = createDebugger("akte:files:render");
12const debugCache = createDebugger("akte:files:cache");
13class AkteFiles {
14 constructor(definition) {
15 __publicField(this, "definition");
16 __publicField(this, "_dataPromiseMap", /* @__PURE__ */ new Map());
17 __publicField(this, "getDataPromise", (context) => {
18 const maybePromise = this._dataPromiseMap.get(context.path);
19 if (maybePromise) {
20 debugCache("using cached data %o", context.path);
21 return maybePromise;
22 }
23 debugCache("retrieving data... %o", context.path);
24 let promise;
25 if (this.definition.data) {
26 promise = this.definition.data(context);
27 } else if (this.definition.bulkData) {
28 const dataFromBulkData = async (path) => {
29 const bulkData = await this.definition.bulkData({
30 globalData: context.globalData
31 });
32 if (path in bulkData) {
33 return bulkData[path];
34 }
35 throw new NotFoundError(path);
36 };
37 promise = dataFromBulkData(context.path);
38 } else {
39 throw new Error(`Cannot render file for path \`${context.path}\`, no \`data\` or \`bulkData\` function available`);
40 }
41 if (promise instanceof Promise) {
42 promise.then(() => {
43 debugCache("retrieved data %o", context.path);
44 }).catch(() => {
45 });
46 } else {
47 debugCache("retrieved data %o", context.path);
48 }
49 this._dataPromiseMap.set(context.path, promise);
50 return promise;
51 });
52 __publicField(this, "_bulkDataPromise");
53 __publicField(this, "getBulkDataPromise", (context) => {
54 var _a, _b;
55 if (!this._bulkDataPromise) {
56 debugCache("retrieving bulk data... %o", this.path);
57 const bulkDataPromise = ((_b = (_a = this.definition).bulkData) == null ? void 0 : _b.call(_a, context)) || {};
58 if (bulkDataPromise instanceof Promise) {
59 bulkDataPromise.then(() => {
60 debugCache("retrieved bulk data %o", this.path);
61 });
62 } else {
63 debugCache("retrieved bulk data %o", this.path);
64 }
65 this._bulkDataPromise = bulkDataPromise;
66 } else {
67 debugCache("using cached bulk data %o", this.path);
68 }
69 return this._bulkDataPromise;
70 });
71 this.definition = definition;
72 debug("created %o", this.path);
73 }
74 /** Path pattern of this Akte files. */
75 get path() {
76 return this.definition.path;
77 }
78 /** @internal Prefer {@link AkteApp.render} or use at your own risks. */
79 async render(args) {
80 const data = await this.getDataPromise(args);
81 return this.definition.render({
82 path: args.path,
83 globalData: args.globalData,
84 data
85 });
86 }
87 /** @internal Prefer {@link AkteApp.renderAll} or use at your own risks. */
88 async renderAll(args) {
89 if (!this.definition.bulkData) {
90 debugRender("no files to render %o", this.path);
91 return {};
92 }
93 debugRender("rendering files... %o", this.path);
94 const bulkData = await this.getBulkDataPromise(args);
95 const render = async (path, data) => {
96 const content = await this.definition.render({
97 path,
98 globalData: args.globalData,
99 data
100 });
101 debugRender("rendered %o", path);
102 return [pathToFilePath(path), content];
103 };
104 const promises = [];
105 for (const path in bulkData) {
106 const data = bulkData[path];
107 promises.push(render(path, data));
108 }
109 const fileEntries = await Promise.all(Object.values(promises));
110 debugRender(`rendered %o ${fileEntries.length > 1 ? "files" : "file"} %o`, fileEntries.length, this.path);
111 return Object.fromEntries(fileEntries);
112 }
113 /** @internal Prefer {@link AkteApp.clearCache} or use at your own risks. */
114 clearCache() {
115 this._dataPromiseMap = /* @__PURE__ */ new Map();
116 this._bulkDataPromise = void 0;
117 }
118}
119export {
120 AkteFiles
121};
122//# sourceMappingURL=AkteFiles.js.map