Fix Atom.kvs showing a value from before the last write after a refresh or remount (#10613)

Co-authored-by: Effect Bot <tech-ops@effectful.co>
This commit is contained in:
Ibrahim Elkamali
2026-11-16 01:25:05 +00:00
committed by GitHub
co-authored by Effect Bot
parent 9f00fd5f83
commit eecfff5048
3 changed files with 44 additions and 5 deletions
+5
View File
@@ -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.
+16 -5
View File
@@ -2335,18 +2335,26 @@ export const kvs = <S extends Schema.ConstraintCodec<any, any>, const Mode exten
readonly mode?: Mode | undefined
}): Writable<"async" extends Mode ? AsyncResult.AsyncResult<S["Type"]> : 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<Option.Option<S["Type"]>, S["Type"]>(
Option.getOrElse(() => {
const value = options.defaultValue()
@@ -2355,11 +2363,12 @@ export const kvs = <S extends Schema.ConstraintCodec<any, any>, 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<Option.Option<S["Type"]>, unknown>) => {
if (!AsyncResult.isSuccess(result)) return
if (Option.isSome(result.value)) {
get.setSelf(result.value.value)
@@ -2368,7 +2377,9 @@ export const kvs = <S extends Schema.ConstraintCodec<any, any>, 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<S["Type"]>(), options.defaultValue)
},
(ctx, value: S["Type"]) => {
@@ -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()
})
}
})
})