From cb788952a418d77d6dc339fbf5696415af83df27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Tue, 13 Jan 2026 11:11:53 +0100 Subject: [PATCH] Refactor Result --- packages/effect-fc/src/Result.ts | 52 +++++++++++++++++++------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/packages/effect-fc/src/Result.ts b/packages/effect-fc/src/Result.ts index 2877412..349434a 100644 --- a/packages/effect-fc/src/Result.ts +++ b/packages/effect-fc/src/Result.ts @@ -5,18 +5,16 @@ export const ResultTypeId: unique symbol = Symbol.for("@effect-fc/Result/Result" export type ResultTypeId = typeof ResultTypeId export type Result = ( - | Initial + // biome-ignore lint/complexity/noBannedTypes: "{}" is relevant here + | (Initial & ({} | WillFetch)) | Running

| Final -// biome-ignore lint/complexity/noBannedTypes: relevant here -) & ({} | Optimistic) +) export type Final = ( & (Success | Failure) - // biome-ignore lint/complexity/noBannedTypes: relevant here - & ({} | Refreshing

) - // biome-ignore lint/complexity/noBannedTypes: relevant here - & ({} | Optimistic) + // biome-ignore lint/complexity/noBannedTypes: "{}" is relevant here + & ({} | WillFetch | WillRefresh | Refreshing

) ) export namespace Result { @@ -49,13 +47,17 @@ export interface Failure extends Result.Prototype { readonly previousSuccess: Option.Option> } -export interface Refreshing

{ - readonly refreshing: true - readonly progress: P +export interface WillFetch extends Result.Prototype { + readonly willFetch: true } -export interface Optimistic { - readonly optimistic: true +export interface WillRefresh extends Result.Prototype { + readonly willRefresh: true +} + +export interface Refreshing

extends Result.Prototype { + readonly refreshing: true + readonly progress: P } @@ -111,8 +113,9 @@ export const isInitial = (u: unknown): u is Initial => isResult(u) && u._tag === export const isRunning = (u: unknown): u is Running => isResult(u) && u._tag === "Running" export const isSuccess = (u: unknown): u is Success => isResult(u) && u._tag === "Success" export const isFailure = (u: unknown): u is Failure => isResult(u) && u._tag === "Failure" +export const isWillFetch = (u: unknown): u is WillFetch => isResult(u) && Predicate.hasProperty(u, "willFetch") +export const isWillRefresh = (u: unknown): u is WillRefresh => isResult(u) && Predicate.hasProperty(u, "willRefresh") export const isRefreshing = (u: unknown): u is Refreshing => isResult(u) && Predicate.hasProperty(u, "refreshing") -export const isOptimistic = (u: unknown): u is Optimistic => isResult(u) && Predicate.hasProperty(u, "optimistic") export const initial: { (): Initial @@ -130,7 +133,21 @@ export const fail = ( previousSuccess: Option.fromNullable(previousSuccess), }, ResultPrototype) -export const refreshing = | Failure, P = never>( +export const willFetch = >( + result: R +): Omit & WillFetch => Object.setPrototypeOf( + Object.assign({}, result, { willFetch: true }), + Object.getPrototypeOf(result), +) + +export const willRefresh = >( + result: R +): Omit & WillRefresh => Object.setPrototypeOf( + Object.assign({}, result, { willRefresh: true }), + Object.getPrototypeOf(result), +) + +export const refreshing = , P = never>( result: R, progress?: P, ): Omit>> & Refreshing

=> Object.setPrototypeOf( @@ -138,13 +155,6 @@ export const refreshing = | Failure, P = never Object.getPrototypeOf(result), ) -export const optimistic = | Failure>( - result: R -): Omit & Optimistic => Object.setPrototypeOf( - Object.assign({}, result, { optimistic: true }), - Object.getPrototypeOf(result), -) - export const fromExit = ( exit: Exit.Exit, previousSuccess?: Success>,