@nuxtjs/i18n
Version:
Internationalization for Nuxt
34 lines (33 loc) • 1.13 kB
JavaScript
import { useStorage } from "nitropack/runtime";
import { prefixStorage } from "unstorage";
const storage = prefixStorage(useStorage(), "i18n");
export function cachedFunctionI18n(fn, opts) {
opts = { maxAge: 1, ...opts };
const pending = {};
async function get(key, resolver) {
const isPending = pending[key];
if (!isPending) {
pending[key] = Promise.resolve(resolver());
}
try {
return await pending[key];
} finally {
delete pending[key];
}
}
return async (...args) => {
const key = [opts.name, opts.getKey(...args)].join(":").replace(/:\/$/, ":index");
const maxAge = opts.maxAge ?? 1;
const isCacheable = !opts.shouldBypassCache(...args) && maxAge >= 0;
const cache = isCacheable && await storage.getItemRaw(key);
if (!cache || cache.ttl < Date.now()) {
pending[key] = Promise.resolve(fn(...args));
const value = await get(key, () => fn(...args));
if (isCacheable) {
await storage.setItemRaw(key, { ttl: Date.now() + maxAge * 1e3, value, mtime: Date.now() });
}
return value;
}
return cache.value;
};
}