WebReflection / usignal

A blend of @preact/signals-core and solid-js basic reactivity API
ISC License
220 stars 15 forks source link

Computed value is undefined #36

Closed dy closed 8 months ago

dy commented 8 months ago

@WebReflection please correct me what I do wrong or misunderstand:

import { signal, computed } from "usignal";

const c = signal(0);
const d = computed(() => c.value * 2);
console.log(d.peek()); // undefined

sandbox

It doesn't seem to be the case with @preact/signals-core.

dy commented 8 months ago

In particular I hoped to use it in untracked function to allow sub-effects const untracked = fn => computed(fn).peek()

WebReflection commented 8 months ago

without effects around and nobody subscribing via .value I don't know why you would need a computed instead of a regular fn() returned value but I've kinda moved to https://github.com/WebReflection/signal when reactivity is minimal and use either preact core or solid signals for more battle-tested solutions, see uhtml/preactive.

I should probably sunset this module as I don't have time, nor care much these days, about it and I won't likely fix anything in the near future.

dy commented 8 months ago

One use case I just showed. @webreflection/signal has the same issue. Main reason why I wanted to use usignal over preact-signals is the size - it's twice smaller.

Do you have an idea how to make untracked function with usignal? In preact/signals it's exported by default.

If you prefer I can check out the source and propose a PR

WebReflection commented 8 months ago

PR welcome, I hope the code in ./esm/ folder is eaasy enough to follow, thanks!

titoBouzout commented 8 months ago

@WebReflection do you have any signals implementation that you actually maintain?

dy commented 8 months ago

@titoBouzout the @webreflection/signal is up-to-date & well-made. Also check out ulive - probably the most minimal signals implementation.

usignal only needs untracked and effect teardown to be in-sync.

titoBouzout commented 8 months ago

Thanks! @dy one of the issues with all this small signal libraries is that making anything that isn't trivial requires a Context API, if they don't expose an owner, with parents and a runWithOwner it becomes impossible to implement.

dy commented 8 months ago

Yeah, if you're talking about react/preact I'd suggest @preact/signals or @preact/signals-react is the right way to avoid footguns and wasted time, but to be honest signals don't shine there - they get mixed up with hooks and it becomes a mess. But as a standalone reactive primitive or foundation for a framework they're brilliant.

titoBouzout commented 8 months ago

I do not use react/preact, I come from a solidjs background, see https://pota.quack.uy/

WebReflection commented 8 months ago

uhtml/preactive and uhtml/signal give you that parent owner if I understood your concern ... as you always need a function to render (when you want nested/confined reactivity) or skip that to render parent on changes within nested components.