Holo-Host / web-sdk

Standard development kit for creating Web UIs that work with Holo Hosting.
176 stars 5 forks source link

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

Open mattyg opened 1 year ago

mattyg commented 1 year ago

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 commented 1 year ago

@robbiecarlton