@hazae41/glacier
Version:
Yet another React data (re)fetching library
119 lines (115 loc) • 5.02 kB
JavaScript
;
var option = require('@hazae41/option');
var result = require('@hazae41/result');
var index = require('../../../libs/request/index.cjs');
var index$1 = require('../../../libs/signals/index.cjs');
var time = require('../../../libs/time/time.cjs');
var core = require('../../core/core.cjs');
var helper = require('./helper.cjs');
function createQuery(settings) {
if (settings.fetcher == null)
return new SimpleFetcherlessQuery(settings);
else
return new SimpleFetcherfulQuery(settings);
}
class SimpleFetcherlessQuery {
settings;
cacheKey;
constructor(settings) {
this.settings = settings;
this.cacheKey = helper.Simple.getCacheKey(settings.key);
}
get state() {
return core.core.getOrThrow(this.cacheKey, this.settings);
}
get aborter() {
return core.core.getAborterSync(this.cacheKey);
}
async mutateOrThrow(mutator) {
return await core.core.mutateOrThrow(this.cacheKey, mutator, this.settings);
}
async deleteOrThrow() {
return await core.core.deleteOrThrow(this.cacheKey, this.settings);
}
async normalizeOrThrow(fetched, more) {
if (more.shallow)
return;
await this.mutateOrThrow(() => new option.Some(fetched));
}
async fetchOrThrow(aborter = new AbortController()) {
throw new core.MissingFetcherError();
}
async refetchOrThrow(aborter = new AbortController()) {
throw new core.MissingFetcherError();
}
async updateOrThrow(updater, aborter = new AbortController()) {
throw new core.MissingFetcherError();
}
}
class SimpleFetcherfulQuery {
settings;
cacheKey;
constructor(settings) {
this.settings = settings;
this.cacheKey = helper.Simple.getCacheKey(settings.key);
}
get state() {
return core.core.getOrThrow(this.cacheKey, this.settings);
}
get aborter() {
return core.core.getAborterSync(this.cacheKey);
}
async mutateOrThrow(mutator) {
return await core.core.mutateOrThrow(this.cacheKey, mutator, this.settings);
}
async deleteOrThrow() {
return await core.core.deleteOrThrow(this.cacheKey, this.settings);
}
async normalizeOrThrow(fetched, more) {
if (more.shallow)
return;
await this.mutateOrThrow(() => new option.Some(fetched));
}
async fetchOrThrow(init) {
const { cacheKey, settings } = this;
const state = await this.state;
if (index.shouldUseCacheIfFresh(init?.cache) && time.Time.isAfterNow(state.real?.current.cooldown))
return new result.Err(state);
if (index.shouldUseCacheIfStale(init?.cache) && time.Time.isAfterNow(state.real?.current.expiration))
return new result.Err(state);
if (!index.shouldUseNetwork(init?.cache))
throw new Error(`Could not fetch using the provided cache directive`);
const aborter = new AbortController();
const signal = AbortSignal.any([aborter.signal, index$1.AbortSignals.getOrNever(init?.signal)]);
return new result.Ok(await core.core.runOrJoin(cacheKey, aborter, () => helper.Simple.fetchOrThrow(cacheKey, signal, settings)));
}
async refetchOrThrow(init) {
const { cacheKey, settings } = this;
const state = await this.state;
if (index.shouldUseCacheIfFresh(init?.cache) && time.Time.isAfterNow(state.real?.current.cooldown))
return new result.Err(state);
if (index.shouldUseCacheIfStale(init?.cache) && time.Time.isAfterNow(state.real?.current.expiration))
return new result.Err(state);
if (!index.shouldUseNetwork(init?.cache))
throw new Error(`Could not fetch using the provided cache directive`);
const aborter = new AbortController();
const signal = AbortSignal.any([aborter.signal, index$1.AbortSignals.getOrNever(init?.signal)]);
return new result.Ok(await core.core.runOrReplace(cacheKey, aborter, () => helper.Simple.fetchOrThrow(cacheKey, signal, settings)));
}
async updateOrThrow(updater, init) {
const { cacheKey, settings } = this;
const state = await this.state;
if (index.shouldUseCacheIfFresh(init?.cache) && time.Time.isAfterNow(state.real?.current.cooldown))
return new result.Err(state);
if (index.shouldUseCacheIfStale(init?.cache) && time.Time.isAfterNow(state.real?.current.expiration))
return new result.Err(state);
if (!index.shouldUseNetwork(init?.cache))
throw new Error(`Could not fetch using the provided cache directive`);
const signal = index$1.AbortSignals.getOrNever(init?.signal);
return new result.Ok(await helper.Simple.updateOrThrow(cacheKey, updater, signal, settings));
}
}
exports.SimpleFetcherfulQuery = SimpleFetcherfulQuery;
exports.SimpleFetcherlessQuery = SimpleFetcherlessQuery;
exports.createQuery = createQuery;
//# sourceMappingURL=query.cjs.map