Skip to content

Conversation

@kitlangton
Copy link
Contributor

Summary

  • Fix bug where Atom.kvs would overwrite existing localStorage data during the Initial state
  • The default value was being written before the async load completed, corrupting stored values
  • Values would reset on page reload because the default overwrote the persisted value

Changes

  • Add Result.isSuccess(result) check before writing default to storage
  • Only write default when async load completed and found no existing value
  • Add test that verifies storage isn't corrupted during async loading

Test plan

  • New test: "preserves existing value after async load completes"
  • All existing tests pass (88 tests)

@kitlangton kitlangton force-pushed the fix/kvs-initial-state-overwrite branch 3 times, most recently from e0b19e8 to 1e6b2ed Compare January 13, 2026 21:53
The kvs atom was writing the default value to storage during the Initial
state, before the async load completed. This corrupted any existing stored
data, causing values to reset on page reload.

The fix removes the code that wrote the default to storage. The original
code had a `Result.isSuccess` check around the write, but since
Effect.flatten unwraps the Option from KeyValueStore.get, Success always
contains a value and we'd return early - making that write unreachable.

The Result.value() + Option.isSome() pattern is preserved to correctly
handle previousSuccess when a refresh fails.

Includes test that verifies storage isn't corrupted by the default value
during async loading.
@kitlangton kitlangton force-pushed the fix/kvs-initial-state-overwrite branch from 1e6b2ed to 3e6e4bd Compare January 13, 2026 22:07
@kitlangton kitlangton marked this pull request as ready for review January 13, 2026 22:12
@kitlangton
Copy link
Contributor Author

I ran into this issue a few times on https://effect.institute

return value.value
}
const defaultValue = options.defaultValue()
get.set(setAtom, defaultValue)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It needs to be set in some way, as the defaultValue is lazy and should only be evaluated once.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, of course!

kitlangton and others added 2 commits January 13, 2026 20:41
Cache the default value in atom state so it is evaluated once while the store is loading. Add a regression test to cover memoization behavior.
@tim-smart tim-smart merged commit 8deb37b into tim-smart:main Jan 14, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants