UNPKG

5.57 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";
10import { toReadonlyMap } from "./lib/toReadonlyMap.js";
11const debug = createDebugger("akte:files");
12const debugRender = createDebugger("akte:files:render");
13const debugCache = createDebugger("akte:files:cache");
14class AkteFiles {
15 constructor(definition) {
16 __publicField(this, "definition");
17 __publicField(this, "_dataMapCache", /* @__PURE__ */ new Map());
18 /**
19 * Retrieves data from files' definition `data` method with given context.
20 *
21 * @param context - Context to get data with.
22 * @returns Retrieved data.
23 * @remark Returned data may come from cache.
24 * @experimental Programmatic API might still change not following SemVer.
25 */
26 __publicField(this, "getData", (context) => {
27 const maybePromise = this._dataMapCache.get(context.path);
28 if (maybePromise) {
29 debugCache("using cached data %o", context.path);
30 return maybePromise;
31 }
32 debugCache("retrieving data... %o", context.path);
33 let promise;
34 if (this.definition.data) {
35 promise = this.definition.data(context);
36 } else if (this.definition.bulkData) {
37 const dataFromBulkData = async (path) => {
38 const bulkData = await this.getBulkData({
39 globalData: context.globalData
40 });
41 if (path in bulkData) {
42 return bulkData[path];
43 }
44 throw new NotFoundError(path);
45 };
46 promise = dataFromBulkData(context.path);
47 } else {
48 throw new Error(`Cannot render file for path \`${context.path}\`, no \`data\` or \`bulkData\` function available`);
49 }
50 if (promise instanceof Promise) {
51 promise.then(() => {
52 debugCache("retrieved data %o", context.path);
53 }).catch(() => {
54 });
55 } else {
56 debugCache("retrieved data %o", context.path);
57 }
58 this._dataMapCache.set(context.path, promise);
59 return promise;
60 });
61 __publicField(this, "_bulkDataCache");
62 /**
63 * Retrieves data from files' definition `bulkData` method with given context.
64 *
65 * @param context - Context to get bulk data with.
66 * @returns Retrieved bulk data.
67 * @remark Returned bulk data may come from cache.
68 * @experimental Programmatic API might still change not following SemVer.
69 */
70 __publicField(this, "getBulkData", (context) => {
71 var _a, _b;
72 if (!this._bulkDataCache) {
73 debugCache("retrieving bulk data... %o", this.path);
74 const bulkDataPromise = ((_b = (_a = this.definition).bulkData) == null ? void 0 : _b.call(_a, context)) || {};
75 if (bulkDataPromise instanceof Promise) {
76 bulkDataPromise.then(() => {
77 debugCache("retrieved bulk data %o", this.path);
78 });
79 } else {
80 debugCache("retrieved bulk data %o", this.path);
81 }
82 this._bulkDataCache = bulkDataPromise;
83 } else {
84 debugCache("using cached bulk data %o", this.path);
85 }
86 return this._bulkDataCache;
87 });
88 this.definition = definition;
89 debug("defined %o", this.path);
90 }
91 /** Path pattern of this Akte files. */
92 get path() {
93 return this.definition.path;
94 }
95 /** @internal Prefer {@link AkteApp.render} or use at your own risks. */
96 async render(args) {
97 const data = args.data || await this.getData(args);
98 return this.definition.render({
99 path: args.path,
100 globalData: args.globalData,
101 data
102 });
103 }
104 /** @internal Prefer {@link AkteApp.renderAll} or use at your own risks. */
105 async renderAll(args) {
106 if (!this.definition.bulkData) {
107 debugRender("no files to render %o", this.path);
108 return {};
109 }
110 debugRender("rendering files... %o", this.path);
111 const bulkData = await this.getBulkData(args);
112 const render = async (path, data) => {
113 const content = await this.definition.render({
114 path,
115 globalData: args.globalData,
116 data
117 });
118 debugRender("rendered %o", path);
119 return [pathToFilePath(path), content];
120 };
121 const promises = [];
122 for (const path in bulkData) {
123 const data = bulkData[path];
124 promises.push(render(path, data));
125 }
126 const fileEntries = await Promise.all(Object.values(promises));
127 debugRender(`rendered %o ${fileEntries.length > 1 ? "files" : "file"} %o`, fileEntries.length, this.path);
128 return Object.fromEntries(fileEntries);
129 }
130 /** @internal Prefer {@link AkteApp.clearCache} or use at your own risks. */
131 clearCache() {
132 this._dataMapCache.clear();
133 this._bulkDataCache = void 0;
134 }
135 /**
136 * Readonly cache of files' definition `data` method.
137 *
138 * @experimental Programmatic API might still change not following SemVer.
139 */
140 get dataMapCache() {
141 return toReadonlyMap(this._dataMapCache);
142 }
143 /**
144 * Readonly cache of files' definition `bulkData` method.
145 *
146 * @experimental Programmatic API might still change not following SemVer.
147 */
148 get bulkDataCache() {
149 return this._bulkDataCache;
150 }
151}
152export {
153 AkteFiles
154};
155//# sourceMappingURL=AkteFiles.js.map