2.0.0-rc.0 createOptimisticStore works inconsistently/unintuitively
#3032
Replies: 6 comments
This is because you don't have a sleep on the toggle,updateName endpoints so it happens instantly |
|
Ah, sorry. I missed that. And yes it does work more as I would expect it to if that is fixed. However, I'm still unsure about the |
|
So I admit it is a bit confusing, but I think its working as intended. The difference you're seeing is that refresh(todos) doesn't hold a plain store's transaction open, but it does hold an optimistic one |
|
Yeah I don't think this is a bug. But we need to do a lot better on explaining/documenting this. I don't think we have to resort into transition isolating APIs.. we have But you can also handle this derived. The same pattern for the save button. Your Button does exactly this with isBusy. Keep isEditing as the persistent truth, add an optimistic in-flight flag, and derive the view: const [isEditing, setIsEditing] = createSignal(false); // persistent: user intent
const [isSaving, setIsSaving] = createOptimistic(false); // lives only as long as the transaction
const showForm = () => isEditing() && !isSaving();
function onSaveClicked() {
setIsSaving(true); // optimistic: visible immediately
setIsEditing(false); // rides the transaction, commits at settle
props.onUpdate(editValue());
}Maybe not the most intuitive? |
|
Yeah, it does feel like there is quite a bit of learning to do for solid developers with this. I would worry about more complex cases being hard to debug. Like if you decided to change from I also realised you can use const [isEditing, setIsEditing] = createSignal(false);
function onSaveClicked() {
setTimeout(() => {
setIsEditing(false);
})
props.onUpdate(editValue());
} |
|
Yeah to be fair this is a property of these async models in general. Like React's Transition model has similar issue. It ends up being infectious. They want component authors to wrap like all their button clicks in transitions. Same with Svelte's upcoming one. I think the move from sync to async is when these sort of createStore -> createOptimisticStore moments happen, because it signals a move in the authorative source of truth.. The async itself is what causes the holding behavior so regardless of the primitive you are forced into aligning to that. Like if someone has a bunch of distributed setStore calls all over the place changing to optimistic is going to be pretty breaking regardless bucause it is reverting behavior. Admittedly this is why I've always favored the Regarding setTimeout that's what I meant in my response above. It's fine. I'd use it a bit sparingly. It does push the update on 2 different cycles but that is relatively cheap. I'd almost recommend it in many cases but its hard to call it the most general approach because it has the behavior implications. Moving to a discussion. |
Uh oh!
There was an error while loading. Please reload this page.
Describe the bug
I'm not sure if this is a bug or if my understanding of
createOptimistic/actionis flawed but sometimes I have a hard time understanding how these work together. If it's not a bug then I find it fairly unintuitive.I created a stackblitz example here. In it there are 2 versions of a todo example with a simulated backend. The first one uses
createOptimisticStoreas the way to store the todos, the second one just usescreateStoreinstead.In the optimistic example a lot of it works the way I would expect. You can add or remove todos and the updates show up straight away. In the store example there is a pause while it waits for the backend work to complete.
However, in the store example it behaves in an optimistic way for updating todos. Marking a todo as done or updating it's name shows up straight away, it doesn't wait. I don't really understand what I'm doing differently between adding/removing vs updating.
The second thing that I find more confusing is that in the optimistic example, clicking the save button on a todo after editing it causes it to wait,
setIsEditing(false)doesn't resolve until the transition is finished. While in the store example this doesn't happen. Which seems reversed to me, why is the optimistic version waiting? Also, theisEditingis just an ordinary signal, why is its behaviour tied to whether I use a store or an optimistic store?Your Example Website or App
https://stackblitz.com/edit/vitejs-vite-faxvqtln?file=src%2FTodo.tsx
Steps to Reproduce the Bug or Issue
There is a wait time before
isEditing()returns false againThere is no wait time for
isEditing()to return false againExpected behavior
.
Screenshots or Videos
No response
Platform
Additional context
No response
All reactions