UNPKG

@hazae41/glacier

Version:

Yet another React data (re)fetching library

136 lines (132 loc) 5.71 kB
'use strict'; var option = require('@hazae41/option'); var result = require('@hazae41/result'); var arrays = require('../../../libs/arrays/arrays.cjs'); 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 createScrollableQuery(settings) { if (settings.fetcher == null) return new ScrollableFetcherlessQuery(settings); return new ScrollableFetcherfulQuery(settings); } class ScrollableFetcherfulQuery { settings; cacheKey; constructor(settings) { this.settings = settings; this.cacheKey = helper.Scrollable.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.Scrollable.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.Scrollable.fetchOrThrow(cacheKey, signal, settings))); } async scrollOrThrow(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.Scrollable.scrollOrThrow(cacheKey, signal, settings))); } async peekOrNull() { const { settings } = this; const state = await this.state; const pages = state.real?.data?.get(); if (pages == null) return undefined; return settings.scroller(arrays.Arrays.last(pages)); } } class ScrollableFetcherlessQuery { settings; cacheKey; constructor(settings) { this.settings = settings; this.cacheKey = helper.Scrollable.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) { throw new core.MissingFetcherError(); } async refetchOrThrow(init) { throw new core.MissingFetcherError(); } async scrollOrThrow(init) { throw new core.MissingFetcherError(); } async peekOrNull() { const { settings } = this; const state = await this.state; const pages = state.real?.data?.get(); if (pages == null) return undefined; return settings.scroller(arrays.Arrays.last(pages)); } } exports.ScrollableFetcherfulQuery = ScrollableFetcherfulQuery; exports.ScrollableFetcherlessQuery = ScrollableFetcherlessQuery; exports.createScrollableQuery = createScrollableQuery; //# sourceMappingURL=query.cjs.map