UNPKG

@hazae41/glacier

Version:

Yet another React data (re)fetching library

68 lines (64 loc) 1.63 kB
'use strict'; var result = require('@hazae41/result'); class Data extends result.Ok { data; time; cooldown; expiration; constructor(data, init = {}) { super(data); const { time = Date.now(), cooldown, expiration } = init; this.data = data; this.time = time; this.cooldown = cooldown; this.expiration = expiration; } static from(init) { const { data, time, cooldown, expiration } = init; return new Data(data, { time, cooldown, expiration }); } isData() { return true; } isFail() { return false; } set(inner) { return new Data(inner, this); } setErr(inner) { return this; } setInit(init) { return new Data(this.inner, init); } async map(mapper) { return new Data(await mapper(this.get()), this); } mapSync(mapper) { return new Data(mapper(this.get()), this); } /** * Transform Result<Promise<T>, E> into Promise<Result<T, E>> * @returns `await this.inner` if `Ok`, `this` if `Err` */ async await() { return new Data(await this.inner, this); } /** * Transform Result<T, Promise<E>> into Promise<Result<T, E>> * @returns `await this.inner` if `Err`, `this` if `Ok` */ async awaitErr() { return this; } /** * Transform Result<Promise<T>, Promise<E>> into Promise<Result<T, E>> * @returns `await this.inner` */ async awaitAll() { return await this.await(); } } exports.Data = Data; //# sourceMappingURL=data.cjs.map