fix(core): commit state before finalize publishes (#38983)

This commit is contained in:
Kit Langton 2026-07-27 11:04:04 -04:00 committed by GitHub
commit 1f2c59a1b6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 3 deletions

View file

@ -36,6 +36,25 @@ describe("State", () => {
}),
)
it.effect("commits rebuilt state before finalize runs", () =>
Effect.gen(function* () {
const observed: string[][] = []
const state: State.Interface<{ values: string[] }, { add: (item: string) => void }> = State.create({
initial: () => ({ values: [] as string[] }),
draft: (draft) => ({ add: (item: string) => draft.values.push(item) }),
finalize: () => Effect.sync(() => observed.push([...state.get().values])),
})
yield* state.transform((draft) => {
draft.add("value")
})
// Update events publish from finalize, so consumers reading on the event
// must observe the rebuilt state, not the previous one.
expect(observed).toEqual([["value"]])
}),
)
it.effect("runs transforms during every reload", () =>
Effect.gen(function* () {
let value = "first"