Skip to content

Ensures observers receive correct initial value #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 17, 2025

Conversation

simonbs
Copy link
Contributor

@simonbs simonbs commented Mar 17, 2025

Description

The changes in this PR ensure that observers installed with spiceStore.$myValue.sink { ... } receive the correct value upon subscribing rather than the default value of the spice.

The value can only be provided when the spice store has been prepared, which happens automatically when reading or writing the value of a spice (e.g., _ = spiceStore.myValue or spiceStore.myValue = newValue), but not when accessing the projected value (spiceStore.$myValue).

As a result, prepareIfNeeded() has been made public, enabling users of Spices to call the function prior to setting up an observer. This is only needed once.

In order to enforce that prepareIfNeeded() is called, projectedValue on a Spice will assert if the underlying storage has not been prepared.

Motivation and Context

Prior to these changes, observers set up with spiceStore.$myValue would receive an incorrect value upon subscribing.

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

@simonbs simonbs merged commit 49bb66c into main Mar 17, 2025
9 checks passed
@simonbs simonbs deleted the bugfix/initial-observed-value branch March 17, 2025 13:35
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.

1 participant