// TODO: Convert to effect/core /* eslint-disable @typescript-eslint/ban-types */ import * as Either from "effect/Either" export class Initial extends Data.TaggedClass("Initial")<{}> {} export class Loading extends Data.TaggedClass("Loading")<{}> {} export class Refreshing extends Data.TaggedClass("Refreshing")<{ readonly current: Either.Either readonly previous: Option }> { static succeed(a: A) { return new Refreshing({ current: Either.right(a), previous: Option.none() }) } static fail(e: E, previous?: A) { return new Refreshing({ current: Either.left(e), previous: previous === undefined ? Option.none() : Option.some(previous) }) } static fromDone(d: Done) { return new Refreshing(d) } } export class Done extends Data.TaggedClass("Done")<{ readonly current: Either.Either readonly previous: Option }> { static succeed(this: void, a: A) { return new Done({ current: Either.right(a), previous: Option.none() }) } static fail(this: void, e: E, previous?: A) { return new Done({ current: Either.left(e), previous: previous === undefined ? Option.none() : Option.some(previous) }) } static refresh(d: Done) { return new Refreshing(d) } } /** * @tsplus type QueryResult */ export type QueryResult = Initial | Loading | Refreshing | Done type Result = Omit, "current"> | Omit, "current"> /** * @tsplus fluent QueryResult isSuccess */ export function isSuccess( qr: QueryResult ): qr is Result & { current: Either.Right } { return qr.hasValue() && qr.current.isRight() } /** * @tsplus fluent QueryResult hasValue */ export function hasValue( qr: QueryResult ): qr is Done | Refreshing { return qr.isDone() || qr.isRefreshing() } /** * @tsplus fluent QueryResult isRefreshing */ export function isRefreshing( qr: QueryResult ): qr is Refreshing { return qr._tag === "Refreshing" } /** * @tsplus fluent QueryResult isDone */ export function isDone( qr: QueryResult ): qr is Done { return qr._tag === "Done" } /** * @tsplus fluent QueryResult isInitializing */ export function isInitializing( qr: QueryResult ): qr is Initial | Loading { return qr._tag === "Initial" || qr._tag === "Loading" } /** * @tsplus fluent QueryResult isFailed */ export function isFailed( qr: QueryResult ): qr is Result & { current: Either.Left } { return qr.hasValue() && qr.current.isLeft() } export type ResultTuple = readonly [result: Result, refresh: () => void] export type QueryResultTuple = ResultTuple> export const { fail, succeed } = Done /** * @tsplus getter effect/io/Effect asQueryResult */ export function queryResult( self: Effect ): Effect, never, R> { return self.match({ onFailure: fail, onSuccess: succeed }) }