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

Agent state inconsistently is defined before WebSdkApi.connect resolves #113

Open
mattyg opened this issue Oct 23, 2023 · 1 comment
Open

Comments

@mattyg
Copy link
Contributor

mattyg commented Oct 23, 2023

There are some weird complexity with how WebSdkApi is initialized, resulting in 2 different forms of initialization that need to be handled by the app developer.

There should only be one route for initialization, such that the developer only needs a single if/else conditional to distinguish between holo & holochain login (i.e it should be this #104).

case 1: Agent State is defined before const client = await WebSdkApi.connect() resolves. Thus the app can read the agent state via client.agentState.

case 2: Agent State is defined after const client = await WebSdkApi.connect() resolves. Thus the app must listen to emitted events to read the agent state via client.on('agent-state', (state) => {})

It's inconsistent which case will occur each time the client is initialized, and thus the developer must handle both cases, resulting in this unnecessarily complex bunch of code to setup a holo client:

export const setupHolo = async () => {
  try {
    const client = await WebSdkApi.connect({
      chaperoneUrl: HOLO_CHAPERONE_URL,
      authFormCustomization: {
        appName: 'My app'
        requireRegistrationCode: false,
      },
    });

    await new Promise((resolve) => {
     client.on('agent-state', (state: AgentState) => {
          if (state.isAvailable && state.isAnonymous) {
            client.signUp({});
          } else if (state.isAvailable && !state.isAnonymous) {
            resolve(state);
          }
        });
        
      if (client.agentState.isAvailable && client.agentState.isAnonymous) {
        client.signUp({});
      } else if (client.agentState.isAvailable && !client.agentState.isAnonymous) {
        resolve(client.agentState);
      }
    });

    return client;
  } catch (e) {
    console.log('Holo client setup error', e);
    throw e;
  }
};
@mattyg
Copy link
Contributor Author

mattyg commented Oct 23, 2023

@robbiecarlton

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

1 participant