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

This commit is contained in:
Julien Valverdé
2025-03-21 01:23:47 +01:00
parent 0f09573948
commit d82d1d1c29
8 changed files with 29 additions and 25 deletions

View File

@@ -1,36 +1,37 @@
import * as AsyncData from "@typed/async-data"
import { Effect, flow, Layer, Match, Option } from "effect"
import * as QueryState from "./QueryState.js"
import { QueryState } from "./internal/index.js"
export class QueryProgress extends Effect.Tag("@reffuse/extension-query/QueryProgress")<QueryProgress, {
readonly get: Effect.Effect<Option.Option<AsyncData.Progress>, never, QueryState.QueryState<unknown, unknown>>
readonly get: Effect.Effect<Option.Option<AsyncData.Progress>>
readonly update: (
f: (previous: Option.Option<AsyncData.Progress>) => AsyncData.Progress
) => Effect.Effect<void, never, QueryState.QueryState<unknown, unknown>>
) => Effect.Effect<void>
}>() {
static readonly Live = Layer.sync(this, () => {
const queryStateTag = QueryState.makeTag()
static readonly Live: Layer.Layer<
QueryProgress,
never,
QueryState.QueryState<any, any>
> = Layer.effect(this, Effect.gen(function*() {
const state = yield* QueryState.makeTag()
const get = queryStateTag.pipe(
Effect.flatMap(state => state.get),
const get = state.get.pipe(
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)
))
Effect.flatMap(progress => state.update(previous =>
AsyncData.updateProgress(previous, progress)
)),
)
return { get, update }
})
}))
}