Okay. Let me show you a next thing. What if I have a clock? A clock only shows up when you scroll into it. Notice the clock is running. Let me show this again for you. I have no JavaScript, scroll the clock into the position, when the clock shows up, the JavaScript shows up, and it starts running.
What exactly does the clock do? Well, there is a useVisible task which you can think of as useEffect. UseEffect runs on hydration, but there is no hydration, so when exactly are we supposed to run something like that? Well, the answer is you run it when the component becomes visible. This piece of code naturally does what you would expect, but it has an added interesting behaviour which is like, hey, if the component is invisible, why even bother downloading, executing or doing anything like that?
Let me show you one more interesting thing. We have a button here that says Do something, and I'm going to flip over to the console log. This Do something says I'm going to do work, expensive work, and return some value. Let me show this to you in here. There's an RPC component that says render, a click button that says hey, I'm going to do the work, I'm going to create some date, and I'm going to create a function that says do work, and I'm going to pretend this is the expensive work, and, just for fun, I'm going to return a function that actually has a value. Typical thing you do, you return closures and pass things around, right? Obviously, all this happens on the browser because, well, it's on the browser. But what if I can do crazy magic like this, say server, and all of a sudden, if I refresh, and I click the button, notice the clink was on a client, the return value is on a client, but the expensive work, well, that showed up on a server. That's pretty magical when you can do that. One of the things we talk about in Click is the idea of a unified execution model, and I can show you more of that later. And where is my slides? My slides disappeared. Here they are. Anyways, that's kind of my talk. Hope you enjoyed it, and hope you kind of seen the point of it, and if you have any questions, I'm happy to answer them.
Here's a philosophical one. Is a button immediately clickable? It needs to download a small piece of JavaScript first, doesn't it? Yeah, that's a good question. So there is something called a quick loader. A quick loader is very small, about one kilobyte, and it executes in about one millisecond on a desktop and about 10 milliseconds on a mobile. And all it does is sets up a global listener. And so that script tag is actually included in the HTML so that it's immediate. And so at that point, nothing else happens. So when you click on a button, the event propagates up and the top level global listener catches it, looks at the button and sees that the button has an attribute, and the attribute says something like on colon click equals and it has a URL, and that's where it goes and fetches the code. And we know that that URL will actually cause a cache hit.
Comments