Skip to content

Fixed computed.peek to get the latest value#40

Merged
WebReflection merged 3 commits intoWebReflection:mainfrom
theJian:fix/computed-peek-value
May 16, 2025
Merged

Fixed computed.peek to get the latest value#40
WebReflection merged 3 commits intoWebReflection:mainfrom
theJian:fix/computed-peek-value

Conversation

@theJian
Copy link
Contributor

@theJian theJian commented May 14, 2025

computed.peek is not returning the latest value.

const counter = signal(0);
const doubleCounter = computed(() => counter.value * 2)

console.log(doubleCounter.peek()) // print undefined, computed callback was not executed due to laziness.
console.log(doubleCounter.value) // print 0

counter.value = 2
console.log(doubleCounter.peek()) // print 0, again, a stale value from last update is returned due to laziness.

this can be fixed by refreshing the internal signal's value before peek() into it.

@WebReflection
Copy link
Owner

beside the need to update Node version to 22 or 24 ... this feels like a peek() with side-effects ... are we sure that's desired?

anyway, I need CI green or I am afraid I can't merge this ... please change Node version and change checkouts to v4 within this MR, thank you!

@theJian
Copy link
Contributor Author

theJian commented May 15, 2025

this feels like a peek() with side-effects ... are we sure that's desired?

if you mean a peek() can trigger an immediate evaluation, I tried @preact/signals-core and it does the same.
That feels natural to me since peek() is essentially trying to access the value, do you think of other scenarios.

@WebReflection
Copy link
Owner

dunno ... these days I am more keen to use something like this instead https://github.com/WebReflection/alien-signals#readme so if you think that's expected/desired and both alien-signals and preact signals work the same ... works for me.

@coveralls
Copy link

Pull Request Test Coverage Report for Build 15044707032

Details

  • 20 of 20 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 100.0%

Totals Coverage Status
Change from base Build 11435340504: 0.0%
Covered Lines: 342
Relevant Lines: 342

💛 - Coveralls

@theJian
Copy link
Contributor Author

theJian commented May 16, 2025

Yes, I tried that too, and it does the same.

@WebReflection WebReflection merged commit d79555d into WebReflection:main May 16, 2025
1 check 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.

3 participants