@paroicms/server-image-cache-engine
Version:
The image variant engine that we use at Paroi.
61 lines • 2.28 kB
JavaScript
import { fetchStoredCacheItems } from "../internal/queries.js";
import { makeTaskKey, toTransactionCacheItem } from "../internal/utils.js";
import { makeTransactionImageAvailable } from "./make-t-image-available.js";
import { createOwnerImageManager } from "./owner-image-manager.js";
export function createOwnerTransaction(options) {
const { engine: { context: engineContext, startQueue, addPending }, ownerHandle, isHandleReusable, canBeRetrievedByOwner, } = options;
let opened = true;
let initialFetchDone = false;
const otContext = {
engineContext,
startQueue,
ownerHandle,
isHandleReusable,
canBeRetrievedByOwner,
ownerImageManager: createOwnerImageManager({
ownerHandle,
isHandleReusable,
engineContext,
}),
localCache: new Map(),
};
const removePending = addPending(async () => {
if (!opened)
return;
opened = false;
await otContext.ownerImageManager.flush();
});
return {
async makeImageAvailable(inputs, options = {}) {
checkStatus();
if (!initialFetchDone) {
initialFetchDone = true;
if (options.optimizeForBatch) {
const fetched = await fetchStoredCacheItems(engineContext, ownerHandle);
for (const item of fetched) {
const taskKey = makeTaskKey(item.variant);
const cacheItem = toTransactionCacheItem(item);
otContext.localCache.set(taskKey, cacheItem);
}
}
}
return await makeTransactionImageAvailable(otContext, inputs, options);
},
async flush() {
checkStatus();
await otContext.ownerImageManager.flush();
},
async close() {
checkStatus();
removePending();
opened = false;
await otContext.ownerImageManager.flush();
},
};
function checkStatus() {
if (!opened)
throw new Error(`ImageCacheUseTransaction "${ownerHandle}" is closed`);
options.engine.checkStatus();
}
}
//# sourceMappingURL=owner-transaction.js.map