@@ -53,7 +53,7 @@ extends Pipeable.Class implements Query<K, A, E, R> {
|
||||
|
||||
constructor(
|
||||
readonly context: Context.Context<Scope.Scope | QueryClient.QueryClient | R>,
|
||||
readonly key: Lens.Lens<K>,
|
||||
readonly key: Subscribable.Subscribable<K>,
|
||||
readonly keyEquivalence: Equivalence.Equivalence<K>,
|
||||
readonly f: (key: K) => Effect.Effect<A, E, R>,
|
||||
|
||||
@@ -71,15 +71,23 @@ extends Pipeable.Class implements Query<K, A, E, R> {
|
||||
|
||||
get run(): Effect.Effect<void> {
|
||||
return Effect.all([
|
||||
Stream.runFoldEffect(
|
||||
Stream.runForEach(
|
||||
this.key.changes,
|
||||
() => Option.none<K>(),
|
||||
(previous, key) => Effect.as(
|
||||
Option.isSome(previous) && this.keyEquivalence(key, previous.value)
|
||||
? this.refreshSubscribable
|
||||
: this.fetchSubscribable(key),
|
||||
Option.some(key),
|
||||
),
|
||||
key => Effect.gen({ self: this }, function*() {
|
||||
yield* this.interrupt
|
||||
const latestFinalState = yield* Lens.get(this.latestFinalState)
|
||||
|
||||
const state = yield* this.startCached(
|
||||
Option.isSome(latestFinalState) && this.keyEquivalence(key, latestFinalState.value.key)
|
||||
? latestFinalState.value
|
||||
: {
|
||||
key,
|
||||
result: AsyncResult.initial(false),
|
||||
}
|
||||
)
|
||||
|
||||
yield* Effect.forkScoped(this.watch(state))
|
||||
}),
|
||||
),
|
||||
|
||||
Effect.promise(() => import("@effect/platform-browser")).pipe(
|
||||
@@ -109,8 +117,6 @@ extends Pipeable.Class implements Query<K, A, E, R> {
|
||||
fetch(key: K): Effect.Effect<FinalQueryState<K, A, E>, Cause.NoSuchElementError> {
|
||||
return Effect.gen({ self: this }, function*() {
|
||||
yield* this.interrupt
|
||||
yield* Lens.set(this.key, key)
|
||||
|
||||
const state = yield* this.startCached({
|
||||
key,
|
||||
result: AsyncResult.initial(false),
|
||||
@@ -127,8 +133,6 @@ extends Pipeable.Class implements Query<K, A, E, R> {
|
||||
> {
|
||||
return Effect.gen({ self: this }, function*() {
|
||||
yield* this.interrupt
|
||||
yield* Lens.set(this.key, key)
|
||||
|
||||
const state = yield* this.startCached({
|
||||
key,
|
||||
result: AsyncResult.initial(false),
|
||||
@@ -144,14 +148,14 @@ extends Pipeable.Class implements Query<K, A, E, R> {
|
||||
get refresh(): Effect.Effect<FinalQueryState<K, A, E>, Cause.NoSuchElementError> {
|
||||
return Effect.gen({ self: this }, function*() {
|
||||
yield* this.interrupt
|
||||
const latestKey = yield* Lens.get(this.key)
|
||||
const latestState = yield* Lens.get(this.state)
|
||||
const latestFinalState = yield* Lens.get(this.latestFinalState)
|
||||
|
||||
const state = yield* this.startCached(
|
||||
Option.isSome(latestFinalState) && this.keyEquivalence(latestKey, latestFinalState.value.key)
|
||||
Option.isSome(latestFinalState) && this.keyEquivalence(latestState.key, latestFinalState.value.key)
|
||||
? latestFinalState.value
|
||||
: {
|
||||
key: latestKey,
|
||||
key: latestState.key,
|
||||
result: AsyncResult.initial(false),
|
||||
}
|
||||
)
|
||||
@@ -168,14 +172,14 @@ extends Pipeable.Class implements Query<K, A, E, R> {
|
||||
> {
|
||||
return Effect.gen({ self: this }, function*() {
|
||||
yield* this.interrupt
|
||||
const latestKey = yield* Lens.get(this.key)
|
||||
const latestState = yield* Lens.get(this.state)
|
||||
const latestFinalState = yield* Lens.get(this.latestFinalState)
|
||||
|
||||
const state = yield* this.startCached(
|
||||
Option.isSome(latestFinalState) && this.keyEquivalence(latestKey, latestFinalState.value.key)
|
||||
Option.isSome(latestFinalState) && this.keyEquivalence(latestState.key, latestFinalState.value.key)
|
||||
? latestFinalState.value
|
||||
: {
|
||||
key: latestKey,
|
||||
key: latestState.key,
|
||||
result: AsyncResult.initial(false),
|
||||
}
|
||||
)
|
||||
@@ -363,7 +367,7 @@ extends Pipeable.Class implements Query<K, A, E, R> {
|
||||
|
||||
export declare namespace make {
|
||||
export interface Options<K, A, E = never, R = never> {
|
||||
readonly key: Lens.Lens<K>,
|
||||
readonly key: Subscribable.Subscribable<K>,
|
||||
readonly keyEquivalence?: Equivalence.Equivalence<K>,
|
||||
readonly f: (key: K) => Effect.Effect<A, E, R>
|
||||
|
||||
@@ -392,7 +396,7 @@ export const make = Effect.fnUntraced(function* <K, A, E = never, R = never>(
|
||||
|
||||
Lens.fromSubscriptionRef(yield* SubscriptionRef.make(Option.none<Fiber.Fiber<A, E>>())),
|
||||
Lens.fromSubscriptionRef(yield* SubscriptionRef.make<QueryState<K, A, E>>({
|
||||
key: yield* Lens.get(options.key),
|
||||
key: yield* options.key.get,
|
||||
result: AsyncResult.initial(false),
|
||||
})),
|
||||
Lens.fromSubscriptionRef(yield* SubscriptionRef.make(Option.none<FinalQueryState<K, A, E>>())),
|
||||
|
||||
Reference in New Issue
Block a user