diff --git a/packages/effect-fc/src/Mutation.ts b/packages/effect-fc/src/Mutation.ts index 0eaccdd..cec9b54 100644 --- a/packages/effect-fc/src/Mutation.ts +++ b/packages/effect-fc/src/Mutation.ts @@ -16,6 +16,7 @@ extends Pipeable.Pipeable { readonly latestKey: Subscribable.Subscribable> readonly fiber: Subscribable.Subscribable>> readonly result: Subscribable.Subscribable> + readonly latestFinalResult: Subscribable.Subscribable>> mutate(key: K): Effect.Effect> mutateSubscribable(key: K): Effect.Effect>> @@ -37,20 +38,23 @@ extends Pipeable.Class() implements Mutation { readonly latestKey: SubscriptionRef.SubscriptionRef>, readonly fiber: SubscriptionRef.SubscriptionRef>>, readonly result: SubscriptionRef.SubscriptionRef>, + readonly latestFinalResult: SubscriptionRef.SubscriptionRef>>, ) { super() } mutate(key: K): Effect.Effect> { return SubscriptionRef.set(this.latestKey, Option.some(key)).pipe( - Effect.andThen(Effect.provide(this.start(key), this.context)), + Effect.andThen(this.start(key)), Effect.andThen(sub => this.watch(sub)), + Effect.provide(this.context), ) } mutateSubscribable(key: K): Effect.Effect>> { - return Effect.andThen( - SubscriptionRef.set(this.latestKey, Option.some(key)), - Effect.provide(this.start(key), this.context) + return SubscriptionRef.set(this.latestKey, Option.some(key)).pipe( + Effect.andThen(this.start(key)), + Effect.tap(sub => Effect.forkScoped(this.watch(sub))), + Effect.provide(this.context), ) } @@ -59,12 +63,8 @@ extends Pipeable.Class() implements Mutation { never, Scope.Scope | R > { - return this.result.pipe( - Effect.map(previous => Result.isFinal(previous) - ? previous - : undefined - ), - Effect.andThen(previous => Result.unsafeForkEffect( + return this.latestFinalResult.pipe( + Effect.andThen(initial => Result.unsafeForkEffect( Effect.onExit(this.f(key), () => Effect.andThen( Effect.all([Effect.fiberId, this.fiber]), ([currentFiberId, fiber]) => Option.match(fiber, { @@ -76,8 +76,8 @@ extends Pipeable.Class() implements Mutation { )), { + initial: Option.isSome(initial) ? Result.willFetch(initial.value) : Result.initial(), initialProgress: this.initialProgress, - previous, } as Result.unsafeForkEffect.Options, )), Effect.tap(([, fiber]) => SubscriptionRef.set(this.fiber, Option.some(fiber))), @@ -88,14 +88,14 @@ extends Pipeable.Class() implements Mutation { watch( sub: Subscribable.Subscribable> ): Effect.Effect> { - return Effect.andThen( - sub.get, - initial => Stream.runFoldEffect( - Stream.filter(sub.changes, Predicate.not(Result.isInitial)), + return sub.get.pipe( + Effect.andThen(initial => Stream.runFoldEffect( + sub.changes, initial, (_, result) => Effect.as(SubscriptionRef.set(this.result, result), result), - ), - ) as Effect.Effect> + ) as Effect.Effect>), + Effect.tap(result => SubscriptionRef.set(this.latestFinalResult, Option.some(result))), + ) } } @@ -123,5 +123,6 @@ export const make = Effect.fnUntraced(function* ()), yield* SubscriptionRef.make(Option.none>()), yield* SubscriptionRef.make(Result.initial()), + yield* SubscriptionRef.make(Option.none>()), ) }) diff --git a/packages/effect-fc/src/Query.ts b/packages/effect-fc/src/Query.ts index 361c1de..0095f42 100644 --- a/packages/effect-fc/src/Query.ts +++ b/packages/effect-fc/src/Query.ts @@ -203,7 +203,7 @@ extends Pipeable.Class() implements Query { ): Effect.Effect> { return sub.get.pipe( Effect.andThen(initial => Stream.runFoldEffect( - Stream.filter(sub.changes, Predicate.not(Result.isInitial)), + sub.changes, initial, (_, result) => Effect.as(SubscriptionRef.set(this.result, result), result), ) as Effect.Effect>), diff --git a/packages/example/src/routes/query.tsx b/packages/example/src/routes/query.tsx index d7ad441..3a35634 100644 --- a/packages/example/src/routes/query.tsx +++ b/packages/example/src/routes/query.tsx @@ -28,7 +28,7 @@ const ResultView = Component.makeUntraced("Result")(function*() { Effect.andThen(response => response.json), Effect.andThen(Schema.decodeUnknown(Post)), ), - staleTime: "1 minutes", + staleTime: "10 seconds", }) const mutation = yield* Mutation.make({ @@ -86,7 +86,6 @@ const ResultView = Component.makeUntraced("Result")(function*() { -