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

AsyncComputed: no way to return to initial state #86

Open
justinfagnani opened this issue Oct 9, 2024 · 3 comments
Open

AsyncComputed: no way to return to initial state #86

justinfagnani opened this issue Oct 9, 2024 · 3 comments

Comments

@justinfagnani
Copy link
Contributor

Once an async computed task has been executed, there is no way to return it to it's "initial" state.

In Lit's Task we provide an initialState sentinel value that a compute function can return to put the Task back into the initial state. It might also be nice to have a reset() method to do this from outside the compute function. It should clear the current error and restore the initial value.

@danielrkling
Copy link

Perhaps similarly, I would find an AsyncWritableComputed that exposes a setValue/mutate function useful for optimistic or single flight mutations.

@justinfagnani
Copy link
Contributor Author

@danielrkling can you expand on the use case there?

One of the main differences between AsyncComputed and an interface like Relays is that the compute function completely determines the current state of the AsyncComputed instance, just like with a Signal.Computed. External mutations shouldn't be possible, and if something like that is needed I think it's best if it's structured as a state signal used by the compute function that only certain actors get a reference to, not all consumers of the AsyncComputed.

@danielrkling
Copy link

I'm not sure I'm understanding the Relay pattern in the article linked in the pinned features issue https://www.pzuraq.com/blog/on-signal-relays so I'll just describe what I am currently doing, and maybe that is more of a Relay, but it seems more like AsyncComputed + Latest
Using my own version of AsyncComputed with a setValue function.

export const todos = new MyAsyncComputed(()=>fetchTodos(),{initialValue: []})

export async function addTodo(todo){
    //this api call returns the newly created todo with extra properties from the server that i want added to the array
    fetch('addTodo',{method:"POST", body: JSON.stringify(todo)}).then(result=>{
       const newTodo = parseResult()
       todos.setValue([...todos.value,newTodo])
    }
}

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