From eecfff5048ce64fbbfeb80ea078098bf0b2a8885 Mon Sep 17 00:00:00 2001 From: Ibrahim Elkamali Date: Mon, 16 Nov 2026 04:25:05 +0300 Subject: [PATCH] Fix Atom.kvs showing a value from before the last write after a refresh or remount (#10613) Co-authored-by: Effect Bot --- .changeset/atom-kvs-stale-refresh.md | 5 +++++ packages/effect/src/reactivity/Atom.ts | 21 +++++++++++++----- packages/effect/test/reactivity/Atom.test.ts | 23 ++++++++++++++++++++ 3 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 .changeset/atom-kvs-stale-refresh.md diff --git a/.changeset/atom-kvs-stale-refresh.md b/.changeset/atom-kvs-stale-refresh.md new file mode 100644 index 0000000000..cd7fc790ad --- /dev/null +++ b/.changeset/atom-kvs-stale-refresh.md @@ -0,0 +1,5 @@ +--- +"effect": patch +--- + +Fix `Atom.kvs` showing the value from before its last write after being refreshed or remounted past its idle TTL. diff --git a/packages/effect/src/reactivity/Atom.ts b/packages/effect/src/reactivity/Atom.ts index a6c6d00170..be46f67373 100644 --- a/packages/effect/src/reactivity/Atom.ts +++ b/packages/effect/src/reactivity/Atom.ts @@ -2335,18 +2335,26 @@ export const kvs = , const Mode exten readonly mode?: Mode | undefined }): Writable<"async" extends Mode ? AsyncResult.AsyncResult : S["Type"], S["Type"]> => { const setAtom = options.runtime.fn( - (value: S["Type"]) => + (value: S["Type"], get) => KeyValueStore.KeyValueStore.use((store) => - KeyValueStore.toSchemaStore(store, options.schema).set(options.key, value) + Effect.map( + KeyValueStore.toSchemaStore(store, options.schema).set(options.key, value), + () => get.set(persistedAtom, value) + ) ) ) const resultAtom = options.runtime.atom( KeyValueStore.KeyValueStore.use((store) => KeyValueStore.toSchemaStore(store, options.schema).get(options.key)) ) + const persistedAtom = writable( + (get) => get(resultAtom), + (ctx, value: S["Type"]) => ctx.setSelf(AsyncResult.success(Option.some(value))) + ) return writable( options.mode === "async" ? (get) => { get.mount(setAtom) + get.mount(persistedAtom) const mapper = AsyncResult.map, S["Type"]>( Option.getOrElse(() => { const value = options.defaultValue() @@ -2355,11 +2363,12 @@ export const kvs = , const Mode exten }) ) get.subscribe(resultAtom, (result) => get.setSelf(mapper(result))) - return mapper(get.once(resultAtom)) + return mapper(get.once(persistedAtom)) } : (get) => { get.mount(setAtom) - get.subscribe(resultAtom, (result) => { + get.mount(persistedAtom) + const update = (result: AsyncResult.AsyncResult, unknown>) => { if (!AsyncResult.isSuccess(result)) return if (Option.isSome(result.value)) { get.setSelf(result.value.value) @@ -2368,7 +2377,9 @@ export const kvs = , const Mode exten get.setSelf(value) get.set(setAtom, value) } - }, { immediate: true }) + } + get.subscribe(resultAtom, update) + update(get.once(persistedAtom)) return Option.getOrElse(get.self(), options.defaultValue) }, (ctx, value: S["Type"]) => { diff --git a/packages/effect/test/reactivity/Atom.test.ts b/packages/effect/test/reactivity/Atom.test.ts index dcff57039b..33d808badf 100644 --- a/packages/effect/test/reactivity/Atom.test.ts +++ b/packages/effect/test/reactivity/Atom.test.ts @@ -3602,6 +3602,29 @@ describe("Atom", { concurrent: false }, () => { assert(AsyncResult.isSuccess(updated)) expect(updated.value).toEqual(24) }) + + for (const mode of ["sync", "async"] as const) { + it(`${mode} mode retains persisted writes across refresh`, () => { + const atom = Atom.kvs({ + mode, + runtime: Atom.runtime(KeyValueStore.layerMemory.pipe( + Layer.tap((services) => + Context.get(services, KeyValueStore.KeyValueStore).set("theme", JSON.stringify("light")) + ) + )), + key: "theme", + schema: Schema.String, + defaultValue: () => "light" + }) + const r = AtomRegistry.make() + r.mount(atom) + r.set(atom, "dark") + r.refresh(atom) + const value = r.get(atom) + assert.strictEqual(AsyncResult.isAsyncResult(value) ? AsyncResult.getOrThrow(value) : value, "dark") + r.dispose() + }) + } }) })