-
Notifications
You must be signed in to change notification settings - Fork 11
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
Comments
Perhaps similarly, I would find an AsyncWritableComputed that exposes a setValue/mutate function useful for optimistic or single flight mutations. |
@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. |
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 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])
}
} |
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 areset()
method to do this from outside the compute function. It should clear the current error and restore the initial value.The text was updated successfully, but these errors were encountered: