UNPKG

@hazae41/glacier

Version:

Yet another React data (re)fetching library

49 lines (46 loc) 1.66 kB
'use strict'; var core = require('../../core/core.cjs'); exports.Simple = void 0; (function (Simple) { function getCacheKey(key) { if (typeof key === "string") return key; return JSON.stringify(key); } Simple.getCacheKey = getCacheKey; async function fetchOrThrow(cacheKey, signal, settings) { const fetched = await settings.fetcher(settings.key, { signal }); return await core.core.replaceOrThrow(cacheKey, fetched, settings); } Simple.fetchOrThrow = fetchOrThrow; /** * Optimistic update * @param core * @param key * @param cacheKey * @param fetcher * @param updater * @param settings * @returns */ async function updateOrThrow(cacheKey, updater, signal, settings) { const uuid = crypto.randomUUID(); try { const generator = updater(); let next = await generator.next(); for (; !next.done; next = await generator.next()) await core.core.optimizeOrThrow(cacheKey, uuid, next.value, settings); const fetcher = next.value ?? settings.fetcher; const fetched = await fetcher(settings.key, { signal, cache: "reload" }); core.core.deoptimize(cacheKey, uuid); return await core.core.replaceOrThrow(cacheKey, fetched, settings); } catch (e) { core.core.deoptimize(cacheKey, uuid); core.core.reoptimizeOrThrow(cacheKey, settings); throw e; } } Simple.updateOrThrow = updateOrThrow; })(exports.Simple || (exports.Simple = {})); //# sourceMappingURL=helper.cjs.map