-
Notifications
You must be signed in to change notification settings - Fork 59
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
Convenience methods #32
Comments
From another discussion: There's a question about whether or not we should have a way to read a value if it's a signal or a value... or make sure it's a signal first etc. Similar to The use case would be something like a component that accepted a property that could either be a signal or a plain value. Let's say the author wants to encourage the use of signals for managing dynamically changing state, but the state doesn't always need to dynamically change. For example with a "layout component". |
potentially also, Demo / behaviors here: (some context: https://twitter.com/nullvoxpopuli/status/1770862533378707571 ) |
|
I think wee need |
I have a utility that deals in a single signal and needs to peek it. Having a |
FWIWI I've resolved the class State extends Signal.State {
peek() {
return untrack(() => this.get());
}
} The If |
@EisenbergEffect Yes, I wasn't suggesting that But sometimes all you need is reading one signal and |
@jods4 Preact has untrack too so there is also previous work around this difference which is very important. Accordingly, I wouldn't mix the two use-cases or needs, edit also let's keep in mind this API proposal is for libraries authors ... the DX argument is hence weak as long as the desired result is achievable and |
@WebReflection that's exactly what I said, not sure how you understood my previous messages.
Yes, anyone can build On the other hand, built-in I'm not sure that's a lot of feature creep, especially considering that most (all?) "signal" implementation for the past 10 years have exposed a peek in their core set of features. |
checking internals of the polyfill, it could indeed just return the internal symbol value and avoid extra stacks. Apologies I've misread your previous comment, I've thought you preferred |
Great conversation here! Thanks for the clarification @WebReflection and @jods4! |
@EisenbergEffect I feel like that's a stretch too far ... or better, I don't understand the use case for it, it feels surprise prone to me as "a sneaky update nobody should notice" and that to me won't lead to better code ... if we take previous art work around the topic, I am not sure which library exposes that to date but if they do, it's also easily an attack vector to me if these libraries land in undesired places where untrusted code starts poking around foreign code ... it's chaos easy to generate, while edit to some extend, think |
@EisenbergEffect RE "Triggering" computeds or bindings even though they "have not changed" is a topic that comes up quite often in UI frameworks. It can often be worked around with better designs or by introducing missing signals (even just a counter that's incremented when a refresh is desired). |
agreed and on a second though, |
do we have a list of these? would be good for a "guidance" and "recommendations" section of the docs / proposal, because there are a lot of ways to shoot one's self in the foot with reactive systems. May be of interest: |
Thanks everyone! I was pretty sure poke was a bad idea and I couldn’t think of any use cases, but wanted to make sure I wasn’t missing something. |
just the fact one can send errors or debug on Promise catch would be enough to me, as you never want to subscribe to those callbacks fetch(remote.get()).catch(error => {
untrack(() => {
console.error(`Unable to fetch ${remote.get()}`, error);
});
}); Sure thing for that specific use case |
I don't think callbacks like that are inherently subscribed to, and untrack wouldn't be needed 🤔 i am about to verify this tho in More info soon! |
Yeah my feeling is that events/microtask queue items would have a null currentComputed by default, and just not be tracking until you go into a Computed. |
my example was poorly presented ... I meant that whenever signals or computed are around and you want to:
I can do a quick search in Preact repo though as I am sure they had various reasons/requests/discussions aroud this topic, if that helps anyone (or users wanting to understand when To my DOM examples based on signals, there are cases I need to retrieve a value in sync with the rest of the flow but without subscribing the whole thing to my introspection logic, so this is another use case ... libraries' authors will need, and want, a way to not subscribe for foreign data they don't understand where it comes from or how frequently it could update. edit ... I mean, without going too far from my own repositories I recently had complains about More requests around This is still my own stuff, which was fully inspired by Preact signals/core |
If you're fishing for peek / untrack use cases: Very recently I wanted to capture the initial state of reactive objects, to be able to provide a "dirty" flag. This happened in a computed that "initialised" the transactional object from the currently bound object. You can also dig into Vue Vue also exposes I previously mentionned that Knockout has had |
Here is a userland implementation of |
@littledan We've had the request for |
my 2c: explicit |
Should the Signal API include any convenience methods? Possibilities:
Signal.isSignal
-- brand checkSignal.State.prototype.update
-- read/run/writeSignal.State.prototype.toReadonly
-- make a computed signal out of a state signal, to drop the capability to write to itSignal.Effect
that schedules itself (badly)Would any other convenience methods be useful? The current API omits all of these on purpose, with the goal of being minimal.
The text was updated successfully, but these errors were encountered: