Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify websocket hooks initialization #396

Open
codeofmochi opened this issue Aug 17, 2023 · 0 comments
Open

Simplify websocket hooks initialization #396

codeofmochi opened this issue Aug 17, 2023 · 0 comments
Labels
bug 🪲 Something isn't working chore Generic task or chore

Comments

@codeofmochi
Copy link
Contributor

Update: after discussion we can actually both respect the rules of hooks and reduce the boiler plate, by simply considering the enableWebsocket && websocketClient from within the deepest hook itself instead of at configuration or call time. Since we can capture the configuration value from within the closure of the hook function, there is no need for additional checks at call site.

I.e.

// this is wrong
const wsHooks = enableWebsocket && websocketClient ? configureFooHooks() : undefined;
useFoo() {
    wsHooks?.useFooUpdates() // <-- ! rules of hooks
}
configureFooHooks() {
   return {
     useFooUpdates: () => { ... implementation }
   }
}

// this is verbose
const { useFooUpdates } = configureFooHooks()
useFoo() {
   useFooUpdates()
}
configureFooHooks(client) {
   if (!client) return {
     useFooUpdates: () => { /* no-op hook */ }
   } else return {
     useFooUpdates: () => { ... implementation }
   }
}

// this is correct
const { useFooUpdates } = configureFooHooks()
useFoo() {
   useFooUpdates()
}
configureFooHooks(client) {
   return {
       useFooUpdates() { // <-- single definition
           useEffect() {
               if (!client || !wsEnabled) return;    // <-- call stack is preserved so no rules of hooks violation
               else // ... implementation
           }
           ...
       }
   }
}

Originally posted by @dialexo in graasp/graasp-apps-query-client#142 (comment)

@codeofmochi codeofmochi self-assigned this Aug 17, 2023
@codeofmochi codeofmochi added bug 🪲 Something isn't working chore Generic task or chore labels Aug 17, 2023
@codeofmochi codeofmochi removed their assignment Sep 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🪲 Something isn't working chore Generic task or chore
Projects
None yet
Development

No branches or pull requests

1 participant