From 4e6c60b161064b828bb72d720170034d50246000 Mon Sep 17 00:00:00 2001 From: "omegent-app[bot]" <306514130+omegent-app[bot]@users.noreply.github.com> Date: Mon, 26 Oct 2026 09:08:02 +0000 Subject: [PATCH 1/2] fix(vue): release live-query discovery when a fetch is interrupted before ready Co-Authored-By: Claude Opus 5.5 --- .../live-query-interrupted-discovery.md | 5 ++ packages/vue/src/atomQuery.ts | 16 ++++--- packages/vue/src/liveQueryInvalidation.ts | 13 ++---- .../vue/test/liveQueryInvalidation.test.ts | 46 +++++++++++++++++-- 4 files changed, 62 insertions(+), 18 deletions(-) create mode 100644 .changeset/live-query-interrupted-discovery.md diff --git a/.changeset/live-query-interrupted-discovery.md b/.changeset/live-query-interrupted-discovery.md new file mode 100644 index 000000000..6b0338774 --- /dev/null +++ b/.changeset/live-query-interrupted-discovery.md @@ -0,0 +1,5 @@ +--- +"@effect-app/vue": patch +--- + +Stop buffering live-query writes forever when a live fetch is interrupted while waiting for the invalidation source to be ready. diff --git a/packages/vue/src/atomQuery.ts b/packages/vue/src/atomQuery.ts index e7dc7b290..40b15b72f 100644 --- a/packages/vue/src/atomQuery.ts +++ b/packages/vue/src/atomQuery.ts @@ -34,7 +34,7 @@ import * as Stream from "effect/Stream" import type * as Tracer from "effect/Tracer" import { clearQueryReadDependencies, getQueryReadDependencies, type QueryInvalidationMode, registerQueryInvalidationMode, setQueryReadDependencies } from "./dependencyMetadata.ts" import { reportRuntimeError } from "./lib.ts" -import { beginLiveQueryFetch, endLiveQueryFetch, type LiveQueryOptions, registerLiveQuery } from "./liveQueryInvalidation.ts" +import { acquireLiveQueryFetch, endLiveQueryFetch, type LiveQueryOptions, liveQueryReady, registerLiveQuery } from "./liveQueryInvalidation.ts" import { atomsToRefetch, observeQueryAtom } from "./queryLifetime.ts" /** All non-empty prefixes of a key, longest last. `[a,b,c]` -> `[[a],[a,b],[a,b,c]]`. */ @@ -503,14 +503,16 @@ export const buildQueryFamily = ( setQueryReadDependencies(fullKey, lastReads) return result }) - let liveFetch = false const effect = Effect - .gen(function*() { - liveFetch = yield* Effect.promise(() => beginLiveQueryFetch(fullKey)) - return yield* recordReads.pipe(Effect.retry({ times: 5, while: isRetryable })) - }) + .acquireUseRelease( + Effect.sync(() => acquireLiveQueryFetch(fullKey)), + (live) => + (live ? Effect.promise(liveQueryReady) : Effect.void).pipe( + Effect.andThen(recordReads.pipe(Effect.retry({ times: 5, while: isRetryable }))) + ), + (live) => Effect.sync(() => endLiveQueryFetch(live)) + ) .pipe( - Effect.ensuring(Effect.sync(() => endLiveQueryFetch(liveFetch))), Effect.tapCauseIf(Cause.hasDies, (cause) => reportRuntimeError(cause)), // On exit, the compute is no longer in-flight. An interrupt (subscriber lost interest / a // superseding refresh) may leave the result at `waiting`; with `inFlight` back at 0 that diff --git a/packages/vue/src/liveQueryInvalidation.ts b/packages/vue/src/liveQueryInvalidation.ts index 48fdefc29..5f71c205c 100644 --- a/packages/vue/src/liveQueryInvalidation.ts +++ b/packages/vue/src/liveQueryInvalidation.ts @@ -121,18 +121,15 @@ export const registerLiveQuery = ( } } -export const beginLiveQueryFetch = async (key: ReadonlyArray) => { +/** Synchronously marks a live fetch as discovering; pair with `endLiveQueryFetch` even if interrupted. */ +export const acquireLiveQueryFetch = (key: ReadonlyArray) => { if (!entries.has(Hash.hash(key)) || source === undefined) return false discovering++ - try { - await source.ready() - return true - } catch (error) { - discovering-- - throw error - } + return true } +export const liveQueryReady = () => source?.ready() ?? Promise.resolve() + export const endLiveQueryFetch = (wasLive: boolean) => { if (!wasLive) return discovering = Math.max(0, discovering - 1) diff --git a/packages/vue/test/liveQueryInvalidation.test.ts b/packages/vue/test/liveQueryInvalidation.test.ts index d807cda3c..2f51a0665 100644 --- a/packages/vue/test/liveQueryInvalidation.test.ts +++ b/packages/vue/test/liveQueryInvalidation.test.ts @@ -6,7 +6,7 @@ import * as ManagedRuntime from "effect/ManagedRuntime" import { afterEach, describe, expect, it, vi } from "vitest" import { awaitAtomResult, buildQueryFamily, invalidateAndAwait, invalidateSoft, makeAtomClientRuntime, withQueryOptions } from "../src/atomQuery.js" import { getQueryInvalidationMode } from "../src/dependencyMetadata.js" -import { beginLiveQueryFetch, configureLiveQueryInvalidation, endLiveQueryFetch, registerLiveQuery } from "../src/liveQueryInvalidation.js" +import { acquireLiveQueryFetch, configureLiveQueryInvalidation, endLiveQueryFetch, liveQueryReady, registerLiveQuery } from "../src/liveQueryInvalidation.js" const repo = DataDependencies.repo("Inventory", ["item-1"]) const otherId = DataDependencies.repo("Inventory", ["item-2"]) @@ -166,16 +166,56 @@ describe("live query invalidation", () => { let reads = DataDependencies.empty() cleanups.push(registerLiveQuery(key, () => reads, {})) - const starting = beginLiveQueryFetch(key) + const wasLive = acquireLiveQueryFetch(key) + const starting = liveQueryReady() receive(new Set([repo])) open() - const wasLive = await starting + await starting reads = new Set([repo]) endLiveQueryFetch(wasLive) expect(invalidations).toEqual([[key]]) }) + it("dispatches writes after an observed live query is interrupted while awaiting readiness", async () => { + const dependency = DataDependencies.repo("LiveOrders") + const mrt = ManagedRuntime.make(Layer.empty) + const rt = makeAtomClientRuntime(() => Layer.empty, mrt.memoMap) + const invalidations: Array>> = [] + let receive = (_writes: DataDependencies.DataDependencies) => {} + let readyCalled = () => {} + const readyRequested = new Promise((resolve) => readyCalled = resolve) + configureLiveQueryInvalidation({ + ready: () => { + readyCalled() + return new Promise(() => {}) + }, + subscribe: (onWrites) => { + receive = onWrites + return () => {} + }, + invalidate: (keys) => invalidations.push(keys) + }) + const query = { id: "LiveOrders.Pending", handler: () => DataDependencies.read(dependency) } + const base = buildQueryFamily(rt, query)(undefined) + const atom = withQueryOptions(base, { live: true, revalidateOnFocus: false }, [...makeQueryKey(query), undefined]) + try { + const unmount = defaultRegistry.mount(atom) + await readyRequested + unmount() + defaultRegistry.reset() + await new Promise((resolve) => setTimeout(resolve, 0)) + + const key = ["$LiveOrders", "Other", undefined] + cleanups.push(registerLiveQuery(key, () => new Set([dependency]), {})) + receive(new Set([dependency])) + expect(invalidations).toEqual([[key]]) + } finally { + defaultRegistry.reset() + await mrt.dispose() + } + }) + it("coalesces matching writes using the query's maximum delay", () => { vi.useFakeTimers() const invalidations: Array>> = [] From 9fa372878a629f12d40a745c56b97cb46505a0af Mon Sep 17 00:00:00 2001 From: "omegent-app[bot]" <306514130+omegent-app[bot]@users.noreply.github.com> Date: Mon, 26 Oct 2026 09:15:43 +0000 Subject: [PATCH 2/2] fix(vue): keep deprecated beginLiveQueryFetch export Co-Authored-By: Claude Opus 5.5 --- packages/vue/src/liveQueryInvalidation.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/vue/src/liveQueryInvalidation.ts b/packages/vue/src/liveQueryInvalidation.ts index 5f71c205c..7197963e3 100644 --- a/packages/vue/src/liveQueryInvalidation.ts +++ b/packages/vue/src/liveQueryInvalidation.ts @@ -130,6 +130,18 @@ export const acquireLiveQueryFetch = (key: ReadonlyArray) => { export const liveQueryReady = () => source?.ready() ?? Promise.resolve() +/** @deprecated Interruptible between acquire and ready; use `acquireLiveQueryFetch` + `liveQueryReady`. */ +export const beginLiveQueryFetch = async (key: ReadonlyArray) => { + if (!acquireLiveQueryFetch(key)) return false + try { + await liveQueryReady() + return true + } catch (error) { + discovering-- + throw error + } +} + export const endLiveQueryFetch = (wasLive: boolean) => { if (!wasLive) return discovering = Math.max(0, discovering - 1)