Compare commits

...

4 Commits

Author SHA1 Message Date
9d35e8eef3 Update bun minor+patch updates
Some checks failed
Lint / lint (push) Failing after 7s
Test build / test-build (pull_request) Failing after 44s
2026-01-13 12:01:41 +00:00
Julien Valverdé
cb788952a4 Refactor Result
Some checks failed
Lint / lint (push) Failing after 42s
2026-01-13 11:11:53 +01:00
Julien Valverdé
cd18a9d108 Query work
All checks were successful
Lint / lint (push) Successful in 13s
2026-01-11 13:08:16 +01:00
Julien Valverdé
a6d91a93a5 Cached queries
All checks were successful
Lint / lint (push) Successful in 53s
2026-01-11 11:08:32 +01:00
6 changed files with 101 additions and 57 deletions

View File

@@ -6,7 +6,7 @@
"name": "@effect-fc/monorepo",
"devDependencies": {
"@biomejs/biome": "^2.3.8",
"@effect/language-service": "^0.60.0",
"@effect/language-service": "^0.65.0",
"@types/bun": "^1.3.3",
"npm-check-updates": "^19.1.2",
"npm-sort": "^0.0.4",
@@ -131,7 +131,7 @@
"@effect-fc/example": ["@effect-fc/example@workspace:packages/example"],
"@effect/language-service": ["@effect/language-service@0.60.0", "", { "bin": { "effect-language-service": "cli.js" } }, "sha512-elJDWHG5Naq3OkilPt9ZRn56JfSA3MhXUIlDx9RWJeScHm96kZ+HkZ3eFBxqROzXwD6Q2DTtFctFwOM0+QLZEA=="],
"@effect/language-service": ["@effect/language-service@0.65.0", "", { "bin": { "effect-language-service": "cli.js" } }, "sha512-eHcpLNCZa1XEDRrXLZqTdky6jAQojL6zQEW53Ba6vJL35j77tJTnV9BFkk34G3rxKoplNo39U0Mum3RfuH9rsg=="],
"@effect/platform": ["@effect/platform@0.93.6", "", { "dependencies": { "find-my-way-ts": "^0.1.6", "msgpackr": "^1.11.4", "multipasta": "^0.2.7" }, "peerDependencies": { "effect": "^3.19.8" } }, "sha512-I5lBGQWzWXP4zlIdPs7z7WHmEFVBQhn+74emr/h16GZX96EEJ6I1rjGaKyZF7mtukbMuo9wEckDPssM8vskZ/w=="],

View File

@@ -16,7 +16,7 @@
},
"devDependencies": {
"@biomejs/biome": "^2.3.8",
"@effect/language-service": "^0.60.0",
"@effect/language-service": "^0.65.0",
"@types/bun": "^1.3.3",
"npm-check-updates": "^19.1.2",
"npm-sort": "^0.0.4",

View File

@@ -1,4 +1,4 @@
import { type Cause, type Context, DateTime, type Duration, Effect, Fiber, HashMap, identity, Option, Pipeable, Predicate, type Scope, Stream, type Subscribable, SubscriptionRef } from "effect"
import { type Cause, type Context, DateTime, type Duration, Effect, Exit, Fiber, HashMap, identity, Option, Pipeable, Predicate, type Scope, Stream, Subscribable, SubscriptionRef } from "effect"
import * as QueryClient from "./QueryClient.js"
import * as Result from "./Result.js"
@@ -21,6 +21,7 @@ extends Pipeable.Pipeable {
readonly fiber: Subscribable.Subscribable<Option.Option<Fiber.Fiber<A, E>>>
readonly result: Subscribable.Subscribable<Result.Result<A, E, P>>
readonly run: Effect.Effect<void>
fetch(key: K): Effect.Effect<Result.Final<A, E, P>>
fetchSubscribable(key: K): Effect.Effect<Subscribable.Subscribable<Result.Result<A, E, P>>>
readonly refetch: Effect.Effect<Result.Final<A, E, P>, Cause.NoSuchElementException>
@@ -54,6 +55,16 @@ extends Pipeable.Class() implements Query<K, A, E, R, P> {
super()
}
get run(): Effect.Effect<void> {
return Stream.runForEach(this.key, key => this.interrupt.pipe(
Effect.andThen(SubscriptionRef.set(this.latestKey, Option.some(key))),
Effect.andThen(this.startCached(key, Result.initial(), false)),
Effect.andThen(sub => Effect.forkScoped(this.watch(sub))),
Effect.provide(this.context),
this.runSemaphore.withPermits(1),
))
}
get interrupt(): Effect.Effect<void, never, never> {
return Effect.andThen(this.fiber, Option.match({
onSome: Fiber.interrupt,
@@ -64,21 +75,21 @@ extends Pipeable.Class() implements Query<K, A, E, R, P> {
fetch(key: K): Effect.Effect<Result.Final<A, E, P>> {
return this.interrupt.pipe(
Effect.andThen(SubscriptionRef.set(this.latestKey, Option.some(key))),
Effect.andThen(Effect.provide(this.start(key), this.context)),
Effect.andThen(Effect.provide(this.startCached(key, Result.initial(), false), this.context)),
Effect.andThen(sub => this.watch(sub)),
)
}
fetchSubscribable(key: K): Effect.Effect<Subscribable.Subscribable<Result.Result<A, E, P>>> {
return this.interrupt.pipe(
Effect.andThen(SubscriptionRef.set(this.latestKey, Option.some(key))),
Effect.andThen(Effect.provide(this.start(key), this.context)),
Effect.andThen(Effect.provide(this.startCached(key, Result.initial(), false), this.context)),
)
}
get refetch(): Effect.Effect<Result.Final<A, E, P>, Cause.NoSuchElementException> {
return this.interrupt.pipe(
Effect.andThen(this.latestKey),
Effect.andThen(identity),
Effect.andThen(key => Effect.provide(this.start(key), this.context)),
Effect.andThen(key => Effect.provide(this.start(key, Result.initial(), false), this.context)),
Effect.andThen(sub => this.watch(sub)),
)
}
@@ -86,14 +97,14 @@ extends Pipeable.Class() implements Query<K, A, E, R, P> {
return this.interrupt.pipe(
Effect.andThen(this.latestKey),
Effect.andThen(identity),
Effect.andThen(key => Effect.provide(this.start(key), this.context)),
Effect.andThen(key => Effect.provide(this.start(key, Result.initial(), false), this.context)),
)
}
get refresh(): Effect.Effect<Result.Final<A, E, P>, Cause.NoSuchElementException> {
return this.interrupt.pipe(
Effect.andThen(this.latestKey),
Effect.andThen(identity),
Effect.andThen(key => Effect.provide(this.start(key, true), this.context)),
Effect.andThen(key => Effect.provide(this.start(key, Result.initial(), true), this.context)),
Effect.andThen(sub => this.watch(sub)),
)
}
@@ -101,26 +112,53 @@ extends Pipeable.Class() implements Query<K, A, E, R, P> {
return this.interrupt.pipe(
Effect.andThen(this.latestKey),
Effect.andThen(identity),
Effect.andThen(key => Effect.provide(this.start(key, true), this.context)),
Effect.andThen(key => Effect.provide(this.start(key, Result.initial(), true), this.context)),
)
}
startCached(
key: K,
initial: Result.Result<A, E, P>,
refresh: boolean,
): Effect.Effect<
Subscribable.Subscribable<Result.Result<A, E, P>>,
never,
Scope.Scope | QueryClient.QueryClient | R
> {
return Effect.andThen(this.getCacheEntry(key), Option.match({
onSome: entry => QueryClient.isQueryClientCacheEntryStale(entry, this.staleTime)
? Effect.map(
SubscriptionRef.set(this.result, entry.result as Result.Result<A, E, P>),
() => Subscribable.make({
get: Effect.succeed(entry.result as Result.Result<A, E, P>),
get changes() { return Stream.empty },
}),
)
: this.start(key, Result.optimistic(entry.result) as Result.Result<A, E, P>, false),
onNone: () => this.start(key, initial, refresh),
}))
}
start(
key: K,
refresh?: boolean,
initial: Result.Result<A, E, P>,
refresh: boolean,
): Effect.Effect<
Subscribable.Subscribable<Result.Result<A, E, P>>,
never,
Scope.Scope | R
Scope.Scope | QueryClient.QueryClient | R
> {
return this.result.pipe(
Effect.map(previous => Result.isFinal(previous)
? previous
: undefined
),
Effect.map(previous => Result.isFinal(previous) ? previous : undefined),
Effect.andThen(previous => Result.unsafeForkEffect(
Effect.onExit(this.f(key), () => SubscriptionRef.set(this.fiber, Option.none())),
Effect.onExit(this.f(key), exit => Effect.andThen(
Exit.isSuccess(exit)
? this.updateCacheEntry(key, Result.succeed(exit.value))
: Effect.void,
SubscriptionRef.set(this.fiber, Option.none()),
)),
{
initial,
initialProgress: this.initialProgress,
refresh: refresh && previous,
previous,
@@ -214,18 +252,5 @@ export const service = <K extends Query.AnyKey, A, E = never, R = never, P = nev
Scope.Scope | QueryClient.QueryClient | Result.forkEffect.OutputContext<A, E, R, P>
> => Effect.tap(
make(options),
query => Effect.forkScoped(run(query)),
query => Effect.forkScoped(query.run),
)
export const run = <K extends Query.AnyKey, A, E, R, P>(
self: Query<K, A, E, R, P>
): Effect.Effect<void> => {
const _self = self as QueryImpl<K, A, E, R, P>
return Stream.runForEach(_self.key, key => _self.interrupt.pipe(
Effect.andThen(SubscriptionRef.set(_self.latestKey, Option.some(key))),
Effect.andThen(_self.start(key)),
Effect.andThen(sub => Effect.forkScoped(_self.watch(sub))),
Effect.provide(_self.context),
_self.runSemaphore.withPermits(1),
))
}

View File

@@ -1,4 +1,4 @@
import { type DateTime, type Duration, Effect, Equal, Equivalence, Hash, HashMap, Pipeable, Predicate, type Scope, SubscriptionRef } from "effect"
import { DateTime, Duration, Effect, Equal, Equivalence, Hash, HashMap, Pipeable, Predicate, type Scope, SubscriptionRef } from "effect"
import type * as Query from "./Query.js"
import type * as Result from "./Result.js"
@@ -105,3 +105,11 @@ implements Pipeable.Pipeable {
}
export const isQueryClientCacheEntry = (u: unknown): u is QueryClientCacheEntry => Predicate.hasProperty(u, QueryClientCacheEntryTypeId)
export const isQueryClientCacheEntryStale = (
self: QueryClientCacheEntry,
staleTime: Duration.DurationInput,
): Effect.Effect<boolean> => Effect.andThen(
DateTime.now,
now => Duration.lessThan(DateTime.distanceDuration(self.createdAt, now), staleTime),
)

View File

@@ -5,18 +5,16 @@ export const ResultTypeId: unique symbol = Symbol.for("@effect-fc/Result/Result"
export type ResultTypeId = typeof ResultTypeId
export type Result<A, E = never, P = never> = (
| Initial
// biome-ignore lint/complexity/noBannedTypes: "{}" is relevant here
| (Initial & ({} | WillFetch))
| Running<P>
| Final<A, E, P>
// biome-ignore lint/complexity/noBannedTypes: relevant here
) & ({} | Optimistic)
)
export type Final<A, E = never, P = never> = (
& (Success<A> | Failure<A, E>)
// biome-ignore lint/complexity/noBannedTypes: relevant here
& ({} | Refreshing<P>)
// biome-ignore lint/complexity/noBannedTypes: relevant here
& ({} | Optimistic)
// biome-ignore lint/complexity/noBannedTypes: "{}" is relevant here
& ({} | WillFetch | WillRefresh | Refreshing<P>)
)
export namespace Result {
@@ -49,13 +47,17 @@ export interface Failure<A, E = never> extends Result.Prototype {
readonly previousSuccess: Option.Option<Success<A>>
}
export interface Refreshing<P = never> {
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<P = never> 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<unknown> => isResult(u) && u._tag === "Running"
export const isSuccess = (u: unknown): u is Success<unknown> => isResult(u) && u._tag === "Success"
export const isFailure = (u: unknown): u is Failure<unknown, unknown> => isResult(u) && u._tag === "Failure"
export const isRefreshing = (u: unknown): u is Refreshing<unknown> => isResult(u) && Predicate.hasProperty(u, "refreshing") && u.refreshing
export const isOptimistic = (u: unknown): u is Optimistic => isResult(u) && Predicate.hasProperty(u, "optimistic") && u.optimistic
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<unknown> => isResult(u) && Predicate.hasProperty(u, "refreshing")
export const initial: {
(): Initial
@@ -130,7 +133,21 @@ export const fail = <E, A = never>(
previousSuccess: Option.fromNullable(previousSuccess),
}, ResultPrototype)
export const refreshing = <R extends Success<any> | Failure<any, any>, P = never>(
export const willFetch = <R extends Initial | Final<any, any, any>>(
result: R
): Omit<R, keyof WillFetch> & WillFetch => Object.setPrototypeOf(
Object.assign({}, result, { willFetch: true }),
Object.getPrototypeOf(result),
)
export const willRefresh = <R extends Final<any, any, any>>(
result: R
): Omit<R, keyof WillRefresh> & WillRefresh => Object.setPrototypeOf(
Object.assign({}, result, { willRefresh: true }),
Object.getPrototypeOf(result),
)
export const refreshing = <R extends Final<any, any, any>, P = never>(
result: R,
progress?: P,
): Omit<R, keyof Refreshing<Result.Progress<R>>> & Refreshing<P> => Object.setPrototypeOf(
@@ -138,13 +155,6 @@ export const refreshing = <R extends Success<any> | Failure<any, any>, P = never
Object.getPrototypeOf(result),
)
export const optimistic = <R extends Success<any> | Failure<any, any>>(
result: R
): Omit<R, keyof Optimistic> & Optimistic => Object.setPrototypeOf(
Object.assign({}, result, { optimistic: true }),
Object.getPrototypeOf(result),
)
export const fromExit = <A, E>(
exit: Exit.Exit<A, E>,
previousSuccess?: Success<NoInfer<A>>,
@@ -214,6 +224,7 @@ export namespace unsafeForkEffect {
export type OutputContext<A, E, R, P> = Exclude<R, State<A, E, P> | Progress<P> | Progress<never>>
export type Options<A, E, P> = {
readonly initial?: Result<A, E, P>
readonly initialProgress?: P
readonly previous?: Final<A, E, P>
} & (
@@ -235,7 +246,7 @@ export const unsafeForkEffect = <A, E, R, P = never>(
never,
Scope.Scope | unsafeForkEffect.OutputContext<A, E, R, P>
> => Effect.Do.pipe(
Effect.bind("ref", () => Ref.make(initial<A, E, P>())),
Effect.bind("ref", () => Ref.make(options?.initial ?? initial<A, E, P>())),
Effect.bind("pubsub", () => PubSub.unbounded<Result<A, E, P>>()),
Effect.bind("fiber", ({ ref, pubsub }) => Effect.forkScoped(State<A, E, P>().pipe(
Effect.andThen(state => state.set(options?.refresh

View File

@@ -26,8 +26,8 @@
"vite": "^7.2.6"
},
"dependencies": {
"@effect/platform": "^0.93.6",
"@effect/platform-browser": "^0.73.0",
"@effect/platform": "^0.94.0",
"@effect/platform-browser": "^0.74.0",
"@radix-ui/themes": "^3.2.1",
"@typed/id": "^0.17.2",
"effect": "^3.19.8",