QueryProgress
All checks were successful
Lint / lint (push) Successful in 14s

This commit is contained in:
Julien Valverdé
2025-03-19 05:13:54 +01:00
parent f21d8b2d8a
commit 5d0aecc9d5
3 changed files with 59 additions and 4 deletions

View File

@@ -1,7 +1,36 @@
import type * as AsyncData from "@typed/async-data"
import { Effect, type Option } from "effect"
import * as AsyncData from "@typed/async-data"
import { Effect, flow, Layer, Match, Option } from "effect"
import * as QueryState from "./QueryState.js"
export class QueryProgress extends Effect.Tag("@reffuse/extension-query/QueryProgress")<QueryProgress, {
readonly set: (previous: Option.Option<AsyncData.Progress>) => Effect.Effect<void>
}>() {}
readonly get: Effect.Effect<Option.Option<AsyncData.Progress>, never, QueryState.QueryState<unknown, unknown>>
readonly update: (
f: (previous: Option.Option<AsyncData.Progress>) => AsyncData.Progress
) => Effect.Effect<void, never, QueryState.QueryState<unknown, unknown>>
}>() {
static readonly Live = Layer.sync(this, () => {
const queryStateTag = QueryState.makeTag()
const get = queryStateTag.pipe(
Effect.flatMap(state => state.get),
Effect.map(flow(Match.value,
Match.tag("Loading", v => v.progress),
Match.tag("Refreshing", v => v.progress),
Match.orElse(() => Option.none()),
)),
)
const update = (f: (previous: Option.Option<AsyncData.Progress>) => AsyncData.Progress) => get.pipe(
Effect.map(f),
Effect.flatMap(progress => queryStateTag.pipe(
Effect.flatMap(queryState => queryState.update(previous =>
AsyncData.updateProgress(previous, progress)
))
)),
)
return { get, update }
})
}