Okay, so let's talk about advanced concepts. So what I mean by advanced concepts is that we can do really complicated things. So let's make a new route, which gonna display a set of users. So inside of here, we're gonna say new file, index.tsx. And as always, we have to have a default component. And so you're here, I delete everything. I will see just, hello quick, right? And so what I would like to do is show a list of, let's show a list of all your favorite repos, right? So I'm gonna just cheat a little bit and copy the code here. Oh, I see this is a different code, okay. So we will use this favorite, okay. Let me copy this code here. To here. Here. And here. Okay, and so instead of returning the length, I'm gonna return a favorite, and that's gonna be, I don't have a type information set up for the superbase, but you could definitely do that, there's a stuff you could do. So we're just gonna say that we're gonna have email string user, oh, perfect, okay. I don't know how to compile a new list, but there's correct information. Okay, so now let's go and get it. So that's not is favorite, use favorites. Let's connect that to the whole, so that Experian becomes like a second method. There we go. Saving this. I think we already got the demo going, so save it exactly as we did the demo, and then we can forget about it. And then everything we done so far, and we can go feed our watch, which is gonna be the first round of media creation. And then the lab, which is gonna be the last one we use for media creation. I'm gonna show you two different ways of doing it in a minute. I'm gonna create a user, that's feeds this index, let's year, yeah, just, Okay, let's see, all because we create gray, we want to query use a specific repo, and I'm just going to put all this, there we go, so here's our list of favorites. So there's two, okay, so we have a list of favorites here and I can click on any one of them and opened up in a separate file and come over there. So that should be pretty straightforward, so let's do the next thing, which is, I would like to execute, basically, I would like to have a search box here, which allows me to search for any specific username in it, so let's see, I think what I have in mind is here, it's username input.text user. User needs use signal. So, you know, I just have username and had to type in here, and I would like to display all the users that are in that document, in there. So, but because I want to talk to the server, I don't want to overwhelm the server, and so what I really want is I want to have a debounce server debounce, debounce user, which is, well, what is this? Which is a, it's same thing as a user, but delayed, right? So by print debounce user dot value, I would like to see it delayed. So for that is something called a use task. So used to ask is similar to use effect for people who are familiar with React, but there's enough differences that we thought that naming it the same exact thing would cause confusion. And so while you can think of it kind of like user effect, there is enough differences between user effect, how user effect works and how it works in Quick that it's worth kind of going into it. So what I'm gonna do is I'm gonna say track and we're gonna say that. So we basically saying, hey, whenever a user dot value changes, we wanna rerun this function. So, so far it doesn't really do anything useful. So what I'm gonna do is we're gonna say, set timeout. And we gonna say that debounce user dot value is equal to value after 500 milliseconds. And then we also have to do a cleanup. So if you, if this function reruns before the, you know, before 5 milliseconds is up, then we should do a cleanup or we clear the timer, right? And so now notice there's a username here and I can type in Hevery, and as long as I'm typing, nothing's happening, but the moment I stopped typing half a second later, M Hevery shows up in this page. Sorry, I was just rereading the question, I already answered this. Okay, so as you can see that there is a delay right? Is a half a second delay, but notice as long as I keep typing, the half a second delay doesn't apply. It's only when I stop typing, right? So we're basically setting up a task and the task says, hey, whenever the user value changes, because there's a track thing in here, go set up a timeout and this timeout, this timeout sets the debounce user, a value to the current value of whatever the track gives you but it's a 500 seconds late and of course there's a cleanup function that is, if this function reruns before the other side then it's not right. Now you might ask yourself like, well, why isn't it a use task? Well, the first big difference, why isn't it just called use effect? The first big difference is that in React, use effect does not run on a server and in our case, if I do, we started console, a log, user let's say the user value. Notice that the user actually, user debugs. It was fatal debugs, okay. Notice that this actually runs on a server and the reason why this runs on a server is because on a server we set up tracking of this particular value, right, so the server learns about the fact that whenever a user changes, this function has to be rerun. And so because we learned this on a server, the fact that this is attached over here, on the client if you look at the console, no JavaScript has to execute, right, but when I start typing, it is at that point that the user debounce task re-executes on the client, but it knew to re-execute on a client because on a server it's set up a subscription to this signal constant, right? If we didn't execute this on a server, then we would not know, we would either have to eagerly execute this task on the client to set up a subscription or this would simply not rerun because, well, the subscription doesn't work. And so you might say, like, yeah, but like in the React, you just put the subscriptions in here on the end. Sure, but the same problem kind of arises, which is that the only reason it works in React is because React re-executes the component all the time, right? Whereas if I do a console log here, if I do a render, I'm gonna see a render on a server, but I don't see a render on the client. Even if I type, the render still is not happening on the client. What is the difference between use task and solid create effect? Why do you need to track? Can't it automatically know that there is a sign, signal, and rerun? Okay, so all of these are good questions. So the use task is, in many ways, very, very similar to create effect, but there are a couple of differences. So the first difference is that this task can be async. So we could rewrite this function like so. So let's say we have const delay equals, delay equals, yes, okay. Oh, right, but this will not give us a cleanup. Okay, that's okay, we can do it differently. We can treat this and clean up is a function that takes a B, which is a number I think when we return. And then we have ID. Is this gonna work? Yo, yo, yo, you don't like it, sorry. Get term if I fall, so. Okay, so what we can do is we can rewrite this code as await delay 500 cleanup. Oh, I'm sorry, sorry, I missed out here. The cleanup is a callback, which is a function which takes, which produces. I'm making this unnecessarily complicated, sorry about that. So in this particular case, this function is now async. And so we just need to do this part. All right. Okay, yeah, it works. Okay, so this is an alternate implementation. And the thing to understand here is that we were now allowed to do an await statement. So the first difference is that the use task is asynchronous. It can be a synchronous and therefore it's meant for side effect the things. And therefore it's meant not to automatically track. The problem is that if you put user value in here you could automatically track it. But if you put it behind the evade statement you couldn't automatically track it. And so you now you have a choice. Like you can have an API that says, okay we can do automatic tracking but you can't have async functions. Or you can have an API that says you can have async tracking, but you can only have automatic traffic in front of the await statement but not after the await statement. Or you can have an API that's a lot more consistent and say, hey, you just either, when it's asynchronous you just can't do automatic tracking. And so because we mean for this thing to really have side effects outside of the application we chose not to do automatic tracking in this case but notice that in the other example, which was right here we were using use computed, right? And use computed doesn't need tracking. And that's because use computing must be synchronous. And so the mental model is that if you are doing a derived value that can be synchronously gotten from an existing state then you should use, use computed and there is no need to do explicit tracking and it's all automatic. On the other hand, if you are not really producing a value but instead you wanna create a side effect that it happens outside of the system then it's probably gonna be asynchronous. And if you do, then you have to do tracking. And there is a third kind of way of looking at it which we haven't covered yet, but we'll cover soon is that maybe I wanna get a value but the value is not derived from the current state but really I have to go external state. And so that's a use resource.
Comments