Skip to content
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

Signal.State.prototype.update to update values using callbacks #249

Open
Ocean-OS opened this issue Dec 24, 2024 · 4 comments
Open

Signal.State.prototype.update to update values using callbacks #249

Ocean-OS opened this issue Dec 24, 2024 · 4 comments

Comments

@Ocean-OS
Copy link

Ocean-OS commented Dec 24, 2024

When using signals, often the value of a signal is not known, but a change needs to be made based on the current value. This pattern can be found in Svelte's stores, Solid's Signals, and React's useState, to name a few. This pattern leads to cleaner code with less clutter. Since using a callback in Signal.State.prototype.set is too ambiguous (setting the value to a function might be the preferred operation), it seems reasonable to add Signal.State.prototype.update. Here's some examples of how the update function would work:

let count = new Signal.State(0);
function increment() {
    count.update(c => c + 1);
}
function createCountdown(start, duration) {
    let countdown = new Signal.State(start);
    let interval = setInterval(()=>{
        countdown.update(c=> c - 1);
        if (countdown.get() === 0) clearInterval(interval);
    }, duration/start);
    return countdown;
}

function createTypewriter(text) {
    let typewriter = new Signal.State("");
    let chars = text.split('');

    function type(char) {
        return function() {
            typewriter.update(t => t + char);
        }
    };
    for (let index = 0; index < chars.length; index++) {
        setTimeout(type(chars[index]), 200 + index * 200);
    }
    return typewriter;
}
@NullVoxPopuli
Copy link
Collaborator

can you describe the situation in which the value can't be known? I don't understand

let known = count.get()

@Ocean-OS
Copy link
Author

can you describe the situation in which the value can't be known? I don't understand

let known = count.get()

By known, I mean a case where the developer can't just do this:

count.set(42); //or any other constant value

...because the value could be acquired through user input, fetching, etc.
The update function is primarily a cleaner way to do:

let updater = c => c+1;
count.set(updater(count.get()));

@NullVoxPopuli
Copy link
Collaborator

NullVoxPopuli commented Dec 25, 2024

...because the value could be acquired through user input, fetching, etc.

sorry I still don't understand this

is primarily a cleaner way to do

this I understand (and am ok with, tbh) -- but I'd like to know what " user input, fetching, etc." patterns need the updater.
I feel like I'm missing something :\

sorry im dense!

@Ocean-OS
Copy link
Author

this I understand (and am ok with, tbh) -- but I'd like to know what " user input, fetching, etc." patterns need the updater. I feel like I'm missing something :\

sorry im dense!

I meant like something where you'd have to use count.get() instead of a constant value, apologies for the bad explanation.
Example:

count.set(42); //constant ("known") value
count.set(count.get() + 1); //not "known" value

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

No branches or pull requests

2 participants