UNPKG

@paroicms/server

Version:
81 lines 2.83 kB
import { createNoTracker } from "@paroicms/internal-server-lib"; let renderingSeq = 0n; export async function createRenderingContext(siteContext, options) { const { cacheKey, tracker } = options; const language = options.language ?? siteContext.siteSchema.defaultLanguage; const dependencies = new Set(options.dependencyKey ? [options.dependencyKey] : undefined); const cache = new Map(); const imageCacheTransaction = siteContext.imageCache.createOwnerTransaction(options.cacheKey); return { siteContext, renderingId: ++renderingSeq, cacheKey, language, isPreview: options.isPreview ?? false, mainDocument: options.mainDocument, withAttachedData: { language }, tracker, sourceImagesByGalleryHandles: new Map(), imageCacheTransaction, pluginRenderState: new Map(), getDependencyKeys() { return Array.from(dependencies); }, addDependencyKey(...dependencyKeys) { for (const key of dependencyKeys) dependencies.add(key); }, getValueFromCache(key) { const val = cache.get(key); return val ?? { found: false }; }, setValueIntoCache(key, val) { cache.set(key, { found: true, val }); }, async close() { await imageCacheTransaction.flush(); }, async flush() { await imageCacheTransaction.flush(); }, }; } export function makeTagCacheKeyForRendering(cacheKey, { tagParameters }) { return `${cacheKey}:${tagParameters === undefined ? "-" : JSON.stringify(tagParameters)}`; } export function createNoRenderingContext(siteContext) { const cache = new Map(); const cacheKey = "outside-rendering"; const imageCacheTransaction = siteContext.imageCache.createOwnerTransaction(cacheKey); const language = siteContext.siteSchema.defaultLanguage; return { siteContext, renderingId: -1n, cacheKey, language, isPreview: false, mainDocument: undefined, withAttachedData: { language }, tracker: createNoTracker(), sourceImagesByGalleryHandles: new Map(), imageCacheTransaction, pluginRenderState: new Map(), getDependencyKeys() { return []; }, addDependencyKey() { }, getValueFromCache(key) { return cache.get(key) ?? { found: false }; }, setValueIntoCache(key, val) { cache.set(key, { found: true, val }); }, async close() { await imageCacheTransaction.flush(); }, async flush() { await imageCacheTransaction.flush(); }, }; } //# sourceMappingURL=rendering-context.js.map