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

API change: resolve WebSdkApi.connect() only once connection established and agent available #104

Open
mattyg opened this issue Aug 9, 2023 · 2 comments

Comments

@mattyg
Copy link
Contributor

mattyg commented Aug 9, 2023

It's seems very weird to me that WebSdkApi.connect() resolves before the agent is actually available, so we have to do this (from the README):

  // We just started up, so we're still connecting. Let's wait for isAvailable == true
  const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
  while (!client.agent.isAvailable) {
    await sleep(50)
    // ATTN! This is not production quality code. In a real UI, you should register an event handler for `client.on('agent-state')`
    // and store the agent state in reactive state so that your components can just branch on isAvailable.
  }

Instead connect() should just not resolve the promise until the agent is available (or preferably signUp() should not resolve until the agent is available #102).

@robbiecarlton
Copy link
Contributor

Hey @mattyg - The code in that example is very bad. I recently decided to just take it out of the docs, as despite the warning, I've seen it replicated many times when someone has implemented holo in their happ (understandably, it's in the README).

But that's slightly orthogonal to your suggestion.

One reason for returning immediately from .connect is to let the UI stay responsive. Rather than blocking on the .connect call, the UI subscribes to the agent-state event, and updates it's state accordingly as that gets updated.

@mattyg
Copy link
Contributor Author

mattyg commented Aug 11, 2023

One reason for returning immediately from .connect is to let the UI stay responsive. Rather than blocking on the .connect call, the UI subscribes to the agent-state event, and updates it's state accordingly as that gets updated.

But in practice you're going to be calling .connect in a function and not waiting for that function, i.e.:

<template> 
  <div v-if="loading">Loading...</div>
  <MyApp v-else />
</template>
<script lang="ts" setup>
const loaded = ref(false);

onMounted(() => {
  setupClient();
});

const setupClient = async () => {
   await WebSdkApi.connect(...);
   loaded.value = true;
}
</script>

This is how you would do it with the holochain client, right? Does it make sense to require more code to setup the two clients differently?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants