vanjs-org / van

🍦 VanJS: World's smallest reactive UI framework. Incredibly Powerful, Insanely Small - Everyone can build a useful UI app in an hour.
https://vanjs.org
MIT License
3.87k stars 91 forks source link

Conditional root rendering #213

Open kwameopareasiedu opened 10 months ago

kwameopareasiedu commented 10 months ago

I have this component which is supposed to show a loader while it checks the auth state and then, if authenticated, show the child.

export function AuthGuard(child: HTMLElement) {
  const authenticating = van.state(true);
  const authenticated = van.state(false);

  auth.onAuthStateChanged((user) => {
    if (user) {
      authenticated.val = true;
    } else navigate(LOGIN_ROUTE);

    authenticating.val = false;
  });

  if (authenticating.val || !authenticated.val) {
    return div(
      { className: "w-full h-full grid place-items-center" },
      Text({ align: "center" }, "Authenticating. Please wait...")
    );
  } else return child;
}

The issue I run into is the tab freezes and from Chrome's tab manager, it just consumes memory at an exponential rate. I'm guessing using the authenticated.val in the function's body (and not in a derive or the content) causes an infinte binding. Screenshot from 2023-12-26 02-01-06

How do I resolve this?

Tao-VanJS commented 10 months ago

I don't have a good idea about what's going wrong in your code. I don't think purely accessing the val property of a State object would cause an infinity loop. I suspect there could be other parts of the code that cause the infinity loop. I would suggest that you can start with some profiling tool (e.g.: https://developer.chrome.com/docs/devtools/memory-problems/allocation-profiler) to see what's going wrong in the web page.

kwameopareasiedu commented 10 months ago

@Tao-VanJS From the profile, and some logging, I think that's what's happening. The profiler shows an unending series of the calls setVal -> derive -> runAndCaptureDeps

image

kwameopareasiedu commented 10 months ago

However, if I move the state and Firebase auth callback outside the component, it works as expected.

AuthGuard

const authenticating = van.state(true);
const authenticated = van.state(false);

auth.onAuthStateChanged((user) => {
  console.log(user)
  if (user) authenticated.val = true;
  else navigate(LOGIN_ROUTE);
  authenticating.val = false;
});

export function AuthGuard(child: HTMLElement) {
  if (authenticating.val || !authenticated.val) {
    return div(
      { className: "w-full h-full grid place-items-center" },
      Text({ align: "center" }, "Authenticating. Please wait...")
    );
  } else return child;
}

App

export default function App() {
  return Router({
    className: "w-screen h-screen",
    routes: [
      { path: HOME_ROUTE, component: () => AuthGuard(HomePage()) },
      { path: LOGIN_ROUTE, component: LoginPage },
      { path: SIGNUP_ROUTE, component: SignupPage }
    ]
  });
}

image

Currently using version 1.2.7 of vanjs-core

sirenkovladd commented 9 months ago

Can you try

export function AuthGuard(child: HTMLElement) {
  const authenticating = van.state(true);
  const authenticated = van.state(false);

  auth.onAuthStateChanged((user) => {
    if (user) {
      authenticated.val = true;
    } else navigate(LOGIN_ROUTE);

    authenticating.val = false;
  });

  return () => {
    if (authenticating.val || !authenticated.val) {
      return div(
        { className: "w-full h-full grid place-items-center" },
        Text({ align: "center" }, "Authenticating. Please wait...")
      );
    } else return child;
  }
}

export default function App() {
  return Router({
    className: "w-screen h-screen",
    routes: [
      { path: HOME_ROUTE, component: AuthGuard(HomePage()) },
      { path: LOGIN_ROUTE, component: LoginPage },
      { path: SIGNUP_ROUTE, component: SignupPage }
    ]
  });
}
kwameopareasiedu commented 9 months ago

Unfortunately, this doesn't work since this calls the AuthGuard function instead of passing it as the component function to the router...

sirenkovladd commented 9 months ago

https://jsfiddle.net/Sirenko/jv29wexf/55/

Here's an example, + fixed a bug with the garbage collector for the HomePage (if there was reactivity on it, it would not work correctly after switching)