S
SolidJS7mo ago
gsoutz

What do I do if I have to wait for things to happen, meanwhile define extra state or what

this piece of code come up as crucial functionality all over the place for me, for an interactive application of solidjs
createEffect(on(uci, uci => {
set_uci(uci)
setTimeout(() => {
set_active_candidate_to_next_guess()
}, 1000)
}))
createEffect(on(uci, uci => {
set_uci(uci)
setTimeout(() => {
set_active_candidate_to_next_guess()
}, 1000)
}))
So I set something, and wait for user to acknowledge it, then I go to next state. While I am waiting some kind of pending state happening. Do I keep track of these waiting states, because I have some timing related bugs creeping in my code I don't know how to fix. Not considering waiting for some asynchronous processes on the way.
9 Replies
REEEEE
REEEEE7mo ago
What does waiting for the user to acknowledge it look like? Is it a button they have to press?
gsoutz
gsoutzOP7mo ago
No just mentally give it a time. Like a chess app, they make a move, I give them a second to see the move on the board. Then I take back the move, because they have to make another move on the previous position.
bigmistqke
bigmistqke7mo ago
You could use resource to take care of the tracking for you?
const [waiting] = createResource(uci, () => new Promise(resolve => setTimeout(resolve, 1000))
const [waiting] = createResource(uci, () => new Promise(resolve => setTimeout(resolve, 1000))
Everytime uci changes waiting will set its pending state to pending for 1000ms. With anything async, resource is probably the answer.
because I have some timing related bugs creeping in my code I don't know how to fix
What kind of bugs are you facing?
gsoutz
gsoutzOP7mo ago
in these pending states i make the board not movable, but sometimes when i click buttons to get a fresh new state, board is still not movable, it happens rarely though, can't reproduce. about using the resource maybe I want to cancel the timeout how do I handle, that maybe some good abstraction comes out of this.
bigmistqke
bigmistqke7mo ago
you could pass a delta to timeout: https://playground.solidjs.com/anonymous/ee800b38-8f26-4f79-9047-239016f0bf4a using resource only as a pending-state setter is probably not completely using the full potential of that api. most likely you could move a lot of logic into createResource as well
gsoutz
gsoutzOP7mo ago
I don't understand how is that clear the timeout with clearTimeout, instead setTimeout(resource, 0) will trigger the timeout immediately
bigmistqke
bigmistqke7mo ago
a i see, you want to be pending forever i don't really know all the logic you want to implement, but as my example shows, you can use signals as ways to trigger resources.
gsoutz
gsoutzOP7mo ago
ok ok thank you
bigmistqke
bigmistqke7mo ago
ur welcome!
Want results from more Discord servers?
Add your server